-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(wrangler): enforce single value on non-array arguments #7005
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9a0c15b
fix(wrangler): prevent users from passing multiple --env arguments
edmundhung f7a5ce7
fix(wrangler): enforce single value on non-array arguments
edmundhung 19ebe94
chore: move demandSingleValue and demandOneOfOptions to core
edmundhung b0cc38f
chore: test
edmundhung e3ed95e
fix(wrangler): ensure single value with --config arg
edmundhung 18b523b
add changeset
edmundhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: prevent users from passing multiple arguments to non array options |
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
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,48 @@ | ||
import { CommandLineArgsError } from "../errors"; | ||
|
||
/** | ||
* A helper to demand one of a set of options | ||
* via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261 | ||
*/ | ||
export function demandOneOfOption(...options: string[]) { | ||
return function (argv: { [key: string]: unknown }) { | ||
const count = options.filter((option) => argv[option]).length; | ||
const lastOption = options.pop(); | ||
|
||
if (count === 0) { | ||
throw new CommandLineArgsError( | ||
`Exactly one of the arguments ${options.join( | ||
", " | ||
)} and ${lastOption} is required` | ||
); | ||
} else if (count > 1) { | ||
throw new CommandLineArgsError( | ||
`Arguments ${options.join( | ||
", " | ||
)} and ${lastOption} are mutually exclusive` | ||
); | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
/** | ||
* A helper to ensure that an argument only receives a single value. | ||
* | ||
* This is a workaround for a limitation in yargs where non-array arguments can still receive multiple values | ||
* even though the inferred type is not an array. | ||
* | ||
* @see https://github.com/yargs/yargs/issues/1318 | ||
*/ | ||
export function demandSingleValue(key: string) { | ||
return function (argv: { [key: string]: unknown }) { | ||
if (Array.isArray(argv[key])) { | ||
throw new CommandLineArgsError( | ||
`The argument "--${key}" expects a single value, but received multiple: ${JSON.stringify(argv[key])}.` | ||
); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { defineAlias, defineCommand, defineNamespace } from "./define-command"; | ||
export { demandOneOfOption, demandSingleValue } from "./helpers"; |
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
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 |
---|---|---|
|
@@ -38,12 +38,17 @@ import { | |
import { devHandler, devOptions } from "./dev"; | ||
import { workerNamespaceCommands } from "./dispatch-namespace"; | ||
import { docsHandler, docsOptions } from "./docs"; | ||
import { JsonFriendlyFatalError, UserError } from "./errors"; | ||
import { | ||
CommandLineArgsError, | ||
JsonFriendlyFatalError, | ||
UserError, | ||
} from "./errors"; | ||
import { generateHandler, generateOptions } from "./generate"; | ||
import { hyperdrive } from "./hyperdrive/index"; | ||
import { initHandler, initOptions } from "./init"; | ||
import "./kv"; | ||
import "./workflows"; | ||
import { demandSingleValue } from "./core"; | ||
import { logBuildFailure, logger, LOGGER_LEVELS } from "./logger"; | ||
import * as metrics from "./metrics"; | ||
import { mTlsCertificateCommands } from "./mtls-certificate/cli"; | ||
|
@@ -159,35 +164,6 @@ export function getLegacyScriptName( | |
: args.name ?? config.name; | ||
} | ||
|
||
/** | ||
* A helper to demand one of a set of options | ||
* via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261 | ||
*/ | ||
export function demandOneOfOption(...options: string[]) { | ||
return function (argv: { [key: string]: unknown }) { | ||
const count = options.filter((option) => argv[option]).length; | ||
const lastOption = options.pop(); | ||
|
||
if (count === 0) { | ||
throw new CommandLineArgsError( | ||
`Exactly one of the arguments ${options.join( | ||
", " | ||
)} and ${lastOption} is required` | ||
); | ||
} else if (count > 1) { | ||
throw new CommandLineArgsError( | ||
`Arguments ${options.join( | ||
", " | ||
)} and ${lastOption} are mutually exclusive` | ||
); | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
export class CommandLineArgsError extends UserError {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved it to |
||
|
||
export function createCLIParser(argv: string[]) { | ||
const experimentalGradualRollouts = | ||
// original flag -- using internal product name (Gradual Rollouts) -- kept for temp back-compat | ||
|
@@ -230,12 +206,14 @@ export function createCLIParser(argv: string[]) { | |
type: "string", | ||
requiresArg: true, | ||
}) | ||
.check(demandSingleValue("config")) | ||
.option("env", { | ||
alias: "e", | ||
describe: "Environment to use for operations and .env files", | ||
type: "string", | ||
requiresArg: true, | ||
}) | ||
.check(demandSingleValue("env")) | ||
.option("experimental-json-config", { | ||
alias: "j", | ||
describe: `Experimental: support wrangler.json`, | ||
|
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
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
2 changes: 1 addition & 1 deletion
2
packages/wrangler/src/queues/cli/commands/consumer/http-pull/add.ts
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
2 changes: 1 addition & 1 deletion
2
packages/wrangler/src/queues/cli/commands/consumer/worker/add.ts
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff! This util is intended for us to be more opinionated than yargs 👏