|
| 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 | +} |
0 commit comments