Skip to content

Commit ccfc4d1

Browse files
author
Josh Newman
committed
ref(cmd): export useful functions for vendoring
1 parent 7ef2839 commit ccfc4d1

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

cmd/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func getStringChunks(items []string, chunkSize int) (chunks [][]string) {
3838
return append(chunks, items)
3939
}
4040

41-
// sortParams sorts parameters by name.
42-
func sortParams(params []types.Parameter) {
41+
// sortParameters sorts parameters by name.
42+
func sortParameters(params []types.Parameter) {
4343
sort.Slice(params, func(i, j int) bool {
4444
return strings.ToLower(*params[i].Name) < strings.ToLower(*params[j].Name)
4545
})

cmd/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func runDiffCmd(cmd *cobra.Command, args []string) {
5050
Decrypt: decrypt,
5151
}
5252

53-
params1 := getParams(options, []types.Parameter{}, nil)
53+
params1 := GetParameters(options, []types.Parameter{}, nil)
5454

5555
options.Path = &path2
56-
params2 := getParams(options, []types.Parameter{}, nil)
56+
params2 := GetParameters(options, []types.Parameter{}, nil)
5757

5858
tw := table.NewWriter()
5959
tw.Style().Format.Header = text.FormatLower

cmd/get.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,47 @@ func runGetCmd(cmd *cobra.Command, args []string) {
3131

3232
client := ssm.NewFromConfig(awsConfig)
3333

34-
out, err := client.GetParameter(context.TODO(), &ssm.GetParameterInput{
35-
Name: &path,
36-
WithDecryption: decrypt,
34+
val, err := GetParameter(&GetParameterOptions{
35+
Client: client,
36+
Name: path,
37+
Decrypt: decrypt,
3738
})
3839
if err != nil {
3940
panic(err)
4041
}
4142

42-
val := *out.Parameter.Value
43-
44-
if strings.HasSuffix(val, "\n") {
45-
val = strings.TrimSuffix(val, "\n")
46-
}
47-
4843
if copy {
4944
if err := clipboard.WriteAll(val); err != nil {
5045
panic(err)
5146
}
47+
5248
fmt.Println("Value copied to clipboard!")
5349
} else {
5450
fmt.Println(val)
5551
}
5652
}
5753

54+
// GetParameterOptions represents options for GetParameter
55+
type GetParameterOptions struct {
56+
Client *ssm.Client
57+
Name string
58+
Decrypt bool
59+
}
60+
61+
// GetParamter returns a parameter by name while removing any trailing new-line
62+
func GetParameter(options *GetParameterOptions) (string, error) {
63+
out, err := options.Client.GetParameter(context.TODO(), &ssm.GetParameterInput{
64+
Name: &options.Name,
65+
WithDecryption: options.Decrypt,
66+
})
67+
if err != nil {
68+
return "", err
69+
}
70+
71+
// return val, nil
72+
return strings.TrimSuffix(*out.Parameter.Value, "\n"), nil
73+
}
74+
5875
func init() {
5976
getCmd.Flags().BoolP("decrypt", "d", true, "decrypt \"SecureString\" value")
6077
getCmd.Flags().BoolP("copy", "c", false, "copy to clipboard")

cmd/ls.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ func runLsCmd(cmd *cobra.Command, args []string) {
4444
Decrypt: decrypt,
4545
}
4646

47-
params := getParams(options, []types.Parameter{}, nil)
48-
49-
sortParams(params)
47+
params := GetParameters(options, []types.Parameter{}, nil)
5048

5149
tw := table.NewWriter()
5250

@@ -107,7 +105,7 @@ type getParamsOptions struct {
107105
Decrypt bool
108106
}
109107

110-
func getParams(options *getParamsOptions, params []types.Parameter, nextToken *string) []types.Parameter {
108+
func GetParameters(options *getParamsOptions, params []types.Parameter, nextToken *string) []types.Parameter {
111109
cfg := &ssm.GetParametersByPathInput{
112110
Path: options.Path,
113111
Recursive: options.Recursive,
@@ -124,21 +122,19 @@ func getParams(options *getParamsOptions, params []types.Parameter, nextToken *s
124122
}
125123

126124
for i := 0; i < len(out.Parameters); i++ {
127-
val := *out.Parameters[i].Value
125+
val := strings.TrimSuffix(*out.Parameters[i].Value, "\n")
128126

129-
// seems like the upgrade to v2 of the aws-sdk sends back a new line on values :shrug:
130-
if strings.HasSuffix(val, "\n") {
131-
trimmed := strings.TrimSuffix(val, "\n")
132-
out.Parameters[i].Value = &trimmed
133-
}
127+
out.Parameters[i].Value = &val
134128
}
135129

136130
params = append(params, out.Parameters...)
137131

138132
if out.NextToken != nil {
139-
return getParams(options, params, out.NextToken)
133+
return GetParameters(options, params, out.NextToken)
140134
}
141135

136+
sortParameters(params)
137+
142138
return params
143139
}
144140

cmd/migrate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func runMigrateCmd(cmd *cobra.Command, args []string) {
6464
Decrypt: true,
6565
}
6666

67-
params := getParams(options, []types.Parameter{}, nil)
67+
params := GetParameters(options, []types.Parameter{}, nil)
6868

6969
fmt.Println(text.FgBlue.Sprintf("Found %d parameters to migrate...", len(params)))
7070

@@ -104,7 +104,9 @@ func init() {
104104
migrateCmd.Flags().StringP("region-to", "t", "", "the region to migrate to")
105105
migrateCmd.Flags().Bool("overwrite", false, "overwrite destination params")
106106

107-
migrateCmd.MarkFlagRequired("region-from")
107+
if err := migrateCmd.MarkFlagRequired("region-from"); err != nil {
108+
panic(err)
109+
}
108110

109111
rootCmd.AddCommand(migrateCmd)
110112
}

cmd/rm.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ func runRmCmd(cmd *cobra.Command, args []string) {
4242
Decrypt: false,
4343
}
4444

45-
params := getParams(opts, []types.Parameter{}, nil)
45+
params := GetParameters(opts, []types.Parameter{}, nil)
4646

4747
if len(params) == 0 {
4848
fmt.Println("No parameters to delete at the specified path.")
4949
os.Exit(0)
5050
}
5151

52-
sortParams(params)
53-
5452
fmt.Println(text.FgYellow.Sprint("The following parameters will be removed..."))
5553

5654
for _, param := range params {

0 commit comments

Comments
 (0)