Skip to content

Commit

Permalink
Fix a bug that aqua g -i fails if aqua.yaml doesn't have the field …
Browse files Browse the repository at this point in the history
…`packages` (#2904)

* fix(generate): fix a bug that aqua g -i fails if aqua.yaml doesn't have the field packages

* fix: remove useless codes

* refactor: refactoring
  • Loading branch information
suzuki-shunsuke authored May 18, 2024
1 parent 5f9df45 commit 4ba02d7
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions pkg/controller/generate/output/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package output
import (
"errors"
"fmt"
"strings"

wast "github.com/aquaproj/aqua/v2/pkg/ast"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
Expand All @@ -29,21 +30,37 @@ func (o *Outputter) generateInsert(cfgFilePath string, pkgs []*aqua.Package) err
"num_of_docs": len(file.Docs),
})
}

if err := updateASTFile(file.Docs[0].Body, pkgs); err != nil {
s, err := o.getAppendedTxt(cfgFilePath, file, pkgs)
if err != nil {
return err
}

stat, err := o.fs.Stat(cfgFilePath)
if err != nil {
return fmt.Errorf("get configuration file stat: %w", err)
}
if err := afero.WriteFile(o.fs, cfgFilePath, []byte(file.String()), stat.Mode()); err != nil {
if err := afero.WriteFile(o.fs, cfgFilePath, []byte(s), stat.Mode()); err != nil {
return fmt.Errorf("write the configuration file: %w", err)
}
return nil
}

func (o *Outputter) getAppendedTxt(cfgFilePath string, file *ast.File, pkgs []*aqua.Package) (string, error) {
body := file.Docs[0].Body
values, err := wast.FindMappingValueFromNode(body, "packages")
if err != nil {
return "", fmt.Errorf(`find a mapping value node "packages": %w`, err)
}
if values == nil {
return o.appendPkgsTxt(cfgFilePath, pkgs)
}

if err := updateASTFile(values, pkgs); err != nil {
return "", err
}
return file.String(), nil
}

func appendPkgsNode(mapValue *ast.MappingValueNode, node ast.Node) error {
switch mapValue.Value.Type() {
case ast.NullType:
Expand All @@ -59,16 +76,31 @@ func appendPkgsNode(mapValue *ast.MappingValueNode, node ast.Node) error {
}
}

func updateASTFile(body ast.Node, pkgs []*aqua.Package) error {
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)
}

values, err := wast.FindMappingValueFromNode(body, "packages")
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"`
}{
Packages: pkgs,
})
if err != nil {
return fmt.Errorf(`find a mapping value node "packages": %w`, err)
return "", fmt.Errorf("marshal packages: %w", err)
}

return appendPkgsNode(values, node)
b, err := afero.ReadFile(o.fs, cfgFilePath)
if err != nil {
return "", fmt.Errorf("read a configuration file: %w", err)
}
sb := string(b)
if !strings.HasSuffix(sb, "\n") {
sb += "\n"
}
return sb + string(a), nil
}

0 comments on commit 4ba02d7

Please sign in to comment.