Skip to content

Commit 3aad3c2

Browse files
authored
Merge pull request #109 from niraj8/newline-block-new-filter
add --newline for block new
2 parents af62373 + 0495605 commit 3aad3c2

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

cmd/block.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ Arguments:
197197
`,
198198
RunE: runBlockNewCmd,
199199
}
200+
201+
flags := cmd.Flags()
202+
flags.Bool("newline", false, "Append a new line before a new child block")
203+
_ = viper.BindPFlag("block.new.newline", flags.Lookup("newline"))
204+
200205
return cmd
201206
}
202207

@@ -209,8 +214,9 @@ func runBlockNewCmd(cmd *cobra.Command, args []string) error {
209214
labels := args[1:]
210215
file := viper.GetString("file")
211216
update := viper.GetBool("update")
217+
newline := viper.GetBool("block.new.newline")
212218

213-
filter := editor.NewBlockNewFilter(blockType, labels)
219+
filter := editor.NewBlockNewFilter(blockType, labels, newline)
214220
c := newDefaultClient(cmd)
215221
return c.Edit(file, update, filter)
216222
}

editor/filter_block_new.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ import "github.com/hashicorp/hcl/v2/hclwrite"
66
type BlockNewFilter struct {
77
blockType string
88
labels []string
9+
newline bool
910
}
1011

1112
// Filter reads HCL and creates a new block with the given type and labels.
1213
func (b *BlockNewFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error) {
14+
if b.newline {
15+
inFile.Body().AppendNewline()
16+
}
1317
inFile.Body().AppendNewBlock(b.blockType, b.labels)
1418
return inFile, nil
1519
}
1620

1721
var _ Filter = (*BlockNewFilter)(nil)
1822

19-
func NewBlockNewFilter(blockType string, labels []string) Filter {
23+
func NewBlockNewFilter(blockType string, labels []string, newline bool) Filter {
2024
return &BlockNewFilter{
2125
blockType: blockType,
2226
labels: labels,
27+
newline: newline,
2328
}
2429
}

editor/filter_block_new_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ func TestBlockNewFilter(t *testing.T) {
1010
src string
1111
blockType string
1212
labels []string
13+
newline bool
1314
want string
1415
}{
1516
{
16-
name: "block with blockType and 2 labels, resource",
17+
name: "block with blockType and 2 labels, resource with newline",
1718
src: `
1819
variable "var1" {
1920
type = string
@@ -23,18 +24,20 @@ variable "var1" {
2324
`,
2425
blockType: "resource",
2526
labels: []string{"aws_instance", "example"},
27+
newline: true,
2628
want: `
2729
variable "var1" {
2830
type = string
2931
default = "foo"
3032
description = "example variable"
3133
}
34+
3235
resource "aws_instance" "example" {
3336
}
3437
`,
3538
},
3639
{
37-
name: "block with blockType and 1 label, module",
40+
name: "block with blockType and 1 label, module without newline",
3841
src: `
3942
variable "var1" {
4043
type = string
@@ -44,6 +47,7 @@ variable "var1" {
4447
`,
4548
blockType: "module",
4649
labels: []string{"example"},
50+
newline: false,
4751
want: `
4852
variable "var1" {
4953
type = string
@@ -55,7 +59,7 @@ module "example" {
5559
`,
5660
},
5761
{
58-
name: "block with blockType and 0 labels, locals",
62+
name: "block with blockType and 0 labels, locals without newline",
5963
src: `
6064
variable "var1" {
6165
type = string
@@ -65,6 +69,7 @@ variable "var1" {
6569
`,
6670
blockType: "locals",
6771
labels: []string{},
72+
newline: false,
6873
want: `
6974
variable "var1" {
7075
type = string
@@ -79,7 +84,7 @@ locals {
7984

8085
for _, tc := range cases {
8186
t.Run(tc.name, func(t *testing.T) {
82-
o := NewEditOperator(NewBlockNewFilter(tc.blockType, tc.labels))
87+
o := NewEditOperator(NewBlockNewFilter(tc.blockType, tc.labels, tc.newline))
8388
output, err := o.Apply([]byte(tc.src), "test")
8489
if err != nil {
8590
t.Fatalf("unexpected err = %s", err)

0 commit comments

Comments
 (0)