diff --git a/pkg/secrethub/client.go b/pkg/secrethub/client.go index 70b09167..d175ecec 100644 --- a/pkg/secrethub/client.go +++ b/pkg/secrethub/client.go @@ -162,19 +162,7 @@ func NewClient(with ...ClientOption) (*Client, error) { } } - appName := os.Getenv("SECRETHUB_APP_INFO_NAME") - if appName != "" { - appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION") - topLevelAppInfo := &AppInfo{ - Name: appName, - Version: appVersion, - } - // Ignore app info from environment variable if name is invalid - if err = topLevelAppInfo.ValidateName(); err == nil { - client.appInfo = append(client.appInfo, topLevelAppInfo) - } - } - + client.loadAppInfoFromEnv() userAgent := client.userAgent() client.httpClient.Options(http.WithUserAgent(userAgent)) @@ -286,6 +274,21 @@ func (c *Client) isKeyed() bool { return c.decrypter != nil } +func (c *Client) loadAppInfoFromEnv() { + appName := os.Getenv("SECRETHUB_APP_INFO_NAME") + if appName != "" { + appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION") + topLevelAppInfo := &AppInfo{ + Name: appName, + Version: appVersion, + } + // Ignore app info from environment variable if name is invalid + if err := topLevelAppInfo.ValidateName(); err == nil { + c.appInfo = append(c.appInfo, topLevelAppInfo) + } + } +} + func (c *Client) userAgent() string { userAgent := userAgentPrefix for _, info := range c.appInfo { diff --git a/pkg/secrethub/client_test.go b/pkg/secrethub/client_test.go index 3b924356..a31477a9 100644 --- a/pkg/secrethub/client_test.go +++ b/pkg/secrethub/client_test.go @@ -62,11 +62,11 @@ func TestClient_userAgent(t *testing.T) { for _, info := range tc.appInfo { opts = append(opts, WithAppInfo(info)) } - client, err := NewClient(opts...) + client := &Client{} + err := client.with(opts...) assert.Equal(t, err, tc.err) - if err != nil { - return - } + + client.loadAppInfoFromEnv() userAgent := client.userAgent() pattern := tc.expected + " \\(.*\\)"