diff --git a/cli/export.go b/cli/export.go index f7d576060..51c4cf861 100644 --- a/cli/export.go +++ b/cli/export.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "os" "time" @@ -12,6 +13,7 @@ import ( "github.com/99designs/keyring" "github.com/alecthomas/kingpin" "github.com/aws/aws-sdk-go-v2/aws" + ini "gopkg.in/ini.v1" ) type ExportCommandInput struct { @@ -109,11 +111,11 @@ func ExportCommand(input ExportCommandInput, f *vault.ConfigFile, keyring keyrin if input.Format == FormatTypeExportJSON { return printJSON(input, credsProvider) } else if input.Format == FormatTypeExportINI { - return printINI(input, credsProvider) + return printINI(credsProvider, input.ProfileName, config.Region) } else if input.Format == FormatTypeExportEnv { - return printEnv(input, credsProvider, "export ") + return printEnv(input, credsProvider, config.Region, "export ") } else { - return printEnv(input, credsProvider, "") + return printEnv(input, credsProvider, config.Region, "") } } @@ -154,26 +156,44 @@ func printJSON(input ExportCommandInput, credsProvider aws.CredentialsProvider) return nil } -func printINI(input ExportCommandInput, credsProvider aws.CredentialsProvider) error { +func mustNewKey(s *ini.Section, name, val string) { + if val != "" { + _, err := s.NewKey(name, val) + if err != nil { + log.Fatalln("Failed to create ini key:", err.Error()) + } + } +} + +func printINI(credsProvider aws.CredentialsProvider, profilename, region string) error { creds, err := credsProvider.Retrieve(context.TODO()) if err != nil { - return fmt.Errorf("Failed to get credentials for %s: %w", input.ProfileName, err) + return fmt.Errorf("Failed to get credentials for %s: %w", profilename, err) } - fmt.Printf("[profile %s]\n", input.ProfileName) - fmt.Printf("aws_access_key_id=%s\n", creds.AccessKeyID) - fmt.Printf("aws_secret_access_key=%s\n", creds.SecretAccessKey) - - if creds.SessionToken != "" { - fmt.Printf("aws_session_token=%s\n", creds.SessionToken) + f := ini.Empty() + s, err := f.NewSection("profile " + profilename) + if err != nil { + return fmt.Errorf("Failed to create ini section: %w", err) } + + mustNewKey(s, "aws_access_key_id", creds.AccessKeyID) + mustNewKey(s, "aws_secret_access_key", creds.SecretAccessKey) + mustNewKey(s, "aws_session_token", creds.SessionToken) if creds.CanExpire { - fmt.Printf(";aws_credential_expiration=%s\n", iso8601.Format(creds.Expires)) + mustNewKey(s, "aws_credential_expiration", iso8601.Format(creds.Expires)) + } + mustNewKey(s, "region", region) + + _, err = f.WriteTo(os.Stdout) + if err != nil { + return fmt.Errorf("Failed to output ini: %w", err) } + return nil } -func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, prefix string) error { +func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, region, prefix string) error { creds, err := credsProvider.Retrieve(context.TODO()) if err != nil { return fmt.Errorf("Failed to get credentials for %s: %w", input.ProfileName, err) @@ -188,5 +208,9 @@ func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, p if creds.CanExpire { fmt.Printf("%sAWS_CREDENTIAL_EXPIRATION=%s\n", prefix, iso8601.Format(creds.Expires)) } + if region != "" { + fmt.Printf("%sAWS_REGION=%s\n", prefix, region) + } + return nil }