Skip to content

Commit 424fc14

Browse files
feat: adding json flag to config:get command (#3464)
adding json flag and applicable tests to handle null and empty string config
1 parent f13fed6 commit 424fc14

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
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} from '@oclif/core'
45

56
import {quote} from '../../lib/config/quote'
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-
this.log(`${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 {
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]
3555
this.log(v || '')
3656
}
3757
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,52 @@ describe('config', function () {
3333
.it('missing', ({stdout}) => {
3434
expect(stdout).to.equal('\n')
3535
})
36+
37+
test
38+
.nock('https://api.heroku.com', api => api
39+
.get('/apps/myapp/config-vars')
40+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
41+
)
42+
.stdout()
43+
.command(['config:get', '--app=myapp', '--json', 'MISSING'])
44+
.it('--json with unset var', ({stdout}) => {
45+
expect(JSON.parse(stdout)).to.deep.equal({key: 'MISSING', value: null})
46+
})
47+
48+
test
49+
.nock('https://api.heroku.com', api => api
50+
.get('/apps/myapp/config-vars')
51+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
52+
)
53+
.stdout()
54+
.command(['config:get', '--app=myapp', '--json', 'EMPTY_VAR'])
55+
.it('--json with empty string var', ({stdout}) => {
56+
expect(JSON.parse(stdout)).to.deep.equal({key: 'EMPTY_VAR', value: ''})
57+
})
58+
59+
test
60+
.nock('https://api.heroku.com', api => api
61+
.get('/apps/myapp/config-vars')
62+
.reply(200, {LANG: 'en_US.UTF-8', RACK_ENV: 'production'}),
63+
)
64+
.stdout()
65+
.command(['config:get', '--app=myapp', '--json', 'RACK_ENV'])
66+
.it('--json with normal var', ({stdout}) => {
67+
expect(JSON.parse(stdout)).to.deep.equal({key: 'RACK_ENV', value: 'production'})
68+
})
69+
70+
test
71+
.nock('https://api.heroku.com', api => api
72+
.get('/apps/myapp/config-vars')
73+
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
74+
)
75+
.stdout()
76+
.command(['config:get', '--app=myapp', '--json', 'MISSING', 'EMPTY_VAR', 'RACK_ENV'])
77+
.it('--json with multiple vars', ({stdout}) => {
78+
expect(JSON.parse(stdout)).to.deep.equal([
79+
{key: 'MISSING', value: null},
80+
{key: 'EMPTY_VAR', value: ''},
81+
{key: 'RACK_ENV', value: 'production'},
82+
])
83+
})
3684
})

0 commit comments

Comments
 (0)