Skip to content

Commit 818a107

Browse files
authored
Backwards compatibility v2 (#108)
* Add refresh command * Credentials file
1 parent acd1c5e commit 818a107

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

cmd/file.go

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func runFile(cmd *cobra.Command, args []string) error {
6565
if err != nil {
6666
return err
6767
}
68+
fmt.Printf("Profile %s for role \"%s\" updated\n", profileName, role)
69+
fmt.Printf("Credentials written to %s\n", destination)
6870
if autoRefresh {
6971
logging.Log.WithFields(logrus.Fields{"role": role}).Infoln("Starting automatic file refresh")
7072
fmt.Printf("starting automatic file refresh for %s", role)

cmd/refresh.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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/spf13/cobra"
23+
"github.com/spf13/viper"
24+
)
25+
26+
func init() {
27+
refreshCmd.PersistentFlags().StringVarP(&roleRefreshARN, "role", "z", "", "role")
28+
rootCmd.AddCommand(refreshCmd)
29+
}
30+
31+
var refreshCmd = &cobra.Command{
32+
Use: "refresh [profile]",
33+
Short: "Refresh AWS credentials for profiles",
34+
Hidden: true,
35+
Args: cobra.MaximumNArgs(1),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
if len(args) > 0 {
38+
profileName = args[0]
39+
}
40+
41+
if roleRefreshARN == "" {
42+
// roleRefreshARN is not present, have to go through aws-profiles to see if a role matches
43+
awsProfiles := viper.GetStringMapString("aws-profiles")
44+
for name, role := range awsProfiles {
45+
if name == profileName {
46+
roleRefreshARN = role
47+
break
48+
}
49+
}
50+
}
51+
if roleRefreshARN == "" {
52+
return fmt.Errorf("unable to find profile %s in 'aws-profiles' property. You can also run with -r role_name <optional_profile_name>", profileName)
53+
}
54+
55+
awsCredentialsFile := viper.GetString("aws-credential-file")
56+
if awsCredentialsFile != "" {
57+
destination = awsCredentialsFile
58+
}
59+
60+
argsPass := []string{roleRefreshARN}
61+
// explicit refresh command means force overwrite the profile
62+
force = true
63+
return fileCmd.RunE(fileCmd, argsPass)
64+
},
65+
}

cmd/vars.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
noOpen bool
4242
profileName string
4343
region string
44+
roleRefreshARN string
4445
shortInfo bool
4546
showAll bool
4647
showConfiguredProfilesOnly bool

0 commit comments

Comments
 (0)