From dd5d0ddafb75ce9f1196928acad6fc2edd819048 Mon Sep 17 00:00:00 2001 From: Kenta HATTORI Date: Sat, 2 Nov 2024 06:58:39 +0900 Subject: [PATCH] Add: ls command --- src/client.ts | 4 +- src/commands/ls.ts | 86 ++++++++++++++++++++++++++++++++++++++++ src/commands/rmdir.ts | 2 +- test/commands/ls.test.ts | 17 ++++++++ 4 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/commands/ls.ts create mode 100644 test/commands/ls.test.ts diff --git a/src/client.ts b/src/client.ts index 5c926bf..78305e8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -54,9 +54,9 @@ export async function get(config: Config, path: string): Promise { +export async function getChildren(config: Config, dirPath: string): Promise { const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) - const resp: rm.IRestResponse = await rest.get(path, requestOpts(config.token)) + const resp: rm.IRestResponse = await rest.get(`${dirPath}.children`, requestOpts(config.token)) if (resp.statusCode === 404) { return null } diff --git a/src/commands/ls.ts b/src/commands/ls.ts new file mode 100644 index 0000000..ad12c9b --- /dev/null +++ b/src/commands/ls.ts @@ -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 { + 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 + } +} diff --git a/src/commands/rmdir.ts b/src/commands/rmdir.ts index 7fc40ff..2305fa6 100644 --- a/src/commands/rmdir.ts +++ b/src/commands/rmdir.ts @@ -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 diff --git a/test/commands/ls.test.ts b/test/commands/ls.test.ts new file mode 100644 index 0000000..f46e85e --- /dev/null +++ b/test/commands/ls.test.ts @@ -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') + }) +})