summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeistel <6742251-feistel@users.noreply.gitlab.com>2021-08-13 13:51:21 +0000
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2021-08-13 13:51:21 +0000
commit160dcb064412e53a638c4329a60feadaafb1ab48 (patch)
treedf8c4dd3134cc1905043b8d62c4b1705dddd7bc6
parent8a1d584de771b3267374397cc99c019de1c4500c (diff)
downloadgitlab-shell-160dcb064412e53a638c4329a60feadaafb1ab48.tar.gz
refactor: change httpclient to return an error
-rw-r--r--internal/config/config.go9
-rw-r--r--internal/config/config_test.go5
-rw-r--r--internal/gitlabnet/client.go5
3 files changed, 12 insertions, 7 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 41ad7cd..f69a6c8 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -15,7 +15,6 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/client"
"gitlab.com/gitlab-org/gitlab-shell/internal/metrics"
- "gitlab.com/gitlab-org/labkit/log"
)
const (
@@ -59,6 +58,7 @@ type Config struct {
Server ServerConfig `yaml:"sshd"`
httpClient *client.HttpClient
+ httpClientErr error
httpClientOnce sync.Once
}
@@ -96,7 +96,7 @@ func (c *Config) ApplyGlobalState() {
}
}
-func (c *Config) HttpClient() *client.HttpClient {
+func (c *Config) HttpClient() (*client.HttpClient, error) {
c.httpClientOnce.Do(func() {
client, err := client.NewHTTPClientWithOpts(
c.GitlabUrl,
@@ -108,7 +108,8 @@ func (c *Config) HttpClient() *client.HttpClient {
nil,
)
if err != nil {
- log.WithError(err).Fatal("new http client with opts")
+ c.httpClientErr = err
+ return
}
tr := client.Transport
@@ -117,7 +118,7 @@ func (c *Config) HttpClient() *client.HttpClient {
c.httpClient = client
})
- return c.httpClient
+ return c.httpClient, c.httpClientErr
}
// NewFromDirExternal returns a new config from a given root dir. It also applies defaults appropriate for
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 5fa8e66..699a261 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -29,9 +29,10 @@ func TestHttpClient(t *testing.T) {
url := testserver.StartHttpServer(t, []testserver.TestRequestHandler{})
config := &Config{GitlabUrl: url}
- client := config.HttpClient()
+ client, err := config.HttpClient()
+ require.NoError(t, err)
- _, err := client.Get("http://host.com/path")
+ _, err = client.Get("http://host.com/path")
require.NoError(t, err)
ms, err := prometheus.DefaultGatherer.Gather()
diff --git a/internal/gitlabnet/client.go b/internal/gitlabnet/client.go
index c0b72c4..39c3320 100644
--- a/internal/gitlabnet/client.go
+++ b/internal/gitlabnet/client.go
@@ -15,7 +15,10 @@ var (
)
func GetClient(config *config.Config) (*client.GitlabNetClient, error) {
- httpClient := config.HttpClient()
+ httpClient, err := config.HttpClient()
+ if err != nil {
+ return nil, err
+ }
if httpClient == nil {
return nil, fmt.Errorf("Unsupported protocol")