From 39116c89181faf621cbeca18dba59117f5bbb46f Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Wed, 25 Oct 2023 16:33:34 +0300 Subject: [PATCH] minor refactor --- packages/cli/src/commands/profile/cp.ts | 54 ++++++++---------------- packages/cli/src/commands/profile/use.ts | 4 +- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/packages/cli/src/commands/profile/cp.ts b/packages/cli/src/commands/profile/cp.ts index 2d2ed476..8fa8c9e6 100644 --- a/packages/cli/src/commands/profile/cp.ts +++ b/packages/cli/src/commands/profile/cp.ts @@ -13,11 +13,6 @@ const validateFsType = (fsType: string) => { return fsType } -const parseFsTypeFromUrl = (target: string) => { - const [fsType, ...rest] = target.split('://') - return rest.length ? validateFsType(fsType) : undefined -} - // eslint-disable-next-line no-use-before-define export default class CopyProfile extends BaseCommand { static description = 'Copy a profile' @@ -29,19 +24,15 @@ export default class CopyProfile extends BaseCommand { description: 'Source profile name, defaults to the current profile', required: false, }), - target: Flags.custom<{ url?: string; fsType: FsType }>({ - description: `Target profile. Either a full URL or a storage type, one of: ${fsTypes.join(', ')}`, + 'target-location': Flags.url({ + description: 'Target profile location', required: false, - parse: async input => { - if (isFsType(input)) { - return { fsType: input } - } - const fsType = parseFsTypeFromUrl(input) - if (!fsType) { - throw new Error(`Invalid target profile. Specify either a full URL or a storage type, one of: ${text.codeList(fsTypes)}`) - } - return { fsType, url: input } - }, + exclusive: ['target-storage'], + }), + 'target-storage': Flags.custom({ + description: 'Target profile storage type', + required: false, + options: [...fsTypes], })(), 'target-name': Flags.string({ description: 'Target profile name. If not specified, the command will ask for one', @@ -53,17 +44,14 @@ export default class CopyProfile extends BaseCommand { }), } - async source(profileConfig: LocalProfilesConfig): Promise<{ - alias: string - location: string - }> { + async source(profileConfig: LocalProfilesConfig): Promise<{ alias: string; location: string }> { if (this.flags.profile) { const { location } = await profileConfig.get(this.flags.profile) return { alias: this.flags.profile, location } } const result = await profileConfig.current() if (!result) { - throw new Error(`No current profile, specify the source alias with ${text.code('--profile')}`) + throw new Error(`No current profile, specify the source alias with ${text.code(`--${CopyProfile.flags.profile.name}`)}`) } ux.info(`Copying current profile ${text.code(result.alias)} from ${text.code(result.location)}`) return result @@ -85,29 +73,23 @@ export default class CopyProfile extends BaseCommand { return targetAlias } - async target(source: { alias: string }): Promise<{ url: string; alias: string }> { - if (this.flags.target) { - const { url, fsType } = this.flags.target - const alias = await this.targetAlias(source, fsType) - return { alias, url: url || await chooseFs[fsType]({ profileAlias: alias }) } - } + async target(source: { alias: string }): Promise<{ location: string; alias: string }> { + const { 'target-location': targetLocation, 'target-storage': targetStorage } = this.flags + const fsType = (targetLocation && validateFsType(targetLocation.protocol.replace(':', ''))) + ?? targetStorage + ?? await chooseFsType() - const fsType = await chooseFsType() const alias = await this.targetAlias(source, fsType) - return { alias, url: await chooseFs[fsType]({ profileAlias: alias }) } + return { alias, location: targetLocation?.toString() ?? await chooseFs[fsType]({ profileAlias: alias }) } } async run(): Promise { const profileConfig = loadProfileConfig(this.config) const source = await this.source(profileConfig) const target = await this.target(source) - await profileConfig.copy( - { location: source.location }, - { alias: target.alias, location: target.url }, - Object.keys(machineDrivers), - ) + await profileConfig.copy(source, target, Object.keys(machineDrivers)) - ux.info(text.success(`Profile ${text.code(source.alias)} copied to ${text.code(target.url)} as ${text.code(target.alias)}`)) + ux.info(text.success(`Profile ${text.code(source.alias)} copied to ${text.code(target.location)} as ${text.code(target.alias)}`)) if (this.flags.use) { await profileConfig.setCurrent(target.alias) diff --git a/packages/cli/src/commands/profile/use.ts b/packages/cli/src/commands/profile/use.ts index 60f18ef3..2ebb8b7b 100644 --- a/packages/cli/src/commands/profile/use.ts +++ b/packages/cli/src/commands/profile/use.ts @@ -1,5 +1,5 @@ import { Args, ux } from '@oclif/core' -import { BaseCommand } from '@preevy/cli-common' +import { BaseCommand, text } from '@preevy/cli-common' import { loadProfileConfig } from '../../profile-command' // eslint-disable-next-line no-use-before-define @@ -17,7 +17,7 @@ export default class UseProfile extends BaseCommand { const alias = this.args.name const profileConfig = loadProfileConfig(this.config) await profileConfig.setCurrent(alias) - ux.info(`Profile ${alias} is now being used`) + ux.info(text.success(`Profile ${text.code(alias)} is now being used`)) return undefined } }