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

feat: deprecate scopedEnvVarKey for scopedEnvVarKeys which accounts for binAliases #751

Merged
merged 3 commits into from
Jul 28, 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
2 changes: 1 addition & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export abstract class Command {
let result
try {
// remove redirected env var to allow subsessions to run autoupdated client
delete process.env[this.config.scopedEnvVarKey('REDIRECTED')]
this.config.scopedEnvVarKeys('REDIRECTED').map(key => delete process.env[key])
await this.init()
result = await this.run()
} catch (error: any) {
Expand Down
19 changes: 17 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,36 @@ export class Config implements IConfig {
}

public scopedEnvVar(k: string): string | undefined {
return process.env[this.scopedEnvVarKey(k)]
return process.env[this.scopedEnvVarKeys(k).find(k => process.env[k]) as string]
}

public scopedEnvVarTrue(k: string): boolean {
const v = process.env[this.scopedEnvVarKey(k)]
const v = process.env[this.scopedEnvVarKeys(k).find(k => process.env[k]) as string]
return v === '1' || v === 'true'
}

/**
* this DOES NOT account for bin aliases, use scopedEnvVarKeys instead which will account for bin aliases
* @param {string} k, the unscoped key you want to get the value for
* @returns {string} returns the env var key
*/
public scopedEnvVarKey(k: string): string {
return [this.bin, k]
.map(p => p.replace(/@/g, '').replace(/[/-]/g, '_'))
.join('_')
.toUpperCase()
}

/**
* gets the scoped env var keys for a given key, including bin aliases
* @param {string} k, the env key e.g. 'debug'
* @returns {string[]} e.g. ['SF_DEBUG', 'SFDX_DEBUG']
*/
public scopedEnvVarKeys(k: string): string[] {
return [this.bin, ...this.binAliases ?? []].map(alias =>
[alias.replace(/@/g, '').replace(/[/-]/g, '_'), k].join('_').toUpperCase())
}

public findCommand(id: string, opts: { must: true }): Command.Loadable

public findCommand(id: string, opts?: { must: boolean }): Command.Loadable | undefined
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export interface Config {
findMatches(id: string, argv: string[]): Command.Loadable[];
scopedEnvVar(key: string): string | undefined;
scopedEnvVarKey(key: string): string;
scopedEnvVarKeys(key: string): string[];
scopedEnvVarTrue(key: string): boolean;
s3Url(key: string): string;
s3Key(type: 'versioned' | 'unversioned', ext: '.tar.gz' | '.tar.xz', options?: Config.s3Key.Options): string;
Expand Down
52 changes: 50 additions & 2 deletions test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pjson = {
files: [],
commands: {},
oclif: {
binAliases: ['foo', 'bar'],
binAliases: ['bar', 'baz'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bin is foo, changed to remove confusing assertions

topics: {
t1: {
description: 'desc for t1',
Expand Down Expand Up @@ -103,7 +103,55 @@ describe('Config', () => {
describe('binAliases', () => {
testConfig({pjson})
.it('will have binAliases set', config => {
expect(config.binAliases).to.deep.equal(['foo', 'bar'])
expect(config.binAliases).to.deep.equal(['bar', 'baz'])
})

testConfig({pjson}).it('will get scoped env vars with bin aliases', config => {
expect(config.scopedEnvVarKeys('abc')).to.deep.equal(['FOO_ABC', 'BAR_ABC', 'BAZ_ABC'])
})

testConfig({pjson}).it('will get scoped env vars', config => {
expect(config.scopedEnvVarKey('abc')).to.equal('FOO_ABC')
})

testConfig({pjson}).it('will get scopedEnvVar', config => {
process.env.FOO_ABC = 'find me'
expect(config.scopedEnvVar('abc')).to.deep.equal('find me')
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVar via alias', config => {
process.env.BAZ_ABC = 'find me'
expect(config.scopedEnvVar('abc')).to.deep.equal('find me')
delete process.env.BAZ_ABC
})

testConfig({pjson}).it('will get scoped env vars', config => {
expect(config.scopedEnvVarKey('abc')).to.equal('FOO_ABC')
})

testConfig({pjson}).it('will get scopedEnvVarTrue', config => {
process.env.FOO_ABC = 'true'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue via alias', config => {
process.env.BAR_ABC = 'true'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.BAR_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue=1', config => {
process.env.FOO_ABC = '1'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue=1 via alias', config => {
process.env.BAR_ABC = '1'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.BAR_ABC
})
})

Expand Down