diff --git a/cmd/hcledit/internal/command/create.go b/cmd/hcledit/internal/command/create.go index c09df7a..b5a1e43 100644 --- a/cmd/hcledit/internal/command/create.go +++ b/cmd/hcledit/internal/command/create.go @@ -10,8 +10,9 @@ import ( ) type CreateOptions struct { - Type string - After string + Type string + After string + Comment string } func NewCmdCreate() *cobra.Command { @@ -31,6 +32,7 @@ func NewCmdCreate() *cobra.Command { cmd.Flags().StringVarP(&opts.Type, "type", "t", "string", "Type of the value") cmd.Flags().StringVarP(&opts.After, "after", "a", "", "Field key which before the value will be created") + cmd.Flags().StringVarP(&opts.Comment, "comment", "c", "", "Comment to be inserted before the field added. Comment symbols like // are required") return cmd } @@ -49,7 +51,7 @@ func runCreate(opts *CreateOptions, args []string) error { return fmt.Errorf("failed to convert input to specific type: %s", err) } - if err := editor.Create(query, value, hcledit.WithAfter(opts.After)); err != nil { + if err := editor.Create(query, value, hcledit.WithAfter(opts.After), hcledit.WithComment(opts.Comment)); err != nil { return fmt.Errorf("failed to create: %s", err) } diff --git a/cmd/hcledit/internal/command/create_test.go b/cmd/hcledit/internal/command/create_test.go index 2793bdf..c457332 100644 --- a/cmd/hcledit/internal/command/create_test.go +++ b/cmd/hcledit/internal/command/create_test.go @@ -12,10 +12,8 @@ func TestRunCreate(t *testing.T) { want string wantErr bool }{ - "WithoutOptionWithAfter": { - opts: &CreateOptions{ - Type: "string", - }, + "WithoutAdditionalOptions": { + opts: &CreateOptions{}, want: `resource "google_container_node_pool" "nodes1" { node_config { preemptible = false @@ -37,6 +35,20 @@ func TestRunCreate(t *testing.T) { machine_type = "e2-medium" } } +`, + }, + "WithOptionComment": { + opts: &CreateOptions{ + Comment: "// TODO: Testing", + }, + want: `resource "google_container_node_pool" "nodes1" { + node_config { + preemptible = false + machine_type = "e2-medium" + // TODO: Testing + disk_size_gb = "100" + } +} `, }, } @@ -52,6 +64,7 @@ func TestRunCreate(t *testing.T) { } `) + tc.opts.Type = "string" // ensure default value if err := runCreate(tc.opts, []string{ "resource.google_container_node_pool.nodes1.node_config.disk_size_gb", "100",