Skip to content

Commit

Permalink
Add: rm commands
Browse files Browse the repository at this point in the history
  • Loading branch information
khattori committed Oct 12, 2024
1 parent a55b2e2 commit 32f6f84
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 25 deletions.
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,36 @@ USAGE
<!-- usagestop -->
# Commands
<!-- 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 <value>] [-t <value>] [-c <value>] [-p] [-v]
Expand All @@ -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 <value>] [-t <value>] [-c <value>] [-p] [-v]
$ ketool rm OBJECT... [-u <value>] [-t <value>] [-c <value>] [-f] [-r] [-v]
ARGUMENTS
DIRECTORY... path of the directory to be removed
OBJECT... path of the object to be removed
FLAGS
-c, --cwd=<value> 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=<value> API access token of the Kompira Enterprise server
-u, --baseurl=<value> base URL of the Kompira Enterprise server
-v, --verbose print a message for each removed directory
Expand All @@ -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 <value>] [-t <value>] [-c <value>] [-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=<value> set current working directory to VALUE
-p, --parent remove DIRECTORY and its ancestors
-t, --token=<value> API access token of the Kompira Enterprise server
-u, --baseurl=<value> 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)_
<!-- commandsstop -->
12 changes: 6 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ export namespace KeClient {
export async function create(config: Config, path: string, name: string, data: any): Promise<ObjectResponse | null> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.create<ObjectResponse>(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<ObjectResponse | null> {
export async function del(config: Config, path: string, force: boolean = false): Promise<boolean> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.del<ObjectResponse>(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
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/commands/rm.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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}`)
}
}
}
2 changes: 1 addition & 1 deletion src/commands/rmdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/commands/rm.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 32f6f84

Please sign in to comment.