This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsession.go
80 lines (74 loc) · 2.5 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cmd
import (
"fmt"
"github.com/danmx/sigil/pkg/aws"
"github.com/danmx/sigil/pkg/session"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// sessionCmd represents the session command
var sessionCmd = &cobra.Command{
Use: "session [--type TYPE] ... TARGET",
DisableFlagsInUseLine: true,
Short: "Start a session",
Long: `Start a new session in chosen EC2 instance.`,
Aliases: []string{"sess", "s"},
Example: fmt.Sprintf("%s session --type instance-id i-xxxxxxxxxxxxxxxxx", appName),
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 && cmd.Flags().Lookup("target").Changed {
return fmt.Errorf("can't use both target argument (%s) and deprecated flag (%s)", args[0], cmd.Flags().Lookup("target").Value.String())
}
// Config bindings
for _, flag := range []string{"target", "type"} {
if err := cfg.BindPFlag(flag, cmd.Flags().Lookup(flag)); err != nil {
log.WithFields(log.Fields{
"flag": flag,
}).Error(err)
return err
}
}
if len(args) > 0 {
cfg.Set("target", args[0])
}
// returns err
return aws.VerifyDependencies()
},
RunE: func(cmd *cobra.Command, args []string) error {
target := cfg.GetString("target")
targetType := cfg.GetString("type")
profile := cfg.GetString("profile")
region := cfg.GetString("region")
mfaToken := cfg.GetString("mfa")
trace := log.IsLevelEnabled(log.TraceLevel)
log.WithFields(log.Fields{
"target": target,
"type": targetType,
"region": region,
"profile": profile,
"mfa": mfaToken,
"trace": trace,
}).Debug("Session inputs")
input := &session.StartInput{
Target: &target,
TargetType: &targetType,
Region: ®ion,
Profile: &profile,
MFAToken: &mfaToken,
Trace: &trace,
}
// returns err
return session.Start(input)
},
DisableAutoGenTag: true,
}
func init() {
rootCmd.AddCommand(sessionCmd)
sessionCmd.Flags().String("target", "", "specify the target depending on the type")
// Deprecating the target flag
err := sessionCmd.Flags().MarkDeprecated("target", "this flag will be deprecated in future releases, use args instead")
if err != nil {
log.WithField("flag", sessionCmd.Flags().Lookup("target")).Error(err)
}
sessionCmd.Flags().String("type", aws.TargetTypeInstanceID, fmt.Sprintf("specify target type: %s/%s/%s", aws.TargetTypeInstanceID, aws.TargetTypePrivateDNS, aws.TargetTypeName))
}