diff --git a/internal/commands/base.go b/internal/commands/base.go index 40c4db85..b20be57f 100644 --- a/internal/commands/base.go +++ b/internal/commands/base.go @@ -57,6 +57,7 @@ func (b *commandsBuilder) addAll() *commandsBuilder { b.newConfigCmd(), b.newEnvCmd(), b.newTunnelCmd(), + b.newMfaCmd(), ) return b @@ -82,21 +83,21 @@ This application is a tool to generate the needed files to quickly create a Cobra application.`, PersistentPreRun: func(cmd *cobra.Command, args []string) { cc.cmd.PersistentFlags().StringVarP(&cc.ll, "log-level", "l", "", "enable debug message") - cc.cmd.PersistentFlags().StringVarP(&cc.cfgFile, "config-file", "c", "", "set config file name") + cc.cmd.PersistentFlags().StringVarP(&cc.cfgFile, "config-file", "c", "", "set config file name") cc.cmd.PersistentFlags().Parse(args) - var logLevel zapcore.Level + var logLevel zapcore.Level - switch cc.ll { - case "info": - logLevel = zapcore.InfoLevel - case "debug": - logLevel = zapcore.DebugLevel - default: - logLevel = zapcore.WarnLevel - } + switch cc.ll { + case "info": + logLevel = zapcore.InfoLevel + case "debug": + logLevel = zapcore.DebugLevel + default: + logLevel = zapcore.WarnLevel + } - cc.log = logger.NewSugaredLogger(logLevel) + cc.log = logger.NewSugaredLogger(logLevel) }, }) diff --git a/internal/commands/mfa.go b/internal/commands/mfa.go new file mode 100644 index 00000000..3db4111c --- /dev/null +++ b/internal/commands/mfa.go @@ -0,0 +1,61 @@ +package commands + +import ( + "fmt" + + "github.com/hazelops/ize/internal/aws/utils" + "github.com/spf13/cobra" +) + +type mfaCmd struct { + *baseBuilderCmd +} + +func (b *commandsBuilder) newMfaCmd() *mfaCmd { + cc := &mfaCmd{} + + cmd := &cobra.Command{ + Use: "mfa", + Short: "", + Long: "", + RunE: nil, + TraverseChildren: true, + } + + mfaCmd := &cobra.Command{ + Use: "export", + Short: "Generate terraform files", + Long: "This command generate terraform files", + RunE: func(cmd *cobra.Command, args []string) error { + err := cc.Init() + if err != nil { + return err + } + + sess, err := utils.GetSession(&utils.SessionConfig{ + Region: cc.config.AwsRegion, + Profile: cc.config.AwsProfile, + }) + if err != nil { + return err + } + + v, err := sess.Config.Credentials.Get() + if err != nil { + return err + } + + fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s && \nexport AWS_SESSION_TOKEN=%s && \nexport AWS_ACCESS_KEY_ID=%s", + v.SecretAccessKey, v.SessionToken, v.AccessKeyID, + ) + + return nil + }, + } + + cmd.AddCommand(mfaCmd) + + cc.baseBuilderCmd = b.newBuilderBasicCdm(cmd) + + return cc +}