diff --git a/.github/workflows/wc-integration-test.yaml b/.github/workflows/wc-integration-test.yaml index bdafdc887..7d1a359d6 100644 --- a/.github/workflows/wc-integration-test.yaml +++ b/.github/workflows/wc-integration-test.yaml @@ -84,6 +84,13 @@ jobs: - name: output fish completion run: aqua completion fish + - name: Test aqua g -i + run: aqua g -i suzuki-shunsuke/ghalint@v1.0.0 suzuki-shunsuke/ci-info@v2.3.1 suzuki-shunsuke/ghd2i@v0.1.2 + 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 diff --git a/pkg/controller/generate/output/insert.go b/pkg/controller/generate/output/insert.go index 893a28902..005543d58 100644 --- a/pkg/controller/generate/output/insert.go +++ b/pkg/controller/generate/output/insert.go @@ -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"` diff --git a/tests/insert/aqua.yaml b/tests/insert/aqua.yaml new file mode 100644 index 000000000..f180c0be2 --- /dev/null +++ b/tests/insert/aqua.yaml @@ -0,0 +1,10 @@ +registries: + - type: standard + ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry + +packages: + # foo + - name: suzuki-shunsuke/mkghtag@v0.1.4 + # hello + - name: suzuki-shunsuke/tfcmt@v4.14.0 + - name: suzuki-shunsuke/pinact@v1.0.0 diff --git a/tests/insert/expect.yaml b/tests/insert/expect.yaml new file mode 100644 index 000000000..85ab775df --- /dev/null +++ b/tests/insert/expect.yaml @@ -0,0 +1,13 @@ +registries: + - type: standard + ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry + +packages: + # foo + - name: suzuki-shunsuke/mkghtag@v0.1.4 + # hello + - name: suzuki-shunsuke/tfcmt@v4.14.0 + - name: suzuki-shunsuke/pinact@v1.0.0 + - name: suzuki-shunsuke/ghalint@v1.0.0 + - name: suzuki-shunsuke/ci-info@v2.3.1 + - name: suzuki-shunsuke/ghd2i@v0.1.2