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(command-env): add support for plaintext output in env:list #5322

Merged
merged 3 commits into from
Dec 21, 2022
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: 2 additions & 0 deletions docs/commands/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ netlify env:list
**Flags**

- `context` (*string*) - Specify a deploy context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev")
- `plain` (*boolean*) - Output environment variables as plaintext
Copy link
Contributor

Choose a reason for hiding this comment

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

Could new entries to the help be put below scope? Would be nice to keep context and scope next to each other. Thanks!

Copy link
Contributor Author

@tinfoil-knight tinfoil-knight Dec 20, 2022

Choose a reason for hiding this comment

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

The plain flag in between seemed weird to me too but the options are sorted here to produced the documentation:

export const sortOptions = (optionA, optionB) => {
// base flags should be always at the bottom
if (BASE_FLAGS.has(optionA.long) || BASE_FLAGS.has(optionB.long)) {
return -1
}
return optionA.long.localeCompare(optionB.long)
}

Although, we can rename the option to text so it appear after scope.


Other alternatives like raw, plaintext would appear in between context & scope in the sorted order.

Let me know if you've a better suggestion or if we should go ahead with text.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha -- let's keep it as plain, and let the docs sort them as they currently do. But in --help, it would be great if context and scope could appear next to each other.

Copy link
Contributor Author

@tinfoil-knight tinfoil-knight Dec 20, 2022

Choose a reason for hiding this comment

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

Unfortunately, it seems like we're using the same function to order the help output too so I don't think the re-ordering would be possible unless we rename the option.

The current order does appear a bit weird to me too but I don't think there's any way to override this specific instance.

Ref:

const optionList = helper
.visibleOptions(command)
.sort(sortOptions)
.map((option) => formatItem(helper.optionTerm(option), helper.optionDescription(option)))
if (optionList.length !== 0) {
output = [...output, chalk.bold('OPTIONS'), formatHelpList(optionList), '']
}
}

- `scope` (*builds | functions | post-processing | runtime | any*) - Specify a scope
- `debug` (*boolean*) - Print debugging information
- `httpProxy` (*string*) - Proxy server address to route requests through.
Expand All @@ -147,6 +148,7 @@ netlify env:list # list variables with values in the dev context and with any sc
netlify env:list --context production
netlify env:list --context branch:staging
netlify env:list --scope functions
netlify env:list --plain
```

---
Expand Down
10 changes: 10 additions & 0 deletions src/commands/env/env-list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const envList = async (options, command) => {
return false
}

if (options.plain) {
const plaintext = Object.entries(environment)
.map(([key, variable]) => `${key}=${variable.value}`)
.join('\n')
log(plaintext)
return false
}

const forSite = `for site ${chalk.green(siteInfo.name)}`
const contextType = AVAILABLE_CONTEXTS.includes(context) ? 'context' : 'branch'
const withContext = isUsingEnvelope ? `in the ${chalk.magenta(options.context)} ${contextType}` : ''
Expand Down Expand Up @@ -126,6 +134,7 @@ export const createEnvListCommand = (program) =>
normalizeContext,
'dev',
)
.addOption(new Option('--plain', 'Output environment variables as plaintext').conflicts('json'))
.addOption(
new Option('-s, --scope <scope>', 'Specify a scope')
.choices(['builds', 'functions', 'post-processing', 'runtime', 'any'])
Expand All @@ -136,6 +145,7 @@ export const createEnvListCommand = (program) =>
'netlify env:list --context production',
'netlify env:list --context branch:staging',
'netlify env:list --scope functions',
'netlify env:list --plain',
])
.description('Lists resolved environment variables for site (includes netlify.toml)')
.action(async (options, command) => {
Expand Down