Skip to content

Commit 1008260

Browse files
chore: merge main into v11 (#3466)
* chore(deps): bump qs from 6.14.0 to 6.14.1 (#3444) Bumps [qs](https://github.com/ljharb/qs) from 6.14.0 to 6.14.1. - [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md) - [Commits](ljharb/qs@v6.14.0...v6.14.1) --- updated-dependencies: - dependency-name: qs dependency-version: 6.14.1 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: adding json flag to config:get command (#3464) adding json flag and applicable tests to handle null and empty string config --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent c60ac27 commit 1008260

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/src/commands/config/get.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
1+
import {hux} from '@heroku/heroku-cli-util'
12
import {Command, flags} from '@heroku-cli/command'
23
import * as Heroku from '@heroku-cli/schema'
34
import {Args, ux} from '@oclif/core'
45

56
import {quote} from '../../lib/config/quote.js'
67

78
export class ConfigGet extends Command {
8-
static usage = 'config:get KEY...'
9+
static args = {
10+
KEY: Args.string({description: 'key name of the config var value', required: true}),
11+
}
912

1013
static description = 'display a single config value for an app'
1114

1215
static example = `$ heroku config:get RAILS_ENV
1316
production`
1417

15-
static strict = false
16-
17-
static args = {
18-
KEY: Args.string({required: true, description: 'key name of the config var value'}),
19-
}
20-
2118
static flags = {
2219
app: flags.app({required: true}),
20+
json: flags.boolean({char: 'j', description: 'output in json format'}),
2321
remote: flags.remote(),
2422
shell: flags.boolean({char: 's', description: 'output config vars in shell format'}),
2523
}
2624

25+
static strict = false
26+
27+
static usage = 'config:get KEY...'
28+
2729
async run() {
28-
const {flags, argv} = await this.parse(ConfigGet)
30+
const {argv, flags} = await this.parse(ConfigGet)
2931
const {body: config} = await this.heroku.get<Heroku.ConfigVars>(`/apps/${flags.app}/config-vars`)
30-
for (const key of (argv as string[])) {
31-
const v = config[key]
32-
if (flags.shell) {
33-
ux.stdout(`${key}=${quote(v || '')}`)
32+
33+
if (flags.json) {
34+
const results = (argv as string[]).map(key => {
35+
const exists = key in config
36+
return {
37+
key,
38+
value: exists ? config[key] : null,
39+
}
40+
})
41+
42+
if (results.length === 1) {
43+
hux.styledJSON(results[0])
3444
} else {
35-
ux.stdout(v || '')
45+
hux.styledJSON(results)
46+
}
47+
} else if (flags.shell) {
48+
for (const key of (argv as string[])) {
49+
const v = config[key]
50+
this.log(`${key}=${quote(v || '')}`)
51+
}
52+
} else {
53+
for (const key of (argv as string[])) {
54+
const v = config[key]
55+
this.log(v || '')
3656
}
3757
}
3858
}

packages/cli/test/unit/commands/config/get.unit.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,48 @@ describe('config', function () {
4343

4444
expect(stdout).to.equal('\n')
4545
})
46+
47+
it('--json with unset var', async function () {
48+
api
49+
.get('/apps/myapp/config-vars')
50+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'})
51+
52+
const {stdout} = await runCommand(['config:get', '--app=myapp', '--json', 'MISSING'])
53+
54+
expect(JSON.parse(stdout)).to.deep.equal({key: 'MISSING', value: null})
55+
})
56+
57+
it('--json with empty string var', async function () {
58+
api
59+
.get('/apps/myapp/config-vars')
60+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'})
61+
62+
const {stdout} = await runCommand(['config:get', '--app=myapp', '--json', 'EMPTY_VAR'])
63+
64+
expect(JSON.parse(stdout)).to.deep.equal({key: 'EMPTY_VAR', value: ''})
65+
})
66+
67+
it('--json with normal var', async function () {
68+
api
69+
.get('/apps/myapp/config-vars')
70+
.reply(200, {LANG: 'en_US.UTF-8', RACK_ENV: 'production'})
71+
72+
const {stdout} = await runCommand(['config:get', '--app=myapp', '--json', 'RACK_ENV'])
73+
74+
expect(JSON.parse(stdout)).to.deep.equal({key: 'RACK_ENV', value: 'production'})
75+
})
76+
77+
it('--json with multiple vars', async function () {
78+
api
79+
.get('/apps/myapp/config-vars')
80+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'})
81+
82+
const {stdout} = await runCommand(['config:get', '--app=myapp', '--json', 'MISSING', 'EMPTY_VAR', 'RACK_ENV'])
83+
84+
expect(JSON.parse(stdout)).to.deep.equal([
85+
{key: 'MISSING', value: null},
86+
{key: 'EMPTY_VAR', value: ''},
87+
{key: 'RACK_ENV', value: 'production'},
88+
])
89+
})
4690
})

0 commit comments

Comments
 (0)