diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-08-14 12:46:13 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-08-15 16:16:33 +0300 |
commit | a2311670d48d9e3ac0081c502d47e63c5e5a5f2d (patch) | |
tree | e886ef434f9f59938e1dd70db9a56192f127adab | |
parent | d2306a5e5ba5c600b05f84b3d1bc06ec3cc47d63 (diff) |
cmd/gitaly: Properly configure the logger for the "check" subcommand
Same as with the preceding commit, the "check" subcommand does not
properly initialize its logging infrastructure. Refactor the code to do
so.
Note that we also refactor the code such that we first load the config
and initialize the logging infrastructure before we start to perform the
checks. This allows us to exit early in case the configuration does not
exist or cannot be parsed, whereas until now we already started printing
status information to the process's standard output.
-rw-r--r-- | cmd/gitaly/main_test.go | 3 | ||||
-rw-r--r-- | internal/cli/gitaly/check.go | 21 |
2 files changed, 12 insertions, 12 deletions
diff --git a/cmd/gitaly/main_test.go b/cmd/gitaly/main_test.go index d9335f74c..f0f10b2d7 100644 --- a/cmd/gitaly/main_test.go +++ b/cmd/gitaly/main_test.go @@ -49,8 +49,7 @@ func TestGitalyCLI(t *testing.T) { desc: "check with non-existent config", args: []string{"check", "non-existent-file"}, exitCode: 1, - stdout: "Checking GitLab API access: FAILED", - stderr: "load config: config_path \"non-existent-file\": open non-existent-file: no such file or directory\n", + stderr: "loading configuration \"non-existent-file\": open non-existent-file: no such file or directory\n", }, } { t.Run(tc.desc, func(t *testing.T) { diff --git a/internal/cli/gitaly/check.go b/internal/cli/gitaly/check.go index 40d5f925e..9dc99e731 100644 --- a/internal/cli/gitaly/check.go +++ b/internal/cli/gitaly/check.go @@ -3,6 +3,7 @@ package gitaly import ( "context" "fmt" + "os" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -11,6 +12,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config/prometheus" "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/v16/internal/gitlab" + "gitlab.com/gitlab-org/gitaly/v16/internal/log" ) func newCheckCommand() *cli.Command { @@ -27,8 +29,6 @@ Example: gitaly check gitaly.config.toml`, } func checkAction(ctx *cli.Context) error { - logrus.SetLevel(logrus.ErrorLevel) - if ctx.NArg() != 1 || ctx.Args().First() == "" { if err := cli.ShowSubcommandHelp(ctx); err != nil { return err @@ -38,9 +38,15 @@ func checkAction(ctx *cli.Context) error { } configPath := ctx.Args().First() + cfg, err := loadConfig(configPath) + if err != nil { + return fmt.Errorf("loading configuration %q: %w", configPath, err) + } + + logger := log.Configure(os.Stderr, "text", "error") fmt.Fprint(ctx.App.Writer, "Checking GitLab API access: ") - info, err := checkAPI(configPath) + info, err := checkAPI(cfg, logger) if err != nil { fmt.Fprintln(ctx.App.Writer, "FAILED") return err @@ -56,13 +62,8 @@ func checkAction(ctx *cli.Context) error { return nil } -func checkAPI(configPath string) (*gitlab.CheckInfo, error) { - cfg, err := loadConfig(configPath) - if err != nil { - return nil, fmt.Errorf("load config: config_path %q: %w", configPath, err) - } - - gitlabAPI, err := gitlab.NewHTTPClient(logrus.New(), cfg.Gitlab, cfg.TLS, prometheus.Config{}) +func checkAPI(cfg config.Cfg, logger logrus.FieldLogger) (*gitlab.CheckInfo, error) { + gitlabAPI, err := gitlab.NewHTTPClient(logger, cfg.Gitlab, cfg.TLS, prometheus.Config{}) if err != nil { return nil, err } |