Skip to content

Commit

Permalink
Merge pull request #67 from allcloud-io/feature/refactor_write_to_file
Browse files Browse the repository at this point in the history
Feature/refactor write to file
  • Loading branch information
johananl authored Sep 13, 2018
2 parents 0a2f6cf + a869ce5 commit f897399
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type Credentials struct {
}

// WriteToFile writes credentials to an AWS CLI credentials file
// (https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html).
// (https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html). In addition, this
// function removes expired temporary credentials from the credentials file.
func WriteToFile(c *Credentials, filename string, section string) error {
cfg, err := ini.LooseLoad(filename)
if err != nil {
Expand All @@ -32,6 +33,7 @@ func WriteToFile(c *Credentials, filename string, section string) error {
cfg.Section(section).NewKey("aws_session_token", c.SessionToken)
cfg.Section(section).NewKey("aws_expiration", c.Expiration.UTC().Format(time.RFC3339))

// Remove expired credentials.
for _, s := range cfg.Sections() {
if s.HasKey("aws_expiration") {
v, err := s.Key("aws_expiration").TimeFormat(time.RFC3339)
Expand All @@ -40,10 +42,12 @@ func WriteToFile(c *Credentials, filename string, section string) error {
cfg.DeleteSection(s.Name())
}
} else {
log.Printf(color.YellowString("Cannot parse date (%v) in section %s: %s", s.Key("aws_expiration")), s.Name(), err)
log.Printf(color.YellowString("Cannot parse date (%v) in section %s: %s",
s.Key("aws_expiration")), s.Name(), err)
}
}
}

return cfg.SaveTo(filename)
}

Expand Down

0 comments on commit f897399

Please sign in to comment.