Skip to content

Commit

Permalink
fix multiple flags bug
Browse files Browse the repository at this point in the history
multiple flags such as `-f` were not parsed correctly due to a bug or misfeature in oclif.
- update oclif to the latest version with the minimal required changes
- create a patch from oclif/core#880 to fix oclif's behavior
  • Loading branch information
Roy Razon committed Nov 21, 2023
1 parent f573a78 commit 6dae17c
Show file tree
Hide file tree
Showing 22 changed files with 469 additions and 193 deletions.
2 changes: 1 addition & 1 deletion packages/cli-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"license": "Apache-2.0",
"dependencies": {
"@oclif/core": "^2",
"@oclif/core": "^3",
"@preevy/core": "0.0.56",
"chalk": "^4.1.2",
"iter-tools-es": "^7.5.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-common/src/lib/common-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const composeFlags = {
file: Flags.string({
description: 'Compose configuration file',
multiple: true,
singleValue: true,
required: false,
char: 'f',
default: [],
Expand All @@ -20,6 +21,7 @@ export const composeFlags = {
'system-compose-file': Flags.string({
description: 'Add extra Compose configuration file without overriding the defaults',
multiple: true,
singleValue: true,
required: false,
default: [],
helpGroup: 'GLOBAL',
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"dependencies": {
"@inquirer/confirm": "^2.0.14",
"@oclif/core": "^2",
"@oclif/core": "^3",
"@oclif/plugin-help": "^5",
"@preevy/cli-common": "0.0.56",
"@preevy/common": "0.0.56",
Expand All @@ -31,14 +31,15 @@
"@preevy/driver-lightsail": "0.0.56",
"@preevy/plugin-github-pr-link": "0.0.56",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^2.0.0",
"iter-tools-es": "^7.5.3",
"lodash": "^4.17.21",
"shell-escape": "^0.2.0",
"yaml": "^2.3.2"
},
"devDependencies": {
"@oclif/test": "^2.3.4",
"@types/inquirer": "^8.0.0",
"@types/inquirer-autocomplete-prompt": "^3.0.3",
"@types/lodash": "^4.14.192",
"@types/node": "18",
"@types/shell-escape": "^0.2.1",
Expand All @@ -53,7 +54,7 @@
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.32.2",
"jest": "29.7.0",
"oclif": "^3",
"oclif": "^4.0.4",
"pkg": "^5.8.1",
"shx": "^0.3.3",
"ts-node": "^10.9.1",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/profile/config/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class UpdateProfileConfig extends ProfileCommand<typeof UpdatePro
description: 'Unset a configuration option',
default: [],
multiple: true,
singleValue: true,
}),
}

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class Purge extends DriverCommand<typeof Purge> {
description: 'Resource type(s) to delete',
default: [machineResourceType],
multiple: true,
singleValue: true,
}),
force: Flags.boolean({
description: 'Do not ask for confirmation',
Expand Down
17 changes: 12 additions & 5 deletions packages/cli/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { fsTypeFromUrl, localFsFromUrl } from '@preevy/core'
import { googleCloudStorageFs, defaultBucketName as gsDefaultBucketName, defaultProjectId as defaultGceProjectId } from '@preevy/driver-gce'
import { s3fs, defaultBucketName as s3DefaultBucketName, AWS_REGIONS, awsUtils } from '@preevy/driver-lightsail'
import { s3fs, defaultBucketName as s3DefaultBucketName, awsUtils, S3_REGIONS } from '@preevy/driver-lightsail'
import inquirer from 'inquirer'
import inquirerAutoComplete from 'inquirer-autocomplete-prompt'
import { DriverName } from './drivers'
import ambientAwsAccountId = awsUtils.ambientAccountId

inquirer.registerPrompt('autocomplete', inquirerAutoComplete)

export const fsFromUrl = async (url: string, localBaseDir: string) => {
const fsType = fsTypeFromUrl(url)
if (fsType === 'local') {
Expand Down Expand Up @@ -57,12 +60,16 @@ export const chooseFs: Record<FsType, FsChooser> = {
// eslint-disable-next-line no-use-before-define
const { region, bucket } = await inquirer.prompt<{ region: string; bucket: string }>([
{
type: 'list',
type: 'autocomplete',
name: 'region',
message: 'S3 bucket region',
choices: AWS_REGIONS,
default: driver?.name === 'lightsail' ? driver.flags.region as string : 'us-east-1',
},
source: async (_opts, input) => S3_REGIONS.filter(r => !input || r.includes(input.toLowerCase())),
default: driver?.name === 'lightsail' && S3_REGIONS.includes(driver.flags.region as string)
? driver.flags.region as string
: 'us-east-1',
suggestOnly: true,
filter: i => i.toLowerCase(),
} as inquirerAutoComplete.AutocompleteQuestionOptions,
{
type: 'input',
name: 'bucket',
Expand Down
19 changes: 4 additions & 15 deletions packages/cli/src/help.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandHelp as BaseCommandHelp, Help as OclifHelp } from '@oclif/core'
import { CommandHelp as BaseCommandHelp, Command, Help as OclifHelp } from '@oclif/core'
import { HelpOptions, Config, Topic } from '@oclif/core/lib/interfaces'
import { BaseCommand, text } from '@preevy/cli-common'

class GlobalFlagsHelp extends BaseCommandHelp {
constructor(config: Config, opts: HelpOptions) {
super(BaseCommand, config, opts)
constructor(command: Command.Loadable, config: Config, opts: HelpOptions) {
super(command, config, opts)
}

globalFlags() {
Expand All @@ -13,20 +13,9 @@ class GlobalFlagsHelp extends BaseCommandHelp {
}
}

class CommandHelp extends BaseCommandHelp {
constructor(...args: ConstructorParameters<typeof BaseCommandHelp>) {
super(...args)
}
}

export default class Help extends OclifHelp {
constructor(...args: ConstructorParameters<typeof OclifHelp>) {
super(...args)
this.CommandHelpClass = CommandHelp
}

protected formatGlobalFlags(): string {
return this.section('GLOBAL FLAGS', new GlobalFlagsHelp(this.config, this.opts).globalFlags())
return this.section('GLOBAL FLAGS', new GlobalFlagsHelp(this.config.commands[0], this.config, this.opts).globalFlags())
}

override async showRootHelp(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"@preevy/compose-tunnel-agent": "0.0.56",
"chalk": "^4.1.2",
"fast-safe-stringify": "^2.1.1",
"inquirer": "^8.0.0",
"is-stream": "^2.0.1",
"iter-tools-es": "^7.5.3",
"jose": "^4.14.4",
Expand Down Expand Up @@ -41,6 +40,7 @@
"devDependencies": {
"@jest/globals": "29.7.0",
"@types/inquirer": "^8.0.0",
"@types/inquirer-autocomplete-prompt": "^3.0.3",
"@types/is-stream": "^2.0.0",
"@types/lodash": "^4.14.192",
"@types/node": "18",
Expand Down
5 changes: 4 additions & 1 deletion packages/driver-azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
"@azure/arm-subscriptions": "^5.1.0",
"@azure/identity": "^3.2.2",
"@azure/logger": "^1.0.4",
"@oclif/core": "^2",
"@oclif/core": "^3",
"@preevy/core": "0.0.56",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^2.0.0",
"iter-tools-es": "^7.5.3",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/azure": "^0.9.20",
"@types/inquirer": "^8.0.0",
"@types/inquirer-autocomplete-prompt": "^3.0.3",
"@types/lodash": "^4.14.192",
"@types/node": "18",
"@typescript-eslint/eslint-plugin": "6.7.4",
Expand Down
1 change: 1 addition & 0 deletions packages/driver-azure/src/driver/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const REGIONS = [
'centraluseuap',
'eastus2euap',
'qatarcentral',
'israelcentral',
]

type VMInstance = {
Expand Down
15 changes: 10 additions & 5 deletions packages/driver-azure/src/driver/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flags, Interfaces } from '@oclif/core'
import { asyncFirst, asyncMap } from 'iter-tools-es'
import { ListQuestion, Question } from 'inquirer'
import inquirer, { Question } from 'inquirer'
import inquirerAutoComplete from 'inquirer-autocomplete-prompt'
import { InferredFlags } from '@oclif/core/lib/interfaces'
import { Resource, VirtualMachine } from '@azure/arm-compute'
import { inspect } from 'util'
Expand All @@ -26,6 +27,8 @@ import { Client, client as createClient, REGIONS } from './client'
import { CUSTOMIZE_BARE_MACHINE } from './scripts'
import { AzureCustomTags, extractResourceGroupNameFromId } from './vm-creation-utils'

inquirer.registerPrompt('autocomplete', inquirerAutoComplete)

type RootObjectDetailsError = {
code: string
message: string
Expand Down Expand Up @@ -142,13 +145,15 @@ const flags = {

type FlagTypes = Omit<Interfaces.InferredFlags<typeof flags>, 'json'>

const questions = async (): Promise<(Question | ListQuestion)[]> => [
const questions = async (): Promise<Question[]> => [
{
type: 'list',
type: 'autocomplete',
name: 'region',
message: flags.region.description,
choices: REGIONS,
},
source: async (_opts, input) => !input || REGIONS.filter(r => r.includes(input.toLowerCase())),
suggestOnly: true,
filter: i => i.toLowerCase(),
} as inquirerAutoComplete.AutocompleteQuestionOptions,
{
type: 'input',
name: 'subscription-id',
Expand Down
5 changes: 4 additions & 1 deletion packages/driver-gce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
"dependencies": {
"@google-cloud/compute": "^4.0.1",
"@google-cloud/storage": "^6.9.5",
"@oclif/core": "^2",
"@oclif/core": "^3",
"@preevy/core": "0.0.56",
"google-auth-library": "^8.7.0",
"google-gax": "^4.0.4",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^2.0.0",
"iter-tools-es": "^7.5.3",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/inquirer": "^8.0.0",
"@types/inquirer-autocomplete-prompt": "^3.0.3",
"@types/lodash": "^4.14.192",
"@types/node": "18",
"@typescript-eslint/eslint-plugin": "6.7.4",
Expand Down
53 changes: 31 additions & 22 deletions packages/driver-gce/src/driver/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flags, Interfaces } from '@oclif/core'
import { asyncMap } from 'iter-tools-es'
import { InputQuestion, ListQuestion } from 'inquirer'
import inquirer, { ListQuestion, Question } from 'inquirer'
import inquirerAutoComplete from 'inquirer-autocomplete-prompt'
import {
MachineDriver,
SshMachine, MachineCreationDriver, MachineCreationDriverFactory, MachineDriverFactory,
Expand All @@ -15,11 +16,13 @@ import {
extractDefined,
PartialMachine,
} from '@preevy/core'
import { pick } from 'lodash'
import { memoize, pick } from 'lodash'
import createClient, { Client, Instance, availableRegions, defaultProjectId, instanceError, shortResourceName } from './client'
import { deserializeMetadata, metadataKey } from './metadata'
import { LABELS } from './labels'

inquirer.registerPrompt('autocomplete', inquirerAutoComplete)

type DriverContext = {
log: Logger
debug: boolean
Expand Down Expand Up @@ -108,26 +111,32 @@ const contextFromFlags = ({
zone,
})

const questions = async (): Promise<(InputQuestion | ListQuestion)[]> => [
{
type: 'input',
name: 'project',
default: defaultProjectId,
message: flags['project-id'].description,
},
{
type: 'list',
name: 'region',
choices: async ({ project }) => (await availableRegions(project)).map(r => r.name),
},
{
type: 'list',
name: 'zone',
choices: async (
{ project, region },
) => (await availableRegions(project)).find(r => r.name === region)?.zones ?? [],
},
]
const questions = async (): Promise<Question[]> => {
const memoizedAvailableRegions = memoize(availableRegions)
return [
{
type: 'input',
name: 'project',
default: defaultProjectId,
message: flags['project-id'].description,
},
{
type: 'autocomplete',
name: 'region',
source: async ({ project }, input) => (await memoizedAvailableRegions(project))
.filter(({ name }) => !input || name.includes(input.toLowerCase()))
.map(r => r.name),
filter: i => i.toLowerCase(),
} as inquirerAutoComplete.AutocompleteQuestionOptions,
{
type: 'list',
name: 'zone',
choices: memoize(
async ({ project, region }) => (await availableRegions(project)).find(r => r.name === region)?.zones ?? [],
),
} as ListQuestion,
]
}

const flagsFromAnswers = async (answers: Record<string, unknown>): Promise<FlagTypes> => ({
'project-id': answers.project as string,
Expand Down
2 changes: 1 addition & 1 deletion packages/driver-kube-pod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "Apache-2.0",
"dependencies": {
"@kubernetes/client-node": "^0.18.1",
"@oclif/core": "^2",
"@oclif/core": "^3",
"@preevy/common": "0.0.56",
"@preevy/core": "0.0.56",
"fast-safe-stringify": "^2.1.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/driver-lightsail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
"@aws-sdk/client-s3": "^3.271.0",
"@aws-sdk/client-sts": "^3.289.0",
"@aws-sdk/util-waiter": "^3.271.0",
"@oclif/core": "^2",
"@oclif/core": "^3",
"@preevy/core": "0.0.56",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^2.0.0",
"iter-tools-es": "^7.5.3",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/inquirer": "^8.0.0",
"@types/inquirer-autocomplete-prompt": "^3.0.3",
"@types/lodash": "^4.14.192",
"@types/node": "18",
"@typescript-eslint/eslint-plugin": "6.7.4",
Expand Down
Loading

0 comments on commit 6dae17c

Please sign in to comment.