Skip to content

Commit

Permalink
feat: add info command (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke authored Jul 23, 2023
1 parent 97eaa87 commit b80f805
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/cli/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"fmt"

"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/controller"
"github.com/urfave/cli/v2"
)

func (runner *Runner) newInfoCommand() *cli.Command {
return &cli.Command{
Name: "info",
Usage: "Show information",
Description: `Show information.
e.g.
$ aqua info`,
Action: runner.info,
}
}

func (runner *Runner) info(c *cli.Context) error {
tracer, err := startTrace(c.String("trace"))
if err != nil {
return err
}
defer tracer.Stop()

cpuProfiler, err := startCPUProfile(c.String("cpu-profile"))
if err != nil {
return err
}
defer cpuProfiler.Stop()

param := &config.Param{}
if err := runner.setParam(c, "info", param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeInfoCommandController(c.Context, param, runner.Runtime)
return ctrl.Info(c.Context, runner.LogE, param, c.Args().First()) //nolint:wrapcheck
}
2 changes: 2 additions & 0 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (runner *Runner) setParam(c *cli.Context, commandName string, param *config
param.File = c.String("f")
param.LogColor = os.Getenv("AQUA_LOG_COLOR")
param.AQUAVersion = runner.LDFlags.Version
param.AquaCommitHash = runner.LDFlags.Commit
param.RootDir = config.GetRootDir(osenv.New())
homeDir, _ := os.UserHomeDir()
param.HomeDir = homeDir
Expand Down Expand Up @@ -155,6 +156,7 @@ func (runner *Runner) Run(ctx context.Context, args ...string) error {
EnableBashCompletion: true,
Commands: []*cli.Command{
runner.newInitCommand(),
runner.newInfoCommand(),
runner.newInitPolicyCommand(),
runner.newPolicyCommand(),
runner.newInstallCommand(),
Expand Down
1 change: 1 addition & 0 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type Param struct {
LogLevel string
File string
AQUAVersion string
AquaCommitHash string
RootDir string
PWD string
InsertFile string
Expand Down
126 changes: 126 additions & 0 deletions pkg/controller/info/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package info

import (
"context"
"encoding/json"
"fmt"
"os"
"os/user"
"runtime"
"strings"

"github.com/aquaproj/aqua/v2/pkg/config"
rt "github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

type Controller struct {
fs afero.Fs
finder ConfigFinder
rt *rt.Runtime
}

func New(fs afero.Fs, finder ConfigFinder, rt *rt.Runtime) *Controller {
return &Controller{
fs: fs,
finder: finder,
rt: rt,
}
}

type Info struct {
Version string `json:"version"`
CommitHash string `json:"commit_hash"`
OS string `json:"os"`
Arch string `json:"arch"`
AquaGOOS string `json:"aqua_goos,omitempty"`
AquaGOARCH string `json:"aqua_goarch,omitempty"`
PWD string `json:"pwd"`
RootDir string `json:"root_dir"`
Env map[string]string `json:"env"`
ConfigFiles []*Config `json:"config_files"`
}

type Config struct {
Path string `json:"path"`
}

func maskUser(s, username string) string {
return strings.ReplaceAll(s, username, "(USER)")
}

func (ctrl *Controller) Info(ctx context.Context, logE *logrus.Entry, param *config.Param, cfgFilePath string) error { //nolint:funlen
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("get a current user: %w", err)
}
userName := currentUser.Username

filePaths := ctrl.finder.Finds(param.PWD, param.ConfigFilePath)
cfgs := make([]*Config, len(filePaths))
for i, filePath := range filePaths {
cfgs[i] = &Config{
Path: maskUser(filePath, userName),
}
}

info := &Info{
Version: param.AQUAVersion,
CommitHash: param.AquaCommitHash,
PWD: maskUser(param.PWD, userName),
OS: runtime.GOOS,
Arch: runtime.GOARCH,
RootDir: maskUser(param.RootDir, userName),
ConfigFiles: cfgs,
Env: map[string]string{},
}

if ctrl.rt.GOOS != runtime.GOOS {
info.AquaGOOS = ctrl.rt.GOOS
}
if ctrl.rt.GOARCH != runtime.GOARCH {
info.AquaGOARCH = ctrl.rt.GOARCH
}

envs := []string{
"AQUA_CONFIG",
"AQUA_DISABLE_LAZY_INSTALL",
"AQUA_DISABLE_POLICY",
"AQUA_EXPERIMENTAL_X_SYS_EXEC",
"AQUA_GENERATE_WITH_DETAIL",
"AQUA_GLOBAL_CONFIG",
"AQUA_GOARCH",
"AQUA_GOOS",
"AQUA_LOG_COLOR",
"AQUA_LOG_LEVEL",
"AQUA_MAX_PARALLELISM",
"AQUA_POLICY_CONFIG",
"AQUA_PROGRESS_BAR",
"AQUA_REQUIRE_CHECKSUM",
"AQUA_ROOT_DIR",
"AQUA_X_SYS_EXEC",
}
for _, envName := range envs {
if v, ok := os.LookupEnv(envName); ok {
info.Env[envName] = maskUser(v, userName)
}
}
if _, ok := os.LookupEnv("AQUA_GITHUB_TOKEN"); ok {
info.Env["AQUA_GITHUB_TOKEN"] = "(masked)"
} else if _, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
info.Env["GITHUB_TOKEN"] = "(masked)"
}

encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(info); err != nil {
return fmt.Errorf("encode info as JSON and output it to stdout: %w", err)
}
return nil
}

type ConfigFinder interface {
Find(wd, configFilePath string, globalConfigFilePaths ...string) (string, error)
Finds(wd, configFilePath string) []string
}
13 changes: 13 additions & 0 deletions pkg/controller/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aquaproj/aqua/v2/pkg/controller/generate"
genrgst "github.com/aquaproj/aqua/v2/pkg/controller/generate-registry"
"github.com/aquaproj/aqua/v2/pkg/controller/generate/output"
"github.com/aquaproj/aqua/v2/pkg/controller/info"
"github.com/aquaproj/aqua/v2/pkg/controller/initcmd"
"github.com/aquaproj/aqua/v2/pkg/controller/initpolicy"
"github.com/aquaproj/aqua/v2/pkg/controller/install"
Expand Down Expand Up @@ -731,3 +732,15 @@ func InitializeDenyPolicyCommandController(ctx context.Context, param *config.Pa
)
return &denypolicy.Controller{}
}

func InitializeInfoCommandController(ctx context.Context, param *config.Param, rt *runtime.Runtime) *info.Controller {
wire.Build(
info.New,
wire.NewSet(
finder.NewConfigFinder,
wire.Bind(new(info.ConfigFinder), new(*finder.ConfigFinder)),
),
afero.NewOsFs,
)
return &info.Controller{}
}
8 changes: 8 additions & 0 deletions pkg/controller/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b80f805

Please sign in to comment.