From 4df269e479ac3364a3430ce77d52572cf01648e5 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 10 Mar 2024 21:53:48 +0900 Subject: [PATCH] feat(list): support outputting installed packages (#2733) * feat(list): support outputting installed packages * feat(list): change the order and separator --- .github/workflows/wc-integration-test.yaml | 3 ++ pkg/cli/list.go | 23 ++++++++++ pkg/cli/runner.go | 1 + pkg/config/package.go | 1 + pkg/controller/list/interface.go | 1 + pkg/controller/list/list.go | 3 ++ pkg/controller/list/list_installed.go | 49 ++++++++++++++++++++++ 7 files changed, 81 insertions(+) create mode 100644 pkg/controller/list/list_installed.go diff --git a/.github/workflows/wc-integration-test.yaml b/.github/workflows/wc-integration-test.yaml index e4d5f4acc..0c5cec4bb 100644 --- a/.github/workflows/wc-integration-test.yaml +++ b/.github/workflows/wc-integration-test.yaml @@ -54,6 +54,9 @@ jobs: GITHUB_TOKEN: ${{steps.token.outputs.token}} - run: aqua list + - run: aqua list -installed + - run: aqua list -installed -a + - run: aqua update-checksum working-directory: tests/main env: diff --git a/pkg/cli/list.go b/pkg/cli/list.go index b75b8f8ab..63108d0dc 100644 --- a/pkg/cli/list.go +++ b/pkg/cli/list.go @@ -23,7 +23,30 @@ standard,99designs/aws-vault standard,abiosoft/colima standard,abs-lang/abs ... + +If the option -installed is set, the command lists only installed packages. + +$ aqua list -installed +standard,golangci/golangci-lint,v1.56.2 +standard,goreleaser/goreleaser,v1.24.0 +... + +By default, the command doesn't list global configuration packages. +If you want to list global configuration packages too, please set the option -a. + +$ aqua list -installed -a `, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "installed", + Usage: "List installed packages", + }, + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "List global configuration packages too", + }, + }, } } diff --git a/pkg/cli/runner.go b/pkg/cli/runner.go index 6bd7c6026..2f98cb487 100644 --- a/pkg/cli/runner.go +++ b/pkg/cli/runner.go @@ -56,6 +56,7 @@ func (r *Runner) setParam(c *cli.Context, commandName string, param *config.Para param.SLSADisabled = c.Bool("disable-slsa") param.Limit = c.Int("limit") param.SelectVersion = c.Bool("select-version") + param.Installed = c.Bool("installed") param.ShowVersion = c.Bool("version") param.File = c.String("f") if cmd := c.String("cmd"); cmd != "" { diff --git a/pkg/config/package.go b/pkg/config/package.go index fc87d54e8..f3b5f55e8 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -276,6 +276,7 @@ type Param struct { OnlyRegistry bool CosignDisabled bool SLSADisabled bool + Installed bool PolicyConfigFilePaths []string Commands []string } diff --git a/pkg/controller/list/interface.go b/pkg/controller/list/interface.go index 6b6821a68..8d453a806 100644 --- a/pkg/controller/list/interface.go +++ b/pkg/controller/list/interface.go @@ -2,4 +2,5 @@ package list type ConfigFinder interface { Find(wd, configFilePath string, globalConfigFilePaths ...string) (string, error) + Finds(wd, configFilePath string) []string } diff --git a/pkg/controller/list/list.go b/pkg/controller/list/list.go index fec43c815..8d45e621a 100644 --- a/pkg/controller/list/list.go +++ b/pkg/controller/list/list.go @@ -11,6 +11,9 @@ import ( ) func (c *Controller) List(ctx context.Context, param *config.Param, logE *logrus.Entry) error { //nolint:cyclop + if param.Installed { + return c.listInstalled(param, logE) + } cfg := &aqua.Config{} cfgFilePath, err := c.configFinder.Find(param.PWD, param.ConfigFilePath, param.GlobalConfigFilePaths...) if err != nil { diff --git a/pkg/controller/list/list_installed.go b/pkg/controller/list/list_installed.go new file mode 100644 index 000000000..ffe0ac07c --- /dev/null +++ b/pkg/controller/list/list_installed.go @@ -0,0 +1,49 @@ +package list + +import ( + "fmt" + + "github.com/aquaproj/aqua/v2/pkg/config" + "github.com/aquaproj/aqua/v2/pkg/config/aqua" + "github.com/sirupsen/logrus" + "github.com/suzuki-shunsuke/logrus-error/logerr" +) + +func (c *Controller) listInstalled(param *config.Param, logE *logrus.Entry) error { + for _, cfgFilePath := range c.configFinder.Finds(param.PWD, param.ConfigFilePath) { + if err := c.listInstalledByConfig(cfgFilePath); err != nil { + return logerr.WithFields(err, logrus.Fields{ //nolint:wrapcheck + "config_file_path": cfgFilePath, + }) + } + } + + if !param.All { + return nil + } + + for _, cfgFilePath := range param.GlobalConfigFilePaths { + logE := logE.WithField("config_file_path", cfgFilePath) + logE.Debug("checking a global configuration file") + if _, err := c.fs.Stat(cfgFilePath); err != nil { + continue + } + if err := c.listInstalledByConfig(cfgFilePath); err != nil { + return logerr.WithFields(err, logrus.Fields{ //nolint:wrapcheck + "config_file_path": cfgFilePath, + }) + } + } + return nil +} + +func (c *Controller) listInstalledByConfig(cfgFilePath string) error { + cfg := &aqua.Config{} + if err := c.configReader.Read(cfgFilePath, cfg); err != nil { + return err //nolint:wrapcheck + } + for _, pkg := range cfg.Packages { + fmt.Fprintln(c.stdout, pkg.Name+"\t"+pkg.Version+"\t"+pkg.Registry) + } + return nil +}