Skip to content

Commit

Permalink
Add: ls command
Browse files Browse the repository at this point in the history
  • Loading branch information
khattori committed Nov 1, 2024
1 parent 1cb3f5d commit dd5d0dd
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export async function get(config: Config, path: string): Promise<ObjectResponse
return resp.result
}

export async function getAll(config: Config, path: string): Promise<ObjectsResponse | null> {
export async function getChildren(config: Config, dirPath: string): Promise<ObjectsResponse | null> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(path, requestOpts(config.token))
const resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(`${dirPath}.children`, requestOpts(config.token))
if (resp.statusCode === 404) {
return null
}
Expand Down
86 changes: 86 additions & 0 deletions src/commands/ls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {Args, Command, Flags} from '@oclif/core'
import path from 'node:path'

import * as KeClient from '../client.js'
import {checkCwd, checkInsecure} from '../common.js'
import Config from '../config.js'
import {DIRECTORY_TYPE} from '../const.js'
import {common as commonFlags} from '../flags.js'


export default class Ls extends Command {
static override args = {
object: Args.string({description: 'file to read'}),
}

static override description = 'list information about the OBJECTs.'

static override examples = [
'<%= config.bin %> <%= command.id %>',
]

static override flags = {
...commonFlags,
// flag with a value (-c, --cwd=VALUE)
cwd: Flags.string({char: 'c', description: 'set current working directory to VALUE'}),
long: Flags.boolean({char: 'l', description: 'use a long listing format'}),
}

static strict = false

private conf!: Config

public async run(): Promise<void> {
const {argv, flags} = await this.parse(Ls)
this.conf = new Config(flags)
checkInsecure(flags.insecure)
const cwd = await checkCwd(this.conf, flags.cwd)

if (argv.length === 0) {
argv.push('.')
}

const withHeader = argv.length > 1
const results = await Promise.all(
argv.map((arg) => this.listObj(path.resolve(cwd, arg as string), withHeader, flags.long))
)
if (results.includes(false)) {
this.exit(1)
}
}

private _printObj(target: KeClient.ObjectResponse, indent: string, longFormat?: boolean) {
if (longFormat) {
const {name} = path.parse(target.abspath)
this.log(`${indent}${target.owner} ${target.updated} ${name}:${target.type_object}`)
} else {
const {name} = path.parse(target.abspath)
this.log(`${indent}${name}`)
}
}

private async listObj(targetPath: string, withHeader: boolean, longFormat?: boolean) {
const target = await KeClient.get(this.conf, targetPath)
if (target === null) {
this.warn(`failed to get '${targetPath}': no such object or directory`)
return false
}

if (target.type_object === DIRECTORY_TYPE) {
const resp = await KeClient.getChildren(this.conf, targetPath)
let indent = ''
if (withHeader) {
this.log(`${target.abspath}:`)
indent = ' '
}

for (const child of resp!.results) {
this._printObj(child, indent, longFormat)
}
} else {
this._printObj(target, '', longFormat)
}

return true
}
}
2 changes: 1 addition & 1 deletion src/commands/rmdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class Rmdir extends Command {
}

// ディレクトリが空かどうかチェックする
const results = await KeClient.getAll(this.conf, `${targetDir}.children`)
const results = await KeClient.getChildren(this.conf, targetDir)
if (results!.count > 0) {
this.warn(`failed to remove directory: '${targetDir}' is not empty`)
return
Expand Down
17 changes: 17 additions & 0 deletions test/commands/ls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'

describe('ls', () => {
before(async () => {
await runCommand('rm testdir --cwd /root -rfk')
await runCommand('mkdir testdir --cwd /root -k')
})
after(async () => {
await runCommand('rm testdir --cwd /root -rfk')
})

it('runs ls --help', async () => {
const {stdout} = await runCommand('ls --help')
expect(stdout).to.contain('USAGE')
})
})

0 comments on commit dd5d0dd

Please sign in to comment.