-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |