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

command: accept API key in environment var #512

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ go get github.com/PagerDuty/go-pagerduty@latest
### CLI

The CLI requires an [authentication token](https://v2.developer.pagerduty.com/docs/authentication), which can be specified in `.pd.yml`
file in the home directory of the user, or passed as a command-line argument.
file in the home directory of the user, set in the `PAGERDUTY_API_KEY` environment variable, or passed as a command-line argument.

Example of config file:

```yaml
Expand All @@ -30,7 +31,10 @@ authtoken: fooBar

#### Commands
`pd` command provides a single entrypoint for all the API endpoints, with individual
API represented by their own sub commands. For an exhaustive list of sub-commands, try:
API represented by their own sub commands. It requires an API token; you can provide
this in the environment variable `PAGERDUTY_API_KEY`, or via the `-authtoken` flag.

For an exhaustive list of sub-commands, try:
```
pd --help
```
Expand Down
8 changes: 7 additions & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ func (m *Meta) loadConfig() error {
if err := yaml.Unmarshal(data, other); err != nil {
return err
}
// precedence:
// 1. CLI flag
// 2. environment variable
// 3. config file
if m.Authtoken == "" {
m.Authtoken = other.Authtoken
if m.Authtoken = os.Getenv("PAGERDUTY_API_KEY"); m.Authtoken == "" {
m.Authtoken = other.Authtoken
}
}
if m.Loglevel == "" {
m.Loglevel = other.Loglevel
Expand Down