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

Allow reading github token from path #239

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions changelog/unreleased/github-token-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Allow reading github token from path

The `--github.token` now optionally accepts a file path as an argument.
The program will read the contents of the file, and use it as the token.

https://github.com/promhippie/github_exporter/issues/238
25 changes: 23 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"fmt"
"os"
"time"

Expand Down Expand Up @@ -47,7 +48,12 @@ func Run() error {
Usage: "Print the current version of that tool",
}

return app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return err
}

return nil
}

// RootFlags defines the available root flags.
Expand Down Expand Up @@ -112,9 +118,24 @@ func RootFlags(cfg *config.Config) []cli.Flag {
&cli.StringFlag{
Name: "github.token",
Value: "",
Usage: "Access token for the GitHub API",
Usage: "Access token for the GitHub API, string or path",
EnvVars: []string{"GITHUB_EXPORTER_TOKEN"},
Destination: &cfg.Target.Token,
Action: func(ctx *cli.Context, v string) error {
if _, err := os.Stat(v); err == nil {
content, err := os.ReadFile(v)
if err != nil {
return fmt.Errorf("Error reading token file:\n%w", err)
}
cfg.Target.Token = string(content)
} else if os.IsNotExist(err) {
cfg.Target.Token = v
} else {
return fmt.Errorf("Error reading token file:\n%w", err)
}

return nil
},
},
&cli.Int64Flag{
Name: "github.app_id",
Expand Down