Skip to content
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 yargs global options bypassing strict checks #1317

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,12 @@ const { jitsuEventPayload, send } = require('../src/analytics');

const aliasLegacyEnvironmentVariables = () => {
const legacyEnvironmentPrefixes = {
CML_CI: 'CML',
CML_PUBLISH: 'CML',
CML_RERUN_WORKFLOW: 'CML',
CML_SEND_COMMENT: 'CML',
CML_SEND_GITHUB_CHECK: 'CML',
CML_TENSORBOARD_DEV: 'CML',
// Remap environment variable prefixes so e.g. CML_COMMAND_OPTION becomes an
// an alias for CML_OPTION, regardless of the command it't referring to.
// See also https://github.com/yargs/yargs/issues/873#issuecomment-917441475
CML_ASSET: 'CML',
CML_CHECK: 'CML',
CML_COMMENT: 'CML',
CML_PR: 'CML',
CML_REPO: 'CML',
CML_RUNNER: 'CML',
CML_TENSORBOARD: 'CML',
CML_WORKFLOW: 'CML'
CML_CI: 'CML_REPO',
CML_PUBLISH: 'CML_ASSET',
CML_RERUN_WORKFLOW: 'CML_WORKFLOW',
CML_SEND_COMMENT: 'CML_COMMENT',
CML_SEND_GITHUB_CHECK: 'CML_CHECK',
CML_TENSORBOARD_DEV: 'CML_TENSORBOARD'
};

for (const [oldPrefix, newPrefix] of Object.entries(
Expand All @@ -41,6 +30,24 @@ const aliasLegacyEnvironmentVariables = () => {
}
}

// Remap environment variable prefixes so e.g. CML_OPTION global options become
// an alias for CML_COMMAND_OPTION, to be interpreted by the appropriate subcommands.
// See also https://github.com/yargs/yargs/issues/873#issuecomment-917441475
for (const globalOption of ['DRIVER', 'DRIVER_TOKEN', 'LOG', 'REPO', 'TOKEN'])
for (const subcommand of [
'ASSET',
'CHECK',
'COMMENT',
'PR',
'REPO',
'RUNNER',
'TENSORBOARD',
'WORKFLOW'
])
if (process.env[`CML_${globalOption}`] !== undefined)
process.env[`CML_${subcommand}_${globalOption}`] =
process.env[`CML_${globalOption}`];

const legacyEnvironmentVariables = {
TB_CREDENTIALS: 'CML_TENSORBOARD_CREDENTIALS',
DOCKER_MACHINE: 'CML_RUNNER_DOCKER_MACHINE',
Expand Down
2 changes: 1 addition & 1 deletion bin/cml/asset/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_ASSET')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/check/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_CHECK')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/comment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_COMMENT')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.builder = (yargs) =>
yargs
.commandDir('./pr', { exclude: /\.test\.js$/ })
.recommendCommands()
.env('CML')
.env('CML_PR')
.options(
Object.fromEntries(
Object.entries(options).map(([key, value]) => [
Expand Down
2 changes: 1 addition & 1 deletion bin/cml/pr/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_PR')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/repo/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_REPO')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.builder = (yargs) =>
yargs
.commandDir('./runner', { exclude: /\.test\.js$/ })
.recommendCommands()
.env('CML')
.env('CML_RUNNER')
.options(
Object.fromEntries(
Object.entries(options).map(([key, value]) => [
Expand Down
2 changes: 1 addition & 1 deletion bin/cml/runner/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_RUNNER')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/tensorboard/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_TENSORBOARD')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down
2 changes: 1 addition & 1 deletion bin/cml/workflow/rerun.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.handler = async (opts) => {

exports.builder = (yargs) =>
yargs
.env('CML')
.env('CML_WORKFLOW')
.option('options', { default: exports.options, hidden: true })
.options(exports.options);

Expand Down