Skip to content

Commit

Permalink
Add --comment flag to create subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim committed Feb 9, 2021
1 parent f7d7b2a commit 37f3fa1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cmd/hcledit/internal/command/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type CreateOptions struct {
Type string
After string
Type string
After string
Comment string
}

func NewCmdCreate() *cobra.Command {
Expand All @@ -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
}

Expand All @@ -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)
}

Expand Down
21 changes: 17 additions & 4 deletions cmd/hcledit/internal/command/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
}
`,
},
}
Expand All @@ -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",
Expand Down

0 comments on commit 37f3fa1

Please sign in to comment.