Skip to content

Commit

Permalink
Merge pull request #87 from mrbarge/add_dry_run
Browse files Browse the repository at this point in the history
Add dry-run ability to servicelog posting
  • Loading branch information
openshift-merge-robot authored May 13, 2021
2 parents 0b54e60 + 525671e commit cccf0e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/servicelog/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
BadReply servicelog.BadReply
Message servicelog.Message
template string
isDryRun bool
)

const (
Expand Down Expand Up @@ -65,8 +66,17 @@ var postCmd = &cobra.Command{
// Use the OCM client to create the POST request
// send it as logservice and validate the response
request := createPostRequest(ocmClient, newData)
response := sendRequest(request)

// If this is a dry-run, print the populated template
// but don't proceed further.
if isDryRun {
if err := printTemplate(newData); err != nil {
log.Errorf("Cannot read generated template: %q", err)
}
return
}

response := sendRequest(request)
check(response, dir)

err := dump.Pretty(os.Stdout, response.Bytes())
Expand All @@ -80,6 +90,7 @@ func init() {
// define required flags
postCmd.Flags().StringVarP(&template, "template", "t", defaultTemplate, "Message template file or URL")
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.")
postCmd.Flags().BoolVarP(&isDryRun, "dry-run", "d", false, "Dry-run - print the service log about to be sent but don't send it.")
}

// parseUserParameters parse all the '-p FOO=BAR' parameters and checks for syntax errors
Expand Down Expand Up @@ -228,6 +239,14 @@ func modifyTemplate(dir string) (newData string) {
return newData
}

func printTemplate(filePath string) error {
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
return dump.Pretty(os.Stdout, contents)
}

func createPostRequest(ocmClient *sdk.Connection, newData string) *sdk.Request {
// Create and populate the request:
request := ocmClient.Post()
Expand Down
1 change: 1 addition & 0 deletions docs/command/osdctl_servicelog_post.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ osdctl servicelog post [flags]
### Options

```
-d, --dry-run Dry-run - print the service log about to be sent but don't send it.
-h, --help help for post
-p, --param stringArray Specify a key-value pair (eg. -p FOO=BAR) to set/override a parameter value in the template.
-t, --template string Message template file or URL
Expand Down

0 comments on commit cccf0e5

Please sign in to comment.