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 --newline for block new #109

Merged
merged 1 commit into from
Feb 4, 2025
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: 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