Skip to content

Commit e08ee3c

Browse files
authored
Wrapper for export (#110)
* Wrapper for export * PR update * Legacy commands
1 parent c82978b commit e08ee3c

File tree

5 files changed

+141
-5
lines changed

5 files changed

+141
-5
lines changed

cmd/export.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func runExport(cmd *cobra.Command, args []string) error {
5757
logging.LogError(err, "Error getting credentials")
5858
return err
5959
}
60-
printExport(credentials)
60+
if !useShellFlag {
61+
// user hasn't explicitly passed in a shell
62+
printExport(credentials)
63+
} else {
64+
printExportForShell(shellInfo, credentials)
65+
}
6166
return nil
6267
}
6368

@@ -72,13 +77,31 @@ func isFish() bool {
7277
}
7378
}
7479

80+
// User hasn't specified a shell, attempt to guess it
7581
func printExport(creds *aws.Credentials) {
7682
if isFish() {
7783
// fish has a different way of setting variables than bash/zsh and others
78-
fmt.Printf("set -x AWS_ACCESS_KEY_ID %s && set -x AWS_SECRET_ACCESS_KEY %s && set -x AWS_SESSION_TOKEN %s\n",
79-
creds.AccessKeyId, creds.SecretAccessKey, creds.SessionToken)
84+
printExportForShell("fish", creds)
8085
} else {
81-
fmt.Printf("export AWS_ACCESS_KEY_ID=%s && export AWS_SECRET_ACCESS_KEY=%s && export AWS_SESSION_TOKEN=%s\n",
82-
creds.AccessKeyId, creds.SecretAccessKey, creds.SessionToken)
86+
// defaults to bash
87+
printExportForShell("bash", creds)
88+
}
89+
}
90+
91+
// Prints out the export command for a specific shell, as defined by user
92+
func printExportForShell(shell string, creds *aws.Credentials) {
93+
fmt.Println(exportVar(shell, "AWS_ACCESS_KEY_ID", creds.AccessKeyId))
94+
fmt.Println(exportVar(shell, "AWS_SECRET_ACCESS_KEY", creds.SecretAccessKey))
95+
fmt.Println(exportVar(shell, "AWS_SESSION_TOKEN", creds.SessionToken))
96+
}
97+
98+
func exportVar(shell, name, value string) string {
99+
switch shell {
100+
case "fish":
101+
return fmt.Sprintf("set -gx %s %q;", name, value)
102+
case "csh", "tcsh":
103+
return fmt.Sprintf("setenv %s %q;", name, value)
104+
default: // "sh", "bash", "ksh", "zsh":
105+
return fmt.Sprintf("export %s=%q", name, value)
83106
}
84107
}

cmd/legacy.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
func init() {
24+
rootCmd.AddCommand(legacyCmd)
25+
}
26+
27+
// legacyCmd to allow for backwards-compatibility for legacy commands that implement the functionality currently
28+
var legacyCmd = &cobra.Command{
29+
Use: "legacy [export]",
30+
Short: "Legacy commands for backwards-compatibility",
31+
Hidden: true,
32+
}

cmd/legacy_export.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/netflix/weep/pkg/util"
23+
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
)
27+
28+
func init() {
29+
legacyExportCmd.PersistentFlags().StringVarP(&roleRefreshARN, "role", "z", "", "role")
30+
legacyExportCmd.PersistentFlags().StringVar(&shellInfo, "shell", "bash", "--shell=sh|bash|ksh|zsh|fish|csh|tcsh")
31+
legacyCmd.AddCommand(legacyExportCmd)
32+
}
33+
34+
// wrapper to allow for backwards-compatibility for commands that implement the export functionality currently
35+
var legacyExportCmd = &cobra.Command{
36+
Use: "export [profile]",
37+
Short: exportShortHelp,
38+
Hidden: true,
39+
Args: cobra.MaximumNArgs(1),
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
if len(args) > 0 {
42+
profileName = args[0]
43+
} else {
44+
profileName = "default"
45+
}
46+
47+
if roleRefreshARN == "" {
48+
// roleRefreshARN is not present, have to go through aws-profiles to see if a role matches
49+
awsProfiles := viper.GetStringMapString("aws-profiles")
50+
for name, role := range awsProfiles {
51+
if name == profileName {
52+
roleRefreshARN = role
53+
break
54+
}
55+
}
56+
}
57+
if roleRefreshARN == "" {
58+
return fmt.Errorf("unable to find profile %s in 'aws-profiles' property. You can also run with -r role_name <optional_profile_name>", profileName)
59+
}
60+
61+
argsPass := []string{roleRefreshARN}
62+
// explicit shell flag, rather than guessing which shell to use
63+
useShellFlag = true
64+
shells := []string{"sh", "bash", "ksh", "zsh", "fish", "csh", "tcsh"}
65+
if !util.StringInSlice(shellInfo, shells) {
66+
return fmt.Errorf("shell must be one of %s", shells)
67+
}
68+
return exportCmd.RunE(exportCmd, argsPass)
69+
},
70+
}

cmd/vars.go

+2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ var (
4242
profileName string
4343
region string
4444
roleRefreshARN string
45+
shellInfo string
4546
shortInfo bool
4647
showAll bool
4748
showConfiguredProfilesOnly bool
4849
showInstanceProfilesOnly bool
4950
shutdown chan os.Signal
51+
useShellFlag bool
5052
)
5153

5254
var completionShortHelp = "Generate completion script"

pkg/util/util.go

+9
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,12 @@ func RenderTabularData(headers []string, data [][]string) string {
206206
table.Render()
207207
return tableString.String()
208208
}
209+
210+
func StringInSlice(elem string, list []string) bool {
211+
for _, elemInList := range list {
212+
if elem == elemInList {
213+
return true
214+
}
215+
}
216+
return false
217+
}

0 commit comments

Comments
 (0)