From 32f6f84ed1f75e51a41dd8c3701b938749a87c23 Mon Sep 17 00:00:00 2001 From: Kenta HATTORI Date: Sun, 13 Oct 2024 01:08:24 +0900 Subject: [PATCH] Add: rm commands --- README.md | 62 +++++++++++++++++++++++++++----------- src/client.ts | 12 ++++---- src/commands/rm.ts | 65 ++++++++++++++++++++++++++++++++++++++++ src/commands/rmdir.ts | 2 +- test/commands/rm.test.ts | 14 +++++++++ 5 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 src/commands/rm.ts create mode 100644 test/commands/rm.test.ts diff --git a/README.md b/README.md index fb0817a..ddc947a 100644 --- a/README.md +++ b/README.md @@ -29,17 +29,36 @@ USAGE # Commands -* [`ketool mkdir DIRECTORY`](#ketool-mkdir) -* [`ketool rmdir DIRECTORY`](#ketool-rmdir) * [`ketool help [COMMAND]`](#ketool-help-command) +* [`ketool mkdir DIRECTORY`](#ketool-mkdir-directory) +* [`ketool rm OBJECT`](#ketool-rm-object) +* [`ketool rmdir DIRECTORY`](#ketool-rmdir-directory) -## `ketool mkdir DIRECTORY` +## `ketool help [COMMAND]` -Say hello +Display help for ketool. ``` +USAGE + $ ketool help [COMMAND...] [-n] + +ARGUMENTS + COMMAND... Command to show help for. + +FLAGS + -n, --nested-commands Include all nested commands in the output. + +DESCRIPTION + Display help for ketool. +``` + +_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v6.2.15/src/commands/help.ts)_ + +## `ketool mkdir DIRECTORY` + Create the directories, if they do not already exist. +``` USAGE $ ketool mkdir DIRECTORY... [-u ] [-t ] [-c ] [-p] [-v] @@ -62,20 +81,21 @@ EXAMPLES _See code: [src/commands/mkdir.ts](https://github.com/fixpoint/ketool/blob/v0.0.0/src/commands/mkdir.ts)_ -## `ketool rmdir DIRECTORY` +## `ketool rm OBJECT` Remove the directories, if they are empty. ``` USAGE - $ ketool rmdir DIRECTORY... [-u ] [-t ] [-c ] [-p] [-v] + $ ketool rm OBJECT... [-u ] [-t ] [-c ] [-f] [-r] [-v] ARGUMENTS - DIRECTORY... path of the directory to be removed + OBJECT... path of the object to be removed FLAGS -c, --cwd= set current working directory to VALUE - -p, --parent remove DIRECTORY and its ancestors + -f, --force ignore nonexistent objects and arguments + -r, --recurcive remove directories and their contents recursively -t, --token= API access token of the Kompira Enterprise server -u, --baseurl= base URL of the Kompira Enterprise server -v, --verbose print a message for each removed directory @@ -84,29 +104,35 @@ DESCRIPTION Remove the directories, if they are empty. EXAMPLES - $ ketool rmdir + $ ketool rm ``` -_See code: [src/commands/rmdir.ts](https://github.com/fixpoint/ketool/blob/v0.0.0/src/commands/rmdir.ts)_ +_See code: [src/commands/rm.ts](https://github.com/fixpoint/ketool/blob/v0.0.0/src/commands/rm.ts)_ -## `ketool help [COMMAND]` +## `ketool rmdir DIRECTORY` -Display help for ketool. +Remove the directories, if they are empty. ``` USAGE - $ ketool help [COMMAND...] [-n] + $ ketool rmdir DIRECTORY... [-u ] [-t ] [-c ] [-p] [-v] ARGUMENTS - COMMAND... Command to show help for. + DIRECTORY... path of the directory to be removed FLAGS - -n, --nested-commands Include all nested commands in the output. + -c, --cwd= set current working directory to VALUE + -p, --parent remove DIRECTORY and its ancestors + -t, --token= API access token of the Kompira Enterprise server + -u, --baseurl= base URL of the Kompira Enterprise server + -v, --verbose print a message for each removed directory DESCRIPTION - Display help for ketool. -``` + Remove the directories, if they are empty. -_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v6.2.15/src/commands/help.ts)_ +EXAMPLES + $ ketool rmdir +``` +_See code: [src/commands/rmdir.ts](https://github.com/fixpoint/ketool/blob/v0.0.0/src/commands/rmdir.ts)_ diff --git a/src/client.ts b/src/client.ts index d5dd959..c200fff 100644 --- a/src/client.ts +++ b/src/client.ts @@ -64,21 +64,21 @@ export namespace KeClient { export async function create(config: Config, path: string, name: string, data: any): Promise { let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) let resp: rm.IRestResponse = await rest.create(path, data, request_options(config.token)) - if (resp.statusCode == 400) { - - } else if (resp.statusCode != 201) { + if (resp.statusCode != 201) { throw new Error(`StatusCode: ${resp.statusCode}`) } return resp.result } - export async function del(config: Config, path: string): Promise { + export async function del(config: Config, path: string, force: boolean = false): Promise { let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) let resp: rm.IRestResponse = await rest.del(path, request_options(config.token)) - if (resp.statusCode != 204) { + if (force && resp.statusCode == 404) { + return false + } else if (resp.statusCode != 204) { throw new Error(`StatusCode: ${resp.statusCode}`) } - return resp.result + return true } } diff --git a/src/commands/rm.ts b/src/commands/rm.ts new file mode 100644 index 0000000..391a424 --- /dev/null +++ b/src/commands/rm.ts @@ -0,0 +1,65 @@ +import {Args, Command, Flags} from '@oclif/core' +import {common as common_flags} from '../flags.js' +import path from 'path' +import Config from '../config.js' +import KeClient from '../client.js' +import {DIRECTORY_TYPE} from '../const.js' + + +export default class Rm extends Command { + static override args = { + object: Args.string({ + description: 'path of the object to be removed', + required: true, + }), + } + static strict = false + + static override description = 'Remove the directories, if they are empty.' + + static override examples = [ + '<%= config.bin %> <%= command.id %>', + ] + + static override flags = { + ...common_flags, + // flag with a value (-c, --cwd=VALUE) + cwd: Flags.string({char: 'c', description: 'set current working directory to VALUE'}), + force: Flags.boolean({char: 'f', description: 'ignore nonexistent objects and arguments'}), + recurcive: Flags.boolean({char: 'r', description: 'remove directories and their contents recursively'}), + verbose: Flags.boolean({char: 'v', description: 'print a message for each removed directory'}), + } + + public async run(): Promise { + const {argv, flags} = await this.parse(Rm) + let cwd = '/' + if (flags.cwd) { + // cwd のパラメータチェック + if (!path.isAbsolute(flags.cwd)) { + throw new Error(`cwd should be absolute path: ${flags.cwd}`) + } + cwd = path.normalize(flags.cwd) + } + const config = new Config(flags) + for (let i = 0; i < argv.length; i++) { + const target = path.resolve(cwd, argv[i] as string) + await this.remove_object(config, target, flags.force, flags.recursive, flags.verbose) + } + } + + private async remove_object(config: Config, target: string, force: boolean = false, recursive: boolean = false, verbose: boolean = false) { + if (!force) { + let result = await KeClient.get(config, target) + if (result == null) { + throw new Error(`failed to remove: '${target}' is not found`) + } + if (result.type_object == DIRECTORY_TYPE) { + throw new Error(`failed to remove: '${target}' is a directory`) + } + } + let removed = await KeClient.del(config, target, force) + if (verbose && removed) { + this.log(`removed: ${target}`) + } + } +} diff --git a/src/commands/rmdir.ts b/src/commands/rmdir.ts index 1c468e0..5e5f353 100644 --- a/src/commands/rmdir.ts +++ b/src/commands/rmdir.ts @@ -41,7 +41,7 @@ export default class Rmdir extends Command { } const config = new Config(flags) for (let i = 0; i < argv.length; i++) { - let result = await this.remove_directory(config, cwd, argv[i] as string, flags.parent, flags.verbose) + await this.remove_directory(config, cwd, argv[i] as string, flags.parent, flags.verbose) } } diff --git a/test/commands/rm.test.ts b/test/commands/rm.test.ts new file mode 100644 index 0000000..087458f --- /dev/null +++ b/test/commands/rm.test.ts @@ -0,0 +1,14 @@ +import {runCommand} from '@oclif/test' +import {expect} from 'chai' + +describe('rm', () => { + it('runs rm cmd', async () => { + const {stdout} = await runCommand('rm') + expect(stdout).to.contain('hello world') + }) + + it('runs rm --name oclif', async () => { + const {stdout} = await runCommand('rm --name oclif') + expect(stdout).to.contain('hello oclif') + }) +})