Skip to content
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
44 changes: 32 additions & 12 deletions packages/cli/src/commands/config/get.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
import {hux} from '@heroku/heroku-cli-util'
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {Args} from '@oclif/core'

import {quote} from '../../lib/config/quote'

export class ConfigGet extends Command {
static usage = 'config:get KEY...'
static args = {
KEY: Args.string({description: 'key name of the config var value', required: true}),
}

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

static example = `$ heroku config:get RAILS_ENV
production`

static strict = false

static args = {
KEY: Args.string({required: true, description: 'key name of the config var value'}),
}

static flags = {
app: flags.app({required: true}),
json: flags.boolean({char: 'j', description: 'output in json format'}),
remote: flags.remote(),
shell: flags.boolean({char: 's', description: 'output config vars in shell format'}),
}

static strict = false

static usage = 'config:get KEY...'

async run() {
const {flags, argv} = await this.parse(ConfigGet)
const {argv, flags} = await this.parse(ConfigGet)
const {body: config} = await this.heroku.get<Heroku.ConfigVars>(`/apps/${flags.app}/config-vars`)
for (const key of (argv as string[])) {
const v = config[key]
if (flags.shell) {
this.log(`${key}=${quote(v || '')}`)

if (flags.json) {
const results = (argv as string[]).map(key => {
const exists = key in config
return {
key,
value: exists ? config[key] : null,
}
})

if (results.length === 1) {
hux.styledJSON(results[0])
} else {
hux.styledJSON(results)
}
} else if (flags.shell) {
for (const key of (argv as string[])) {
const v = config[key]
this.log(`${key}=${quote(v || '')}`)
}
} else {
for (const key of (argv as string[])) {
const v = config[key]
this.log(v || '')
}
}
Expand Down
48 changes: 48 additions & 0 deletions packages/cli/test/unit/commands/config/get.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,52 @@ describe('config', function () {
.it('missing', ({stdout}) => {
expect(stdout).to.equal('\n')
})

test
.nock('https://api.heroku.com', api => api
.get('/apps/myapp/config-vars')
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
)
.stdout()
.command(['config:get', '--app=myapp', '--json', 'MISSING'])
.it('--json with unset var', ({stdout}) => {
expect(JSON.parse(stdout)).to.deep.equal({key: 'MISSING', value: null})
})

test
.nock('https://api.heroku.com', api => api
.get('/apps/myapp/config-vars')
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
)
.stdout()
.command(['config:get', '--app=myapp', '--json', 'EMPTY_VAR'])
.it('--json with empty string var', ({stdout}) => {
expect(JSON.parse(stdout)).to.deep.equal({key: 'EMPTY_VAR', value: ''})
})

test
.nock('https://api.heroku.com', api => api
.get('/apps/myapp/config-vars')
.reply(200, {LANG: 'en_US.UTF-8', RACK_ENV: 'production'}),
)
.stdout()
.command(['config:get', '--app=myapp', '--json', 'RACK_ENV'])
.it('--json with normal var', ({stdout}) => {
expect(JSON.parse(stdout)).to.deep.equal({key: 'RACK_ENV', value: 'production'})
})

test
.nock('https://api.heroku.com', api => api
.get('/apps/myapp/config-vars')
.reply(200, {EMPTY_VAR: '', RACK_ENV: 'production'}),
)
.stdout()
.command(['config:get', '--app=myapp', '--json', 'MISSING', 'EMPTY_VAR', 'RACK_ENV'])
.it('--json with multiple vars', ({stdout}) => {
expect(JSON.parse(stdout)).to.deep.equal([
{key: 'MISSING', value: null},
{key: 'EMPTY_VAR', value: ''},
{key: 'RACK_ENV', value: 'production'},
])
})
})
Loading