Skip to content

Commit

Permalink
Pipeline support (#26)
Browse files Browse the repository at this point in the history
* bump mock-fs vesrion

* add experimental support for pipelines

* fix app flag

* refactor, add push support

* fix bug

* fix bug, update header & README
  • Loading branch information
xavdid authored Feb 13, 2022
1 parent e2f09e7 commit 42daaa7
Show file tree
Hide file tree
Showing 8 changed files with 2,308 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jspm_packages
# Optional REPL history
.node_repl_history
.vscode
yarn.lock

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ You can install the package by running

This package includes two commands:

* `heroku config:pull`: Writes the contents of `heroku config` into a local file
* `heroku config:push`: Writes the contents of a local file into `heroku config`
- `heroku config:pull`: Writes the contents of `heroku config` into a local file
- `heroku config:push`: Writes the contents of a local file into `heroku config`

As of version 1.6.0, the `heroku-config` supports modifying pipeline config variables with the `--pipelie-name` and `--pipeline-stage` flags.

Run `heroku help config:pull` and `heroku help config:push` to see a full list of flags.

Expand Down
19 changes: 15 additions & 4 deletions commands/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ const _ = require('lodash')

const merge = require('../util/merge')
const file = require('../util/file')
const { shared: sharedFlags, pipelineFlagsAreValid, buildPullUrl } = require('../util/flags')

// eslint-disable-next-line generator-star-spacing, space-before-function-paren
function* pull(context, heroku) {
let fname = context.flags.file // this gets defaulted in read

if (!pipelineFlagsAreValid(context.flags)){
cli.exit(1, 'If you specify either `pipeline-name` or `pipeline-stage`, specify them both.')
}

const pullUrl = yield buildPullUrl(context, heroku, cli)

let config = yield {
remote: heroku.get(`/apps/${context.app}/config-vars`),
remote: heroku.get(pullUrl),
local: file.read(fname, context.flags)
}
let res = merge(config.remote, config.local, context.flags)
Expand All @@ -33,7 +41,7 @@ function* pull(context, heroku) {

module.exports = (() => {
let flags = [
...require('../util/flags'),
...sharedFlags,
{
name: 'unquoted',
char: 'u',
Expand All @@ -47,9 +55,12 @@ module.exports = (() => {
description: 'pull env variables from heroku',
help:
'Write remote config vars into file FILE, favoring existing local configs in case of collision',
needsApp: true,
// doesn't seem to be documented anywhere, but this works
// no clue where it plugs into the CLI now, but I found it here:
// https://github.com/heroku/cli-engine/blob/5004250bd03c0b38e6e33e69ee962a3b50274b20/src/plugins/legacy.ts#L169
wantsApp: true,
needsAuth: true,
run: cli.command(co.wrap(pull)),
flags: flags
flags
}
})()
21 changes: 15 additions & 6 deletions commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const co = require('co')
const merge = require('../util/merge')
const file = require('../util/file')
const _ = require('lodash')
const { shared: sharedFlags, pipelineFlagsAreValid, buildPullUrl } = require('../util/flags')

// eslint-disable-next-line generator-star-spacing, space-before-function-paren
function* patchConfig(context, heroku, payload, success) {
function* patchConfig(context, heroku, payload, success, url) {
try {
yield heroku.patch(`/apps/${context.app}/config-vars`, { body: payload })
yield heroku.patch(url, { body: payload })
if (!context.flags.quiet) {
cli.log(success)
}
Expand All @@ -21,8 +22,15 @@ function* patchConfig(context, heroku, payload, success) {
// eslint-disable-next-line generator-star-spacing, space-before-function-paren
function* push(context, heroku) {
let fname = context.flags.file // this gets defaulted in read

if (!pipelineFlagsAreValid(context.flags)){
cli.exit(1, 'If you specify either `pipeline-name` or `pipeline-stage`, specify them both.')
}

const pullUrl = yield buildPullUrl(context, heroku, cli)

let config = yield {
remote: heroku.get(`/apps/${context.app}/config-vars`),
remote: heroku.get(pullUrl),
local: file.read(fname, context.flags)
}

Expand All @@ -31,7 +39,8 @@ function* push(context, heroku) {
context,
heroku,
res,
'Successfully wrote settings to Heroku!'
'Successfully wrote settings to Heroku!',
pullUrl
)

if (context.flags.clean) {
Expand All @@ -53,7 +62,7 @@ function* push(context, heroku) {

module.exports = (() => {
let flags = [
...require('../util/flags'),
...sharedFlags,
{
name: 'clean',
char: 'c',
Expand All @@ -71,6 +80,6 @@ module.exports = (() => {
needsApp: true,
needsAuth: true,
run: cli.command(co.wrap(push)),
flags: flags
flags
}
})()
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "heroku-config",
"version": "1.5.4",
"version": "1.6.0",
"description": "Push and pull environment variables from a Heroku instance",
"main": "index.js",
"scripts": {
"test": "mocha && standard",
"test": "mocha",
"deploy": "npx np"
},
"repository": {
Expand All @@ -23,14 +23,14 @@
"dependencies": {
"fs-extra": "8.1.0",
"heroku-cli-util": "6.2.12",
"lodash": "4.17.15"
"lodash": "4.17.21"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"js-yaml": "^3.6.1",
"mocha": "^5.1.1",
"mock-fs": "^4.0.0",
"mock-fs": "5.1.2",
"mock-stdin": "^0.3.0",
"nock": "^9.0.0",
"rewire": "^2.5.1",
Expand Down
2 changes: 1 addition & 1 deletion util/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const cli = require('heroku-cli-util')
const HOME_DIR = require('os').homedir()

const DEFAULT_FNAME = '.env'
const header = '# this file was created automatically by heroku-config\n\n'
const header = '# this file was created automatically by https://github.com/xavdid/heroku-config/\n\n'

const objToFileFormat = (obj, flags = {}) => {
let res = `${header}`
Expand Down
83 changes: 66 additions & 17 deletions util/flags.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,70 @@
// command line args

module.exports = [
{
name: 'file',
char: 'f',
hasValue: true,
description: 'specify target filename'
module.exports = {
shared: [
{
name: 'file',
char: 'f',
hasValue: true,
description: 'specify target filename',
},
{
name: 'overwrite',
char: 'o',
description: 'overwrite destination config vars',
},
{ name: 'quiet', char: 'q', description: 'supress all non-error output' },
{
name: 'expanded',
char: 'e',
description: 'allow non-standard characters in variable names',
},
{
name: 'pipeline-name',
description:
'pull or push the configuration of a specific pipeline. If provided, must also specify pipeline-stage',
hasValue: true,
},
{
name: 'pipeline-stage',
description:
'pull or push the configuration of a specific pipeline. If provided, must also specify pipeline-name',
hasValue: true,
},
],
pipelineFlagsAreValid: (flags) => {
const pipelineName = flags['pipeline-name']
const pipelineStage = flags['pipeline-stage']

if ((pipelineName || pipelineStage) && !(pipelineName && pipelineStage)) {
return false
}
return true
},
{
name: 'overwrite',
char: 'o',
description: 'overwrite destination config vars'
buildPullUrl: async (context, heroku, cli) => {
const pipelineName = context.flags['pipeline-name']
const pipelineStage = context.flags['pipeline-stage']

let pullUrl

if (pipelineName) {
let pipelineData
try {
pipelineData = await heroku.get(`/pipelines/${pipelineName}`)
} catch (err) {
cli.error('problem fetching pipeline info')
cli.exit(1, String(err))
}
pullUrl = `/pipelines/${pipelineData.id}/stage/${pipelineStage}/config-vars`
} else {
if (!context.app) {
cli.exit(
1,
'Must specify `--app` parameter, or `--pipeline-name` and `--pipeline-stage`'
)
}
pullUrl = `/apps/${context.app}/config-vars`
}
return pullUrl
},
{ name: 'quiet', char: 'q', description: 'supress all non-error output' },
{
name: 'expanded',
char: 'e',
description: 'allow non-standard characters in variable names'
}
]
}
Loading

0 comments on commit 42daaa7

Please sign in to comment.