Skip to content

Commit

Permalink
refactor(generate): add outputter
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Nov 2, 2022
1 parent ec75382 commit 012273d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
48 changes: 30 additions & 18 deletions pkg/controller/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/logrus-error/logerr"
"gopkg.in/yaml.v2"
)

type Controller struct {
Expand All @@ -33,6 +32,7 @@ type Controller struct {
fuzzyFinder FuzzyFinder
versionSelector VersionSelector
fs afero.Fs
outputter Outputter
}

type RepositoriesService interface {
Expand All @@ -45,6 +45,10 @@ type ConfigFinder interface {
Find(wd, configFilePath string, globalConfigFilePaths ...string) (string, error)
}

type Outputter interface {
Output(param *OutputParam) error
}

func New(configFinder ConfigFinder, configReader domain.ConfigReader, registInstaller domain.RegistryInstaller, gh RepositoriesService, fs afero.Fs, fuzzyFinder FuzzyFinder, versionSelector VersionSelector) *Controller {
return &Controller{
stdin: os.Stdin,
Expand All @@ -56,13 +60,31 @@ func New(configFinder ConfigFinder, configReader domain.ConfigReader, registInst
fs: fs,
fuzzyFinder: fuzzyFinder,
versionSelector: versionSelector,
outputter: &outputter{
fs: fs,
stdout: os.Stdout,
},
}
}

// Generate searches packages in registries and outputs the configuration to standard output.
// If no package is specified, the interactive fuzzy finder is launched.
// If the package supports, the latest version is gotten by GitHub API.
func (ctrl *Controller) Generate(ctx context.Context, logE *logrus.Entry, param *config.Param, args ...string) error { //nolint:cyclop
func (ctrl *Controller) Generate(ctx context.Context, logE *logrus.Entry, param *config.Param, args ...string) error {
// Find and read a configuration file (aqua.yaml).
// Install registries
// List outputted packages
// Get packages by fuzzy finder or from file or from arguments
// Get versions
// Get versions from arguments or GitHub API (GitHub Tag or GitHub Release) or fuzzy finder (-s)
// Output packages
// Format outputs
// registry:
// omit standard registry
// version:
// merge version with package name
// set default value
// Output to Stdout or Update aqua.yaml (-i)
cfgFilePath, err := ctrl.configFinder.Find(param.PWD, param.ConfigFilePath, param.GlobalConfigFilePaths...)
if err != nil {
return err //nolint:wrapcheck
Expand All @@ -81,23 +103,13 @@ func (ctrl *Controller) Generate(ctx context.Context, logE *logrus.Entry, param
if len(list) == 0 {
return nil
}
if !param.Insert && param.Dest == "" {
if err := yaml.NewEncoder(ctrl.stdout).Encode(list); err != nil {
return fmt.Errorf("output generated package configuration: %w", err)
}
return nil
}

if param.Dest != "" {
if _, err := ctrl.fs.Stat(param.Dest); err != nil {
if err := afero.WriteFile(ctrl.fs, param.Dest, []byte("packages:\n\n"), 0o644); err != nil { //nolint:gomnd
return fmt.Errorf("create a file: %w", err)
}
}
return ctrl.generateInsert(param.Dest, list) //nolint:contextcheck
}

return ctrl.generateInsert(cfgFilePath, list) //nolint:contextcheck
return ctrl.outputter.Output(&OutputParam{ //nolint:wrapcheck
Insert: param.Insert,
Dest: param.Dest,
List: list,
ConfigFilePath: cfgFilePath,
})
}

func excludeDuplicatedPkgs(logE *logrus.Entry, cfg *aqua.Config, pkgs []*aqua.Package) []*aqua.Package {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/spf13/afero"
)

func (ctrl *Controller) generateInsert(cfgFilePath string, pkgs []*aqua.Package) error {
b, err := afero.ReadFile(ctrl.fs, cfgFilePath)
func (out *outputter) generateInsert(cfgFilePath string, pkgs []*aqua.Package) error {
b, err := afero.ReadFile(out.fs, cfgFilePath)
if err != nil {
return fmt.Errorf("read a configuration file: %w", err)
}
Expand All @@ -21,21 +21,21 @@ func (ctrl *Controller) generateInsert(cfgFilePath string, pkgs []*aqua.Package)
return fmt.Errorf("parse configuration file as YAML: %w", err)
}

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

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

func (ctrl *Controller) updateASTFile(file *ast.File, pkgs []*aqua.Package) error { //nolint:cyclop
func updateASTFile(file *ast.File, pkgs []*aqua.Package) error { //nolint:cyclop
node, err := yaml.ValueToNode(pkgs)
if err != nil {
return fmt.Errorf("convert packages to node: %w", err)
Expand Down
42 changes: 42 additions & 0 deletions pkg/controller/generate/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package generate

import (
"fmt"
"io"

"github.com/aquaproj/aqua/pkg/config/aqua"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)

type outputter struct {
stdout io.Writer
fs afero.Fs
}

type OutputParam struct {
Insert bool
Dest string
List []*aqua.Package
ConfigFilePath string
}

func (out *outputter) Output(param *OutputParam) error {
if !param.Insert && param.Dest == "" {
if err := yaml.NewEncoder(out.stdout).Encode(param.List); err != nil {
return fmt.Errorf("output generated package configuration: %w", err)
}
return nil
}

if param.Dest != "" {
if _, err := out.fs.Stat(param.Dest); err != nil {
if err := afero.WriteFile(out.fs, param.Dest, []byte("packages:\n\n"), 0o644); err != nil { //nolint:gomnd
return fmt.Errorf("create a file: %w", err)
}
}
return out.generateInsert(param.Dest, param.List)
}

return out.generateInsert(param.ConfigFilePath, param.List)
}

0 comments on commit 012273d

Please sign in to comment.