Skip to content

Commit

Permalink
Merge pull request #60 from RustyF/powershell-export
Browse files Browse the repository at this point in the history
export-ps - a PowerShell variation of export
  • Loading branch information
jaxxstorm authored Mar 26, 2024
2 parents 2e5c713 + 039cc64 commit 3a517fc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ export AWS_SECRET_ACCESS_KEY=<SECRET_KEY>
export AWS_SESSION_TOKEN=<SESSION_TOKEN>
```

#### PowerShell more your thing?

If you're using PowerShell, you can use `export-ps` to generate PowerShell assignments, instead.

```powershell
> aws-sso-creds export-ps
$env:AWS_ACCESS_KEY_ID='<KEY>'
$env:AWS_SECRET_ACCESS_KEY='<SECRET_KEY>'
$env:AWS_SESSION_TOKEN='<SESSION_TOKEN>'
```

Use it with `Invoke-Expression` :-

```powershell
> aws-sso-creds export-ps | Invoke-Expression
```

## List accounts

You can also list the accounts you have available within AWS SSO:
Expand Down
39 changes: 39 additions & 0 deletions cmd/aws-sso-creds/exportps/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package exportps

import (
"fmt"

"github.com/jaxxstorm/aws-sso-creds/pkg/credentials"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Command() *cobra.Command {
command := &cobra.Command{
Use: "export-ps",
Short: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment",
Long: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

cmd.SilenceUsage = true

profile := viper.GetString("profile")
homeDir := viper.GetString("home-directory")

creds, _, err := credentials.GetSSOCredentials(profile, homeDir)

if err != nil {
return err
}

fmt.Printf("$env:AWS_ACCESS_KEY_ID='%s'\n", *creds.RoleCredentials.AccessKeyId)
fmt.Printf("$env:AWS_SECRET_ACCESS_KEY='%s'\n", *creds.RoleCredentials.SecretAccessKey)
fmt.Printf("$env:AWS_SESSION_TOKEN='%s'\n", *creds.RoleCredentials.SessionToken)

return nil
},
}

return command
}
2 changes: 2 additions & 0 deletions cmd/aws-sso-creds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/viper"

"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/export"
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/exportps"
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/get"
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/helper"
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/list"
Expand All @@ -29,6 +30,7 @@ func configureCLI() *cobra.Command {
rootCommand.AddCommand(set.Command())
rootCommand.AddCommand(version.Command())
rootCommand.AddCommand(export.Command())
rootCommand.AddCommand(exportps.Command())
rootCommand.AddCommand(list.Command())
rootCommand.AddCommand(helper.Command())

Expand Down

0 comments on commit 3a517fc

Please sign in to comment.