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

fix: fix a bug that aqua g -i removes comments in packages #3325

Merged
merged 2 commits into from
Dec 4, 2024
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
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]
Loading