Skip to content

Commit 82cbd31

Browse files
committed
Use -p to replace any parameter
1 parent 1cb7f12 commit 82cbd31

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

cmd/post.go

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

2121
var (
22-
template, clusterUUID, caseID string
23-
isURL bool
24-
HTMLBody []byte
25-
Message servicelog.Message
26-
GoodReply servicelog.GoodReply
27-
BadReply servicelog.BadReply
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
2829
)
2930

3031
const (
31-
defaultTemplate = ""
32-
defaultClusterUUID = ""
33-
defaultCaseID = ""
34-
targetAPIPath = "/api/service_logs/v1/cluster_logs" // https://api.openshift.com/?urls.primaryName=Service%20logs#/default/post_api_service_logs_v1_cluster_logs
35-
modifiedJSON = "modified-template.json"
36-
clusterParameter = "${CLUSTER_UUID}"
37-
caseIDParameter = "${CASE_ID}"
38-
clusterUUIDLongName = "cluster-external-id"
39-
caseIDLongName = "support-case-id"
40-
clusterUUIDShorthand = "c"
41-
caseIDShorthand = "i"
32+
defaultTemplate = ""
33+
targetAPIPath = "/api/service_logs/v1/cluster_logs" // https://api.openshift.com/?urls.primaryName=Service%20logs#/default/post_api_service_logs_v1_cluster_logs
34+
modifiedJSON = "modified-template.json"
4235
)
4336

4437
// postCmd represents the post command
4538
var postCmd = &cobra.Command{
4639
Use: "post",
4740
Short: "Send a servicelog message to a given cluster",
4841
Run: func(cmd *cobra.Command, args []string) {
42+
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+
}
55+
4956
readTemplate() // verify and parse
50-
replaceFlags(clusterUUID, defaultClusterUUID, clusterParameter, clusterUUIDLongName, clusterUUIDShorthand)
51-
replaceFlags(caseID, defaultCaseID, caseIDParameter, caseIDLongName, caseIDShorthand)
57+
58+
// Replace the custom parameters given by the user
59+
for k, v := range templateParams {
60+
replaceFlags(sliceValue[k], "", sliceVariable[k], sliceVariable[k], "p", v)
61+
}
62+
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+
}
5277

5378
dir := tempDir()
5479
defer cleanup(dir)
@@ -75,8 +100,7 @@ var postCmd = &cobra.Command{
75100
func init() {
76101
// define required flags
77102
postCmd.Flags().StringVarP(&template, "template", "t", defaultTemplate, "Message template file or URL")
78-
postCmd.Flags().StringVarP(&clusterUUID, clusterUUIDLongName, clusterUUIDShorthand, defaultClusterUUID, "Target cluster UUID")
79-
postCmd.Flags().StringVarP(&caseID, caseIDLongName, caseIDShorthand, defaultCaseID, "Related ticket (RedHat Support Case ID)")
103+
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.")
80104
}
81105

82106
// accessTemplate checks if the provided template is currently accessible and returns an error
@@ -146,16 +170,16 @@ func readTemplate() {
146170
}
147171
}
148172

149-
func replaceFlags(flagName, flagDefaultValue, flagParameter, flagLongName, flagShorthand string) {
173+
func replaceFlags(flagName, flagDefaultValue, flagParameter, flagLongName, flagShorthand, parameter string) {
150174
if err := strings.Compare(flagName, flagDefaultValue); err == 0 {
151175
// The user didn't set the flag. Check if the template is using the flag.
152176
if found := Message.SearchFlag(flagParameter); found == true {
153-
log.Fatalf("The selected template is using '%s' parameter, but '%s' flag is not set. Use '-%s' to fix this.", flagParameter, flagLongName, flagShorthand)
177+
log.Fatalf("The selected template is using '%s' parameter, but '%s' flag was not set. Use '-%s' to fix this.", flagParameter, flagLongName, flagShorthand)
154178
}
155179
} else {
156180
// The user set the flag. Check if the template is using the flag.
157181
if found := Message.SearchFlag(flagParameter); found == false {
158-
log.Fatalf("The selected template is not using '%s' parameter, but '%s' flag is set. Do not use '-%s' to fix this.", flagParameter, flagLongName, flagShorthand)
182+
log.Fatalf("The selected template is not using '%s' parameter, but '--%s' flag was set. Do not use '-%s %s' to fix this.", flagParameter, "param", flagShorthand, parameter)
159183
}
160184
Message.ReplaceWithFlag(flagParameter, flagName)
161185
}
@@ -261,8 +285,8 @@ func validateGoodResponse(body []byte) {
261285
log.Fatalf("Message sent, but wrong service_name information was passed (wanted %q, got %q)", Message.ServiceName, serviceName)
262286
}
263287
clusteruuid := GoodReply.ClusterUUID
264-
if clusterUUID != clusteruuid {
265-
log.Fatalf("Message sent, but to different cluster (wanted %q, got %q)", clusterUUID, clusteruuid)
288+
if clusteruuid != Message.ClusterUUID {
289+
log.Fatalf("Message sent, but to different cluster (wanted %q, got %q)", Message.ClusterUUID, clusteruuid)
266290
}
267291
summary := GoodReply.Summary
268292
if summary != Message.Summary {

docs/command/osdctl_servicelog_post.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ osdctl servicelog post [flags]
1313
### Options
1414

1515
```
16-
-c, --cluster-external-id string Target cluster UUID
17-
-h, --help help for post
18-
-i, --support-case-id string Related ticket (RedHat Support Case ID)
19-
-t, --template string Message template file or URL
16+
-h, --help help for post
17+
-p, --param stringArray Specify a key-value pair (eg. -p FOO=BAR) to set/override a parameter value in the template.
18+
-t, --template string Message template file or URL
2019
```
2120

2221
### Options inherited from parent commands

internal/servicelog/template.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package servicelog
22

3-
import "strings"
3+
import (
4+
"regexp"
5+
"strings"
6+
)
47

58
// Message is the base template structure
69
type Message struct {
@@ -62,3 +65,13 @@ func (m *Message) SearchFlag(placeholder string) (found bool) {
6265
}
6366
return false
6467
}
68+
69+
func (m *Message) FindLeftovers() (matches []string, found bool) {
70+
r := regexp.MustCompile(`\${[^{}]*}`)
71+
str := m.Severity + m.ServiceName + m.ClusterUUID + m.Summary + m.Description
72+
matches = r.FindAllString(str, -1)
73+
if len(matches) > 0 {
74+
found = true
75+
}
76+
return matches, found
77+
}

0 commit comments

Comments
 (0)