-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97eaa87
commit b80f805
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.