From 049560584487af8f4aa7171507366cbce9762814 Mon Sep 17 00:00:00 2001 From: Niraj Palecha Date: Tue, 4 Feb 2025 15:09:39 +0530 Subject: [PATCH] add --newline for block new --- cmd/block.go | 8 +++++++- editor/filter_block_new.go | 7 ++++++- editor/filter_block_new_test.go | 13 +++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/block.go b/cmd/block.go index 3d6a87e..421d235 100644 --- a/cmd/block.go +++ b/cmd/block.go @@ -197,6 +197,11 @@ Arguments: `, RunE: runBlockNewCmd, } + + flags := cmd.Flags() + flags.Bool("newline", false, "Append a new line before a new child block") + _ = viper.BindPFlag("block.new.newline", flags.Lookup("newline")) + return cmd } @@ -209,8 +214,9 @@ func runBlockNewCmd(cmd *cobra.Command, args []string) error { labels := args[1:] file := viper.GetString("file") update := viper.GetBool("update") + newline := viper.GetBool("block.new.newline") - filter := editor.NewBlockNewFilter(blockType, labels) + filter := editor.NewBlockNewFilter(blockType, labels, newline) c := newDefaultClient(cmd) return c.Edit(file, update, filter) } diff --git a/editor/filter_block_new.go b/editor/filter_block_new.go index 45e9c79..f87d5d8 100644 --- a/editor/filter_block_new.go +++ b/editor/filter_block_new.go @@ -6,19 +6,24 @@ import "github.com/hashicorp/hcl/v2/hclwrite" type BlockNewFilter struct { blockType string labels []string + newline bool } // Filter reads HCL and creates a new block with the given type and labels. func (b *BlockNewFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error) { + if b.newline { + inFile.Body().AppendNewline() + } inFile.Body().AppendNewBlock(b.blockType, b.labels) return inFile, nil } var _ Filter = (*BlockNewFilter)(nil) -func NewBlockNewFilter(blockType string, labels []string) Filter { +func NewBlockNewFilter(blockType string, labels []string, newline bool) Filter { return &BlockNewFilter{ blockType: blockType, labels: labels, + newline: newline, } } diff --git a/editor/filter_block_new_test.go b/editor/filter_block_new_test.go index b9606cb..bb464ad 100644 --- a/editor/filter_block_new_test.go +++ b/editor/filter_block_new_test.go @@ -10,10 +10,11 @@ func TestBlockNewFilter(t *testing.T) { src string blockType string labels []string + newline bool want string }{ { - name: "block with blockType and 2 labels, resource", + name: "block with blockType and 2 labels, resource with newline", src: ` variable "var1" { type = string @@ -23,18 +24,20 @@ variable "var1" { `, blockType: "resource", labels: []string{"aws_instance", "example"}, + newline: true, want: ` variable "var1" { type = string default = "foo" description = "example variable" } + resource "aws_instance" "example" { } `, }, { - name: "block with blockType and 1 label, module", + name: "block with blockType and 1 label, module without newline", src: ` variable "var1" { type = string @@ -44,6 +47,7 @@ variable "var1" { `, blockType: "module", labels: []string{"example"}, + newline: false, want: ` variable "var1" { type = string @@ -55,7 +59,7 @@ module "example" { `, }, { - name: "block with blockType and 0 labels, locals", + name: "block with blockType and 0 labels, locals without newline", src: ` variable "var1" { type = string @@ -65,6 +69,7 @@ variable "var1" { `, blockType: "locals", labels: []string{}, + newline: false, want: ` variable "var1" { type = string @@ -79,7 +84,7 @@ locals { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - o := NewEditOperator(NewBlockNewFilter(tc.blockType, tc.labels)) + o := NewEditOperator(NewBlockNewFilter(tc.blockType, tc.labels, tc.newline)) output, err := o.Apply([]byte(tc.src), "test") if err != nil { t.Fatalf("unexpected err = %s", err)