Skip to content

Commit

Permalink
Merge pull request #109 from niraj8/newline-block-new-filter
Browse files Browse the repository at this point in the history
add --newline for block new
  • Loading branch information
minamijoyo authored Feb 4, 2025
2 parents af62373 + 0495605 commit 3aad3c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion cmd/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
7 changes: 6 additions & 1 deletion editor/filter_block_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
13 changes: 9 additions & 4 deletions editor/filter_block_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -44,6 +47,7 @@ variable "var1" {
`,
blockType: "module",
labels: []string{"example"},
newline: false,
want: `
variable "var1" {
type = string
Expand All @@ -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
Expand All @@ -65,6 +69,7 @@ variable "var1" {
`,
blockType: "locals",
labels: []string{},
newline: false,
want: `
variable "var1" {
type = string
Expand All @@ -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)
Expand Down

0 comments on commit 3aad3c2

Please sign in to comment.