Skip to content

Commit 144b2b0

Browse files
author
Daniele Marostica
committed
added optional --message flag
1 parent cbe11c7 commit 144b2b0

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

internal/clioptions/clioptions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type CLIOptions struct {
8989
ShowServiceAccounts bool
9090

9191
ResolveExtensionsDetails bool
92+
93+
Message string
9294
}
9395

9496
// NewCLIOptions return a new CLIOptions instance

internal/cmd/project/apply.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type applyProjectOptions struct {
4242
ProjectID string
4343
RevisionName string
4444
FilePath string
45+
Title string
4546
}
4647

4748
// ApplyCmd returns a cobra command for applying a project configuration
@@ -61,6 +62,7 @@ func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command {
6162
RevisionName: options.Revision,
6263
ProjectID: restConfig.ProjectID,
6364
FilePath: options.InputFilePath,
65+
Title: options.Message,
6466
}
6567

6668
return handleApplyProjectConfigurationCmd(cmd.Context(), client, cmdOptions)
@@ -71,6 +73,9 @@ func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command {
7173
options.AddProjectFlags(flags)
7274
options.AddRevisionFlags(flags)
7375

76+
flags.StringVarP(&options.Message, "message", "m", "", "the message to use when saving the configuration")
77+
78+
// file path flag is required
7479
flags.StringVarP(&options.InputFilePath, "file", "f", "", "path to JSON/YAML file containing the project configuration")
7580
if err := cmd.MarkFlagRequired("file"); err != nil {
7681
panic(err)
@@ -126,11 +131,10 @@ func applyConfiguration(ctx context.Context, client *client.APIClient, options a
126131
return fmt.Errorf("cannot parse project configuration: %w", err)
127132
}
128133

129-
previousSnapshotID := structuredConfig.Config["commitId"].(string)
130-
applyConfig := configuration.ApplyRequest{
131-
Configuration: structuredConfig,
132-
Title: "[miactl] Applied project configuration",
133-
PreviousSave: previousSnapshotID,
134+
applyConfig := configuration.BuildApplyRequest(structuredConfig)
135+
136+
if options.Title != "" {
137+
applyConfig = applyConfig.WithTitle(options.Title)
134138
}
135139

136140
body, err := resources.EncodeResourceToJSON(applyConfig)

internal/cmd/project/apply_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ func TestApplyProjectCmd(t *testing.T) {
232232
return false
233233
}),
234234
},
235+
"with custom title": {
236+
options: applyProjectOptions{
237+
ProjectID: "test-project",
238+
RevisionName: "test-revision",
239+
FilePath: "testdata/base-config.json",
240+
Title: "apply config custom title",
241+
},
242+
testServer: applyTestServer(t, func(w http.ResponseWriter, r *http.Request) bool {
243+
if r.URL.Path == "/api/backend/projects/test-project/revisions/test-revision/configuration" && r.Method == http.MethodPost {
244+
var requestBody map[string]any
245+
err := json.NewDecoder(r.Body).Decode(&requestBody)
246+
require.NoError(t, err)
247+
248+
assert.Contains(t, requestBody, "title")
249+
assert.Equal(t, "apply config custom title", requestBody["title"])
250+
251+
w.WriteHeader(http.StatusOK)
252+
w.Write([]byte(`{}`))
253+
254+
return true
255+
}
256+
257+
return false
258+
}),
259+
},
235260
}
236261

237262
for name, testCase := range testCases {

internal/resources/configuration/applyrequest.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,32 @@
1515

1616
package configuration
1717

18+
const (
19+
DefaultTitle = "[miactl] Applied project configuration"
20+
)
21+
1822
type ApplyRequest struct {
1923
Title string `json:"title" yaml:"title"`
2024
PreviousSave string `json:"previousSave,omitempty" yaml:"previousSave,omitempty"`
2125
*Configuration
2226
}
27+
28+
func BuildApplyRequest(config *Configuration) *ApplyRequest {
29+
req := &ApplyRequest{
30+
Configuration: config,
31+
}
32+
33+
return req.
34+
withPreviousSnapshotID(config.Config["commitId"].(string)).
35+
WithTitle(DefaultTitle)
36+
}
37+
38+
func (r *ApplyRequest) WithTitle(title string) *ApplyRequest {
39+
r.Title = title
40+
return r
41+
}
42+
43+
func (r *ApplyRequest) withPreviousSnapshotID(previousSnapshotID string) *ApplyRequest {
44+
r.PreviousSave = previousSnapshotID
45+
return r
46+
}

0 commit comments

Comments
 (0)