Skip to content

Commit b92900e

Browse files
committed
Refactor a bit
1 parent 82cbd31 commit b92900e

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

cmd/post.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919
)
2020

2121
var (
22-
template string
23-
isURL bool
24-
HTMLBody []byte
25-
Message servicelog.Message
26-
GoodReply servicelog.GoodReply
27-
BadReply servicelog.BadReply
28-
templateParams, sliceVariable, sliceValue []string
22+
template string
23+
isURL bool
24+
HTMLBody []byte
25+
Message servicelog.Message
26+
GoodReply servicelog.GoodReply
27+
BadReply servicelog.BadReply
28+
templateParams, userParameterNames, userParameterValues []string
2929
)
3030

3131
const (
@@ -40,40 +40,17 @@ var postCmd = &cobra.Command{
4040
Short: "Send a servicelog message to a given cluster",
4141
Run: func(cmd *cobra.Command, args []string) {
4242

43-
// Parse all the '-p' parameters from the command line
44-
for k, v := range templateParams {
45-
if !strings.Contains(v, "=") {
46-
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
47-
}
48-
sliceVariable = append(sliceVariable, fmt.Sprintf("${%v}", strings.Split(v, "=")[0]))
49-
sliceValue = append(sliceValue, strings.Split(v, "=")[1])
50-
51-
if sliceValue[k] == "" {
52-
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
53-
}
54-
}
43+
parseUserParameters() // parse all the '-p' user flags
5544

56-
readTemplate() // verify and parse
45+
readTemplate() // parse the given JSON template provided via '-t' flag
5746

58-
// Replace the custom parameters given by the user
47+
// For every '-p' flag, replace its related placeholder in the template
5948
for k, v := range templateParams {
60-
replaceFlags(sliceValue[k], "", sliceVariable[k], sliceVariable[k], "p", v)
49+
replaceFlags(userParameterValues[k], "", userParameterNames[k], userParameterNames[k], "p", v)
6150
}
6251

63-
// Check if there are any remaining parameters (aka ${...}) in the template that was not replaces
64-
// checkLeftoverParameters()
65-
unusedParameters, found := Message.FindLeftovers()
66-
if found {
67-
for _, v := range unusedParameters {
68-
regex := strings.NewReplacer("${", "", "}", "")
69-
log.Errorf("The selected template is using '%s' parameter, but '--%s' flag is not set for this one. Use '-%s %v=\"FOOBAR\"' to fix this.", v, "param", "p", regex.Replace(v))
70-
}
71-
if numberOfMissingParameters := len(unusedParameters); numberOfMissingParameters == 1 {
72-
log.Fatal("Please define this missing parameter properly.")
73-
} else {
74-
log.Fatalf("Please define all %v missing parameters properly.", numberOfMissingParameters)
75-
}
76-
}
52+
// Check if there are any remaining placeholders in the template that are not replaced by a parameter
53+
checkLeftovers()
7754

7855
dir := tempDir()
7956
defer cleanup(dir)
@@ -103,6 +80,22 @@ func init() {
10380
postCmd.Flags().StringArrayVarP(&templateParams, "param", "p", templateParams, "Specify a key-value pair (eg. -p FOO=BAR) to set/override a parameter value in the template.")
10481
}
10582

83+
// parseUserParameters parse all the '-p FOO=BAR' parameters and checks for syntax errors
84+
func parseUserParameters() {
85+
for k, v := range templateParams {
86+
if !strings.Contains(v, "=") {
87+
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
88+
}
89+
90+
userParameterNames = append(userParameterNames, fmt.Sprintf("${%v}", strings.Split(v, "=")[0]))
91+
userParameterValues = append(userParameterValues, strings.Split(v, "=")[1])
92+
93+
if userParameterValues[k] == "" {
94+
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
95+
}
96+
}
97+
}
98+
10699
// accessTemplate checks if the provided template is currently accessible and returns an error
107100
func accessTemplate(template string) (err error) {
108101

@@ -170,6 +163,21 @@ func readTemplate() {
170163
}
171164
}
172165

166+
func checkLeftovers() {
167+
unusedParameters, found := Message.FindLeftovers()
168+
if found {
169+
for _, v := range unusedParameters {
170+
regex := strings.NewReplacer("${", "", "}", "")
171+
log.Errorf("The selected template is using '%s' parameter, but '--%s' flag is not set for this one. Use '-%s %v=\"FOOBAR\"' to fix this.", v, "param", "p", regex.Replace(v))
172+
}
173+
if numberOfMissingParameters := len(unusedParameters); numberOfMissingParameters == 1 {
174+
log.Fatal("Please define this missing parameter properly.")
175+
} else {
176+
log.Fatalf("Please define all %v missing parameters properly.", numberOfMissingParameters)
177+
}
178+
}
179+
}
180+
173181
func replaceFlags(flagName, flagDefaultValue, flagParameter, flagLongName, flagShorthand, parameter string) {
174182
if err := strings.Compare(flagName, flagDefaultValue); err == 0 {
175183
// The user didn't set the flag. Check if the template is using the flag.

0 commit comments

Comments
 (0)