Skip to content

Commit

Permalink
fix: fix a bug that aqua g -i removes comments in packages (#3325)
Browse files Browse the repository at this point in the history
* fix: fix a bug that aqua g -i removes comments in packages

* fix: fix a lint error
  • Loading branch information
suzuki-shunsuke authored Dec 4, 2024
1 parent 8a51363 commit 4bc3490
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/wc-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ jobs:
- name: output fish completion
run: aqua completion fish

- name: Test aqua g -i
run: aqua g -i suzuki-shunsuke/[email protected] suzuki-shunsuke/[email protected] suzuki-shunsuke/[email protected]
working-directory: tests/insert
- name: compare aqua.yaml and expect.yaml
run: diff aqua.yaml expect.yaml
working-directory: tests/insert

- run: aqua g -i suzuki-shunsuke/tfcmt
working-directory: tests/main
- name: add duplicated package
Expand Down
29 changes: 16 additions & 13 deletions pkg/controller/generate/output/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,33 @@ func (o *Outputter) getAppendedTxt(cfgFilePath string, file *ast.File, pkgs []*a
return file.String(), nil
}

func appendPkgsNode(mapValue *ast.MappingValueNode, node ast.Node) error {
switch mapValue.Value.Type() {
func updateASTFile(values *ast.MappingValueNode, pkgs []*aqua.Package) error {
node, err := yaml.ValueToNode(pkgs)
if err != nil {
return fmt.Errorf("convert packages to node: %w", err)
}

switch values.Value.Type() {
case ast.NullType:
mapValue.Value = node
values.Value = node
return nil
case ast.SequenceType:
if err := ast.Merge(mapValue.Value, node); err != nil {
if err := ast.Merge(values.Value, node); err != nil {
return fmt.Errorf("merge packages: %w", err)
}
seq, ok := values.Value.(*ast.SequenceNode)
if ok {
for range pkgs {
// https://github.com/goccy/go-yaml/issues/502#issuecomment-2515981600
seq.ValueHeadComments = append(seq.ValueHeadComments, nil)
}
}
return nil
default:
return errors.New("packages must be null or array")
}
}

func updateASTFile(values *ast.MappingValueNode, pkgs []*aqua.Package) error {
node, err := yaml.ValueToNode(pkgs)
if err != nil {
return fmt.Errorf("convert packages to node: %w", err)
}

return appendPkgsNode(values, node)
}

func (o *Outputter) appendPkgsTxt(cfgFilePath string, pkgs []*aqua.Package) (string, error) {
a, err := yaml.Marshal(struct {
Packages []*aqua.Package `yaml:"packages"`
Expand Down
10 changes: 10 additions & 0 deletions tests/insert/aqua.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
registries:
- type: standard
ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry

packages:
# foo
- name: suzuki-shunsuke/[email protected]
# hello
- name: suzuki-shunsuke/[email protected]
- name: suzuki-shunsuke/[email protected]
13 changes: 13 additions & 0 deletions tests/insert/expect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
registries:
- type: standard
ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry

packages:
# foo
- name: suzuki-shunsuke/[email protected]
# hello
- name: suzuki-shunsuke/[email protected]
- name: suzuki-shunsuke/[email protected]
- name: suzuki-shunsuke/[email protected]
- name: suzuki-shunsuke/[email protected]
- name: suzuki-shunsuke/[email protected]

0 comments on commit 4bc3490

Please sign in to comment.