Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --comment flag to create subcommand #30

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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