Skip to content

Commit ba89d88

Browse files
GokceGKDiogoFerrao
andauthored
Implement file path flag for generate-payload (#341)
* implement FileOutput method * implement --file-path for generate_payload commands * update docs * Update internal/cmd/argus/scrape-config/generate-payload/generate_payload.go Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * move FileOutput function to fileutils in pkg * change used FileOutput function * edit flag descriptions * pass only filePath as argument * add example without --file-path flag * update docs * remove unused arg * rename function * change error handling --------- Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com>
1 parent d86e97b commit ba89d88

File tree

11 files changed

+232
-36
lines changed

11 files changed

+232
-36
lines changed

docs/stackit_argus_scrape-config_generate-payload.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ stackit argus scrape-config generate-payload [flags]
2020

2121
```
2222
Generate a Create payload with default values, and adapt it with custom values for the different configuration options
23-
$ stackit argus scrape-config generate-payload > ./payload.json
23+
$ stackit argus scrape-config generate-payload --file-path ./payload.json
2424
<Modify payload in file, if needed>
2525
$ stackit argus scrape-config create my-config --payload @./payload.json
2626
2727
Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and adapt it with custom values for the different configuration options
28-
$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx > ./payload.json
28+
$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx --file-path ./payload.json
2929
<Modify payload in file>
3030
$ stackit argus scrape-config update my-config --payload @./payload.json
31+
32+
Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and preview it in the terminal
33+
$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx
3134
```
3235

3336
### Options
3437

3538
```
39+
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
3640
-h, --help Help for "stackit argus scrape-config generate-payload"
3741
--instance-id string Instance ID
3842
-n, --job-name string If set, generates an update payload with the current state of the given scrape config. If unset, generates a create payload with default values

docs/stackit_load-balancer_generate-payload.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@ stackit load-balancer generate-payload [flags]
1515

1616
```
1717
Generate a payload, and adapt it with custom values for the different configuration options
18-
$ stackit load-balancer generate-payload > ./payload.json
18+
$ stackit load-balancer generate-payload --file-path ./payload.json
1919
<Modify payload in file, if needed>
2020
$ stackit load-balancer create --payload @./payload.json
2121
2222
Generate a payload with values of an existing load balancer, and adapt it with custom values for the different configuration options
23-
$ stackit load-balancer generate-payload --lb-name xxx > ./payload.json
23+
$ stackit load-balancer generate-payload --lb-name xxx --file-path ./payload.json
2424
<Modify payload in file>
2525
$ stackit load-balancer update xxx --payload @./payload.json
26+
27+
Generate a payload with values of an existing load balancer, and preview it in the terminal
28+
$ stackit load-balancer generate-payload --lb-name xxx
2629
```
2730

2831
### Options
2932

3033
```
31-
-h, --help Help for "stackit load-balancer generate-payload"
32-
-n, --lb-name string If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values
34+
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
35+
-h, --help Help for "stackit load-balancer generate-payload"
36+
-n, --lb-name string If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values
3337
```
3438

3539
### Options inherited from parent commands

docs/stackit_ske_cluster_generate-payload.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ stackit ske cluster generate-payload [flags]
1515

1616
```
1717
Generate a payload with default values, and adapt it with custom values for the different configuration options
18-
$ stackit ske cluster generate-payload > ./payload.json
18+
$ stackit ske cluster generate-payload --file-path ./payload.json
1919
<Modify payload in file, if needed>
2020
$ stackit ske cluster create my-cluster --payload @./payload.json
2121
2222
Generate a payload with values of a cluster, and adapt it with custom values for the different configuration options
23-
$ stackit ske cluster generate-payload --cluster-name my-cluster > ./payload.json
23+
$ stackit ske cluster generate-payload --cluster-name my-cluster --file-path ./payload.json
2424
<Modify payload in file>
2525
$ stackit ske cluster update my-cluster --payload @./payload.json
26+
27+
Generate a payload with values of a cluster, and preview it in the terminal
28+
$ stackit ske cluster generate-payload --cluster-name my-cluster
2629
```
2730

2831
### Options
2932

3033
```
3134
-n, --cluster-name string If set, generates the payload with the current state of the given cluster. If unset, generates the payload with default values
35+
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
3236
-h, --help Help for "stackit ske cluster generate-payload"
3337
```
3438

internal/cmd/argus/scrape-config/generate-payload/generate_payload.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
99
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
@@ -20,12 +21,14 @@ import (
2021
const (
2122
jobNameFlag = "job-name"
2223
instanceIdFlag = "instance-id"
24+
filePathFlag = "file-path"
2325
)
2426

2527
type inputModel struct {
2628
*globalflags.GlobalFlagModel
2729
JobName *string
2830
InstanceId string
31+
FilePath *string
2932
}
3033

3134
func NewCmd(p *print.Printer) *cobra.Command {
@@ -44,14 +47,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
4447
Example: examples.Build(
4548
examples.NewExample(
4649
`Generate a Create payload with default values, and adapt it with custom values for the different configuration options`,
47-
`$ stackit argus scrape-config generate-payload > ./payload.json`,
50+
`$ stackit argus scrape-config generate-payload --file-path ./payload.json`,
4851
`<Modify payload in file, if needed>`,
4952
`$ stackit argus scrape-config create my-config --payload @./payload.json`),
5053
examples.NewExample(
5154
`Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and adapt it with custom values for the different configuration options`,
52-
`$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx > ./payload.json`,
55+
`$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx --file-path ./payload.json`,
5356
`<Modify payload in file>`,
5457
`$ stackit argus scrape-config update my-config --payload @./payload.json`),
58+
examples.NewExample(
59+
`Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and preview it in the terminal`,
60+
`$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx`),
5561
),
5662
RunE: func(cmd *cobra.Command, args []string) error {
5763
ctx := context.Background()
@@ -68,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6874

6975
if model.JobName == nil {
7076
createPayload := argusUtils.DefaultCreateScrapeConfigPayload
71-
return outputCreateResult(p, &createPayload)
77+
return outputCreateResult(p, model.FilePath, &createPayload)
7278
}
7379

7480
req := buildRequest(ctx, model, apiClient)
@@ -82,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8288
return fmt.Errorf("map update scrape config payloads: %w", err)
8389
}
8490

85-
return outputUpdateResult(p, payload)
91+
return outputUpdateResult(p, model.FilePath, payload)
8692
},
8793
}
8894
configureFlags(cmd)
@@ -92,6 +98,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9298
func configureFlags(cmd *cobra.Command) {
9399
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
94100
cmd.Flags().StringP(jobNameFlag, "n", "", "If set, generates an update payload with the current state of the given scrape config. If unset, generates a create payload with default values")
101+
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the payload to the given file. If unset, writes the payload to the standard output")
95102
}
96103

97104
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
@@ -108,6 +115,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
108115
GlobalFlagModel: globalFlags,
109116
JobName: jobName,
110117
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
118+
FilePath: flags.FlagToStringPointer(p, cmd, filePathFlag),
111119
}, nil
112120
}
113121

@@ -116,22 +124,38 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl
116124
return req
117125
}
118126

119-
func outputCreateResult(p *print.Printer, payload *argus.CreateScrapeConfigPayload) error {
127+
func outputCreateResult(p *print.Printer, filePath *string, payload *argus.CreateScrapeConfigPayload) error {
120128
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
121129
if err != nil {
122130
return fmt.Errorf("marshal payload: %w", err)
123131
}
124-
p.Outputln(string(payloadBytes))
132+
133+
if filePath != nil {
134+
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
135+
if err != nil {
136+
return fmt.Errorf("write payload to the file: %w", err)
137+
}
138+
} else {
139+
p.Outputln(string(payloadBytes))
140+
}
125141

126142
return nil
127143
}
128144

129-
func outputUpdateResult(p *print.Printer, payload *argus.UpdateScrapeConfigPayload) error {
145+
func outputUpdateResult(p *print.Printer, filePath *string, payload *argus.UpdateScrapeConfigPayload) error {
130146
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
131147
if err != nil {
132148
return fmt.Errorf("marshal payload: %w", err)
133149
}
134-
p.Outputln(string(payloadBytes))
150+
151+
if filePath != nil {
152+
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
153+
if err != nil {
154+
return fmt.Errorf("write payload to the file: %w", err)
155+
}
156+
} else {
157+
p.Outputln(string(payloadBytes))
158+
}
135159

136160
return nil
137161
}

internal/cmd/argus/scrape-config/generate-payload/generate_payload_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ var testClient = &argus.APIClient{}
2222
var testProjectId = uuid.NewString()
2323
var testInstanceId = uuid.NewString()
2424

25-
const testJobName = "test-job-name"
25+
const (
26+
testJobName = "test-job-name"
27+
testFilePath = "example-file"
28+
)
2629

2730
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2831
flagValues := map[string]string{
2932
projectIdFlag: testProjectId,
3033
instanceIdFlag: testInstanceId,
3134
jobNameFlag: testJobName,
35+
filePathFlag: testFilePath,
3236
}
3337
for _, mod := range mods {
3438
mod(flagValues)
@@ -44,6 +48,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4448
},
4549
InstanceId: testInstanceId,
4650
JobName: utils.Ptr(testJobName),
51+
FilePath: utils.Ptr(testFilePath),
4752
}
4853
for _, mod := range mods {
4954
mod(model)
@@ -80,6 +85,16 @@ func TestParseInput(t *testing.T) {
8085
GlobalFlagModel: &globalflags.GlobalFlagModel{Verbosity: globalflags.VerbosityDefault},
8186
},
8287
},
88+
{
89+
description: "file path missing",
90+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
91+
delete(flagValues, filePathFlag)
92+
}),
93+
isValid: true,
94+
expectedModel: fixtureInputModel(func(model *inputModel) {
95+
model.FilePath = nil
96+
}),
97+
},
8398
{
8499
description: "job name missing",
85100
flagValues: fixtureFlagValues(func(flagValues map[string]string) {

internal/cmd/load-balancer/generate-payload/generate_payload.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
99
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1010
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
@@ -20,11 +21,13 @@ import (
2021

2122
const (
2223
loadBalancerNameFlag = "lb-name"
24+
filePathFlag = "file-path"
2325
)
2426

2527
type inputModel struct {
2628
*globalflags.GlobalFlagModel
2729
LoadBalancerName *string
30+
FilePath *string
2831
}
2932

3033
var (
@@ -118,14 +121,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
118121
Example: examples.Build(
119122
examples.NewExample(
120123
`Generate a payload, and adapt it with custom values for the different configuration options`,
121-
`$ stackit load-balancer generate-payload > ./payload.json`,
124+
`$ stackit load-balancer generate-payload --file-path ./payload.json`,
122125
`<Modify payload in file, if needed>`,
123126
`$ stackit load-balancer create --payload @./payload.json`),
124127
examples.NewExample(
125128
`Generate a payload with values of an existing load balancer, and adapt it with custom values for the different configuration options`,
126-
`$ stackit load-balancer generate-payload --lb-name xxx > ./payload.json`,
129+
`$ stackit load-balancer generate-payload --lb-name xxx --file-path ./payload.json`,
127130
`<Modify payload in file>`,
128131
`$ stackit load-balancer update xxx --payload @./payload.json`),
132+
examples.NewExample(
133+
`Generate a payload with values of an existing load balancer, and preview it in the terminal`,
134+
`$ stackit load-balancer generate-payload --lb-name xxx`),
129135
),
130136
RunE: func(cmd *cobra.Command, args []string) error {
131137
ctx := context.Background()
@@ -142,7 +148,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
142148

143149
if model.LoadBalancerName == nil {
144150
createPayload := DefaultCreateLoadBalancerPayload
145-
return outputCreateResult(p, &createPayload)
151+
return outputCreateResult(p, model.FilePath, &createPayload)
146152
}
147153

148154
req := buildRequest(ctx, model, apiClient)
@@ -162,7 +168,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
162168
TargetPools: resp.TargetPools,
163169
Version: resp.Version,
164170
}
165-
return outputUpdateResult(p, updatePayload)
171+
return outputUpdateResult(p, model.FilePath, updatePayload)
166172
},
167173
}
168174
configureFlags(cmd)
@@ -171,6 +177,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
171177

172178
func configureFlags(cmd *cobra.Command) {
173179
cmd.Flags().StringP(loadBalancerNameFlag, "n", "", "If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values")
180+
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the payload to the given file. If unset, writes the payload to the standard output")
174181
}
175182

176183
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
@@ -185,6 +192,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
185192
model := inputModel{
186193
GlobalFlagModel: globalFlags,
187194
LoadBalancerName: loadBalancerName,
195+
FilePath: flags.FlagToStringPointer(p, cmd, filePathFlag),
188196
}
189197

190198
if p.IsVerbosityDebug() {
@@ -204,22 +212,38 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
204212
return req
205213
}
206214

207-
func outputCreateResult(p *print.Printer, payload *loadbalancer.CreateLoadBalancerPayload) error {
215+
func outputCreateResult(p *print.Printer, filePath *string, payload *loadbalancer.CreateLoadBalancerPayload) error {
208216
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
209217
if err != nil {
210218
return fmt.Errorf("marshal create load balancer payload: %w", err)
211219
}
212-
p.Outputln(string(payloadBytes))
220+
221+
if filePath != nil {
222+
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
223+
if err != nil {
224+
return fmt.Errorf("write create load balancer payload to the file: %w", err)
225+
}
226+
} else {
227+
p.Outputln(string(payloadBytes))
228+
}
213229

214230
return nil
215231
}
216232

217-
func outputUpdateResult(p *print.Printer, payload *loadbalancer.UpdateLoadBalancerPayload) error {
233+
func outputUpdateResult(p *print.Printer, filePath *string, payload *loadbalancer.UpdateLoadBalancerPayload) error {
218234
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
219235
if err != nil {
220236
return fmt.Errorf("marshal update load balancer payload: %w", err)
221237
}
222-
p.Outputln(string(payloadBytes))
238+
239+
if filePath != nil {
240+
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
241+
if err != nil {
242+
return fmt.Errorf("write update load balancer payload to the file: %w", err)
243+
}
244+
} else {
245+
p.Outputln(string(payloadBytes))
246+
}
223247

224248
return nil
225249
}

0 commit comments

Comments
 (0)