|
7 | 7 | "log" |
8 | 8 | "os" |
9 | 9 | "os/signal" |
| 10 | + "strings" |
10 | 11 |
|
11 | 12 | "github.com/aws/aws-sdk-go-v2/service/ssm" |
12 | 13 | "github.com/aws/aws-sdk-go-v2/service/ssm/types" |
@@ -51,12 +52,13 @@ func FetchKeysOfParameters( |
51 | 52 | var parameters []string |
52 | 53 |
|
53 | 54 | // Set parameter filters |
54 | | - filterKey := "tag:Product" |
55 | | - parameterFilters := []types.ParameterStringFilter{ |
56 | | - { |
| 55 | + parameterFilters := []types.ParameterStringFilter{} |
| 56 | + for _, ft := range flags.FilterTags { |
| 57 | + filterKey := fmt.Sprintf("tag:%s", ft.Name) |
| 58 | + parameterFilters = append(parameterFilters, types.ParameterStringFilter{ |
57 | 59 | Key: &filterKey, |
58 | | - Values: []string{flags.ProductTag}, |
59 | | - }, |
| 60 | + Values: []string{ft.Value}, |
| 61 | + }) |
60 | 62 | } |
61 | 63 | describeInput := &ssm.DescribeParametersInput{ |
62 | 64 | MaxResults: int32(flags.MaxResults), |
@@ -107,6 +109,26 @@ func GenerateChunks(flatSlice []string, chunkSize int) [][]string { |
107 | 109 | return chunks |
108 | 110 | } |
109 | 111 |
|
| 112 | +// ParseFilterTags convert string from user input to key value structure |
| 113 | +func ParseFilterTags(filterTagsStr string) []FilterTag { |
| 114 | + var filterTags []FilterTag |
| 115 | + |
| 116 | + filterTagsSlice := strings.Split(filterTagsStr, ",") |
| 117 | + for _, t := range filterTagsSlice { |
| 118 | + tagNameValue := strings.Split(t, ":") |
| 119 | + if len(tagNameValue) != 2 || len(tagNameValue[0]) == 0 || len(tagNameValue[1]) == 0 { |
| 120 | + log.Printf("Unable to parse tag name and value: %s", t) |
| 121 | + continue |
| 122 | + } |
| 123 | + filterTags = append(filterTags, FilterTag{ |
| 124 | + Name: tagNameValue[0], |
| 125 | + Value: tagNameValue[1], |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + return filterTags |
| 130 | +} |
| 131 | + |
110 | 132 | // WriteToFile generate or update existing file and |
111 | 133 | // flash to it environment variables |
112 | 134 | func WriteToFile(parameters []Parameter, outfile string, update bool, export bool) { |
@@ -150,17 +172,22 @@ func HandleSignals(cancel context.CancelFunc) { |
150 | 172 |
|
151 | 173 | func Extract() { |
152 | 174 | var flags Flags |
| 175 | + var filterTagsStr string |
153 | 176 | flag.BoolVar(&flags.Export, "export", false, "Add prefix 'export' to each parameter") |
154 | 177 | flag.IntVar(&flags.MaxResults, "max", 3, "The maximum number of items to return for call to AWS") |
155 | 178 | flag.StringVar(&flags.Outfile, "outfile", "", "Output file where parameters will be saved") |
156 | | - flag.StringVar(&flags.ProductTag, "product", "", "Product tag") |
| 179 | + flag.StringVar(&filterTagsStr, "tags", "", "Product tags for filter separated by comma in format 'tagName1:tagValue1,tagName2:tagValue2'") |
157 | 180 | flag.BoolVar(&flags.Update, "update", false, "Update existing file if exists (by default the file will be overwritten)") |
158 | 181 | flag.Parse() |
159 | 182 |
|
160 | | - if flags.ProductTag == "" { |
161 | | - log.Fatalln("Please specify the tag of product") |
| 183 | + if filterTagsStr == "" { |
| 184 | + log.Fatalln("Please specify the tags for filter, at least Product tag") |
162 | 185 | } |
163 | 186 |
|
| 187 | + // Convert string of tags for filter to key:value structure |
| 188 | + filterTags := ParseFilterTags(filterTagsStr) |
| 189 | + flags.FilterTags = filterTags |
| 190 | + |
164 | 191 | ctx, cancel := context.WithCancel(context.Background()) |
165 | 192 | go HandleSignals(cancel) |
166 | 193 |
|
|
0 commit comments