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

Adds quiet mode (cache only) #772

Open
wants to merge 1 commit into
base: master
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
3 changes: 3 additions & 0 deletions cmd/aws-iam-authenticator/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ var initCmd = &cobra.Command{
}

func init() {
viper.AutomaticEnv()
viper.SetEnvPrefix("aws_iam_authenticator")

initCmd.Flags().String(
"hostname",
"localhost",
Expand Down
3 changes: 3 additions & 0 deletions cmd/aws-iam-authenticator/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Load configuration from `filename`")

rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Reduce output verbosity")
viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))

rootCmd.PersistentFlags().StringP("log-format", "l", "text", "Specify log format to use when logging to stderr [text or json]")

rootCmd.PersistentFlags().StringP(
Expand Down
9 changes: 7 additions & 2 deletions pkg/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/gofrs/flock"
"github.com/spf13/afero"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -211,11 +212,15 @@ func (f *FileCacheProvider) Retrieve() (credentials.Value, error) {
// otherwise fetching the credential from the underlying Provider and caching the results on disk
// with an expiration time.
func (f *FileCacheProvider) RetrieveWithContext(ctx context.Context) (credentials.Value, error) {
var quiet bool = viper.GetBool("quiet")

if !f.cachedCredential.Expired() && f.cachedCredential.HasKeys() {
// use the cached credential
return V2CredentialToV1Value(f.cachedCredential), nil
} else {
_, _ = fmt.Fprintf(os.Stderr, "No cached credential available. Refreshing...\n")
if !quiet {
_, _ = fmt.Fprintf(os.Stderr, "No cached credential available. Refreshing...\n")
}
// fetch the credentials from the underlying Provider
credential, err := f.provider.Retrieve(ctx)
if err != nil {
Expand Down Expand Up @@ -246,7 +251,7 @@ func (f *FileCacheProvider) RetrieveWithContext(ctx context.Context) (credential
// can't write cache, but still return the credential
_, _ = fmt.Fprintf(os.Stderr, "Unable to update credential cache %s: %v\n", f.filename, err)
err = nil
} else {
} else if !quiet {
_, _ = fmt.Fprintf(os.Stderr, "Updated cached credential\n")
}
} else {
Expand Down