diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-05-24 16:22:07 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-05-30 09:09:53 +0300 |
commit | a7ed6ea1d947a18a69cb673baa4f85c7013f7e1c (patch) | |
tree | a8f38874a82159180826a6c7f6e190bb3d76aec4 | |
parent | b62d7112a5e1591053abf68d1a4ff3f406327339 (diff) |
gitaly-lfs-smudge: Move config-related code into its own file
We're about to refactor the way the configuration is handled, which also
includes moving the configuration code into a separate package. Split it
out into a separate file for now to prepare for this move.
-rw-r--r-- | cmd/gitaly-lfs-smudge/config.go | 50 | ||||
-rw-r--r-- | cmd/gitaly-lfs-smudge/lfs_smudge.go | 37 | ||||
-rw-r--r-- | cmd/gitaly-lfs-smudge/main.go | 6 |
3 files changed, 50 insertions, 43 deletions
diff --git a/cmd/gitaly-lfs-smudge/config.go b/cmd/gitaly-lfs-smudge/config.go new file mode 100644 index 000000000..d6bd7e139 --- /dev/null +++ b/cmd/gitaly-lfs-smudge/config.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" + + "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" +) + +type configProvider interface { + Get(key string) string +} + +type envConfig struct{} + +func (e *envConfig) Get(key string) string { + return os.Getenv(key) +} + +func loadConfig(cfgProvider configProvider) (config.Gitlab, config.TLS, string, error) { + var cfg config.Gitlab + var tlsCfg config.TLS + + glRepository := cfgProvider.Get("GL_REPOSITORY") + if glRepository == "" { + return cfg, tlsCfg, "", fmt.Errorf("error loading project: GL_REPOSITORY is not defined") + } + + u := cfgProvider.Get("GL_INTERNAL_CONFIG") + if u == "" { + return cfg, tlsCfg, glRepository, fmt.Errorf("unable to retrieve GL_INTERNAL_CONFIG") + } + + if err := json.Unmarshal([]byte(u), &cfg); err != nil { + return cfg, tlsCfg, glRepository, fmt.Errorf("unable to unmarshal GL_INTERNAL_CONFIG: %v", err) + } + + u = cfgProvider.Get("GITALY_TLS") + if u == "" { + return cfg, tlsCfg, glRepository, errors.New("unable to retrieve GITALY_TLS") + } + + if err := json.Unmarshal([]byte(u), &tlsCfg); err != nil { + return cfg, tlsCfg, glRepository, fmt.Errorf("unable to unmarshal GITALY_TLS: %w", err) + } + + return cfg, tlsCfg, glRepository, nil +} diff --git a/cmd/gitaly-lfs-smudge/lfs_smudge.go b/cmd/gitaly-lfs-smudge/lfs_smudge.go index 75166b001..780b379ae 100644 --- a/cmd/gitaly-lfs-smudge/lfs_smudge.go +++ b/cmd/gitaly-lfs-smudge/lfs_smudge.go @@ -2,24 +2,17 @@ package main import ( "context" - "encoding/json" - "errors" "fmt" "io" "net/url" "github.com/git-lfs/git-lfs/lfs" - "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/prometheus" "gitlab.com/gitlab-org/gitaly/v15/internal/gitlab" "gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/labkit/tracing" ) -type configProvider interface { - Get(key string) string -} - func smudge(to io.Writer, from io.Reader, cfgProvider configProvider) (returnedErr error) { // Since the environment is sanitized at the moment, we're only // using this to extract the correlation ID. The finished() call @@ -89,33 +82,3 @@ func handleSmudge(ctx context.Context, to io.Writer, from io.Reader, config conf return io.NopCloser(contents), nil } - -func loadConfig(cfgProvider configProvider) (config.Gitlab, config.TLS, string, error) { - var cfg config.Gitlab - var tlsCfg config.TLS - - glRepository := cfgProvider.Get("GL_REPOSITORY") - if glRepository == "" { - return cfg, tlsCfg, "", fmt.Errorf("error loading project: GL_REPOSITORY is not defined") - } - - u := cfgProvider.Get("GL_INTERNAL_CONFIG") - if u == "" { - return cfg, tlsCfg, glRepository, fmt.Errorf("unable to retrieve GL_INTERNAL_CONFIG") - } - - if err := json.Unmarshal([]byte(u), &cfg); err != nil { - return cfg, tlsCfg, glRepository, fmt.Errorf("unable to unmarshal GL_INTERNAL_CONFIG: %v", err) - } - - u = cfgProvider.Get("GITALY_TLS") - if u == "" { - return cfg, tlsCfg, glRepository, errors.New("unable to retrieve GITALY_TLS") - } - - if err := json.Unmarshal([]byte(u), &tlsCfg); err != nil { - return cfg, tlsCfg, glRepository, fmt.Errorf("unable to unmarshal GITALY_TLS: %w", err) - } - - return cfg, tlsCfg, glRepository, nil -} diff --git a/cmd/gitaly-lfs-smudge/main.go b/cmd/gitaly-lfs-smudge/main.go index c29d03d25..69e62fe77 100644 --- a/cmd/gitaly-lfs-smudge/main.go +++ b/cmd/gitaly-lfs-smudge/main.go @@ -10,12 +10,6 @@ import ( "gitlab.com/gitlab-org/labkit/log" ) -type envConfig struct{} - -func (e *envConfig) Get(key string) string { - return os.Getenv(key) -} - func requireStdin(msg string) { var out string |