Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOOLS-90 add mfa export command #25

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions internal/commands/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (b *commandsBuilder) addAll() *commandsBuilder {
b.newConfigCmd(),
b.newEnvCmd(),
b.newTunnelCmd(),
b.newMfaCmd(),
)

return b
Expand All @@ -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)
},
})

Expand Down
61 changes: 61 additions & 0 deletions internal/commands/mfa.go
Original file line number Diff line number Diff line change
@@ -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
}