Skip to content

Commit 3a517fc

Browse files
authored
Merge pull request #60 from RustyF/powershell-export
export-ps - a PowerShell variation of export
2 parents 2e5c713 + 039cc64 commit 3a517fc

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ export AWS_SECRET_ACCESS_KEY=<SECRET_KEY>
7878
export AWS_SESSION_TOKEN=<SESSION_TOKEN>
7979
```
8080

81+
#### PowerShell more your thing?
82+
83+
If you're using PowerShell, you can use `export-ps` to generate PowerShell assignments, instead.
84+
85+
```powershell
86+
> aws-sso-creds export-ps
87+
$env:AWS_ACCESS_KEY_ID='<KEY>'
88+
$env:AWS_SECRET_ACCESS_KEY='<SECRET_KEY>'
89+
$env:AWS_SESSION_TOKEN='<SESSION_TOKEN>'
90+
```
91+
92+
Use it with `Invoke-Expression` :-
93+
94+
```powershell
95+
> aws-sso-creds export-ps | Invoke-Expression
96+
```
97+
8198
## List accounts
8299

83100
You can also list the accounts you have available within AWS SSO:

cmd/aws-sso-creds/exportps/cli.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package exportps
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/jaxxstorm/aws-sso-creds/pkg/credentials"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
func Command() *cobra.Command {
12+
command := &cobra.Command{
13+
Use: "export-ps",
14+
Short: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment",
15+
Long: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment",
16+
SilenceUsage: true,
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
19+
cmd.SilenceUsage = true
20+
21+
profile := viper.GetString("profile")
22+
homeDir := viper.GetString("home-directory")
23+
24+
creds, _, err := credentials.GetSSOCredentials(profile, homeDir)
25+
26+
if err != nil {
27+
return err
28+
}
29+
30+
fmt.Printf("$env:AWS_ACCESS_KEY_ID='%s'\n", *creds.RoleCredentials.AccessKeyId)
31+
fmt.Printf("$env:AWS_SECRET_ACCESS_KEY='%s'\n", *creds.RoleCredentials.SecretAccessKey)
32+
fmt.Printf("$env:AWS_SESSION_TOKEN='%s'\n", *creds.RoleCredentials.SessionToken)
33+
34+
return nil
35+
},
36+
}
37+
38+
return command
39+
}

cmd/aws-sso-creds/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spf13/viper"
77

88
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/export"
9+
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/exportps"
910
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/get"
1011
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/helper"
1112
"github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/list"
@@ -29,6 +30,7 @@ func configureCLI() *cobra.Command {
2930
rootCommand.AddCommand(set.Command())
3031
rootCommand.AddCommand(version.Command())
3132
rootCommand.AddCommand(export.Command())
33+
rootCommand.AddCommand(exportps.Command())
3234
rootCommand.AddCommand(list.Command())
3335
rootCommand.AddCommand(helper.Command())
3436

0 commit comments

Comments
 (0)