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 pathlist.go
150 lines (142 loc) · 5.33 KB
/
list.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"encoding/json"
"fmt"
"github.com/danmx/sigil/pkg/aws"
"github.com/danmx/sigil/pkg/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
sessionFilters = map[string]string{
"after": "",
"before": "",
"target": "",
"owner": "",
}
// listCmd represents the list command
listCmd = &cobra.Command{
Use: "list [--type TYPE] ... { [--instance-ids IDs] [--instance-tags TAGS] | [--session-filters FILTERS] }",
DisableFlagsInUseLine: true,
Short: "List available EC2 instances or SSM sessions",
Long: `Show list of all EC2 instances with AWS SSM Agent running or active SSM sessions.
Supported groups of filters:
- instances:
- tags - list of tag keys with a list of values for given keys
- ids - list of instastance ids
- sessions:
- after - the timestamp, in ISO-8601 Extended format, to see sessions that started after given date
- before - the timestamp, in ISO-8601 Extended format, to see sessions that started before given date
- target - an instance to which session connections have been made
- owner - an AWS user account to see a list of sessions started by that user
Filter format examples:
[default.filters.session]
after="2018-08-29T00:00:00Z"
before="2019-08-29T00:00:00Z"
target="i-xxxxxxxxxxxxxxxx1"
owner="[email protected]"
[default.filters.instance]
ids=["i-xxxxxxxxxxxxxxxx1","i-xxxxxxxxxxxxxxxx2"]
tags=[{key="Name",values=["WebApp1","WebApp2"]}]
`,
Aliases: []string{"ls", "l"},
Example: fmt.Sprintf(`%s list --output-format wide --instance-tags '[{"key":"Name","values":["Web","DB"]}]'`, appName),
//nolint:dupl // deduplicating it wouldn't provide much value
PreRunE: func(cmd *cobra.Command, args []string) error {
// Config bindings
for flag, lookup := range map[string]string{
"output-format": "output-format",
"interactive": "interactive",
"filters.session": "session-filters",
"filters.instance.ids": "session-filters",
"filters.instance.tags": "instance-tags",
"list-type": "type",
} {
if err := cfg.BindPFlag(flag, cmd.Flags().Lookup(lookup)); err != nil {
log.WithFields(log.Fields{
"flag": flag,
"lookup": lookup,
}).Error(err)
return err
}
}
// returns err
return aws.VerifyDependencies()
},
RunE: func(cmd *cobra.Command, args []string) error {
var filters aws.Filters
if err := cfg.UnmarshalKey("filters", &filters); err != nil {
log.Error("failed unmarshaling filters")
return fmt.Errorf("failed unmarshaling filters: %s", err)
}
outputFormat := cfg.GetString("output-format")
profile := cfg.GetString("profile")
region := cfg.GetString("region")
interactive := cfg.GetBool("interactive")
listType := cfg.GetString("list-type")
instanceIDs := cfg.GetStringSlice("filters.instance.ids")
mfaToken := cfg.GetString("mfa")
trace := log.IsLevelEnabled(log.TraceLevel)
// hack to get map[string]string from args
// https://github.com/spf13/viper/issues/608
if cmd.Flags().Changed("session-filters") {
filters.Session = aws.SessionFilters{
After: sessionFilters["after"],
Before: sessionFilters["before"],
Target: sessionFilters["target"],
Owner: sessionFilters["owner"],
}
}
if cmd.Flags().Changed("instance-ids") {
filters.Instance.IDs = instanceIDs
}
var tags []aws.TagValues
if cmd.Flags().Changed("instance-tags") {
if err := json.Unmarshal([]byte(cfg.GetString("filters.instance.tags")), &tags); err != nil {
log.WithField("tags", cfg.GetString("filters.instance.tags")).Error("failed unmarshaling tags")
return fmt.Errorf("failed unmarshaling tags: %s", err)
}
filters.Instance.Tags = tags
}
log.WithFields(log.Fields{
"filters": filters,
"output-format": outputFormat,
"region": region,
"profile": profile,
"mfa": mfaToken,
"interactive": interactive,
"type": listType,
"instanceIDs": instanceIDs,
"sessionFilters": sessionFilters,
"tags": tags,
"trace": trace,
}).Debug("List inputs")
input := &list.StartInput{
OutputFormat: &outputFormat,
MFAToken: &mfaToken,
Region: ®ion,
Profile: &profile,
Filters: &filters,
Interactive: &interactive,
Type: &listType,
Trace: &trace,
}
err := list.Start(input)
if err != nil {
log.Error(err)
return err
}
return nil
},
DisableAutoGenTag: true,
}
)
func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().String("output-format", list.FormatText, fmt.Sprintf("specify output format: %s/%s/%s/%s", list.FormatText, list.FormatWide, list.FormatJSON, list.FormatYAML))
listCmd.Flags().BoolP("interactive", "i", false, "pick an instance or a session from a list and start or terminate the session")
listCmd.Flags().StringP("type", "t", list.TypeListInstances, fmt.Sprintf("specify list type: %s/%s", list.TypeListInstances, list.TypeListSessions))
listCmd.Flags().StringToStringVar(&sessionFilters, "session-filters", sessionFilters, "specify session filters to limit results")
listCmd.Flags().StringSlice("instance-ids", []string{}, "specify instance ids to limit results")
listCmd.Flags().String("instance-tags", "", "specify instance tags, in JSON format, to limit results")
}