fix: WithToken should override environment variable (#2706)

This commit is contained in:
abelanger5
2025-12-23 14:50:15 -05:00
committed by GitHub
parent 6013cadda7
commit 7ae0d5f86b
3 changed files with 8 additions and 4 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ repos:
- id: check-yaml
exclude: ^examples/
- repo: https://github.com/golangci/golangci-lint
rev: v2.4.0
rev: v2.5.0
hooks:
- id: golangci-lint
args: ["--config=.golangci.yml"]
+1 -1
View File
@@ -114,7 +114,7 @@ func defaultClientOpts(token *string, cf *client.ClientConfigFile) *ClientOpts {
if token != nil {
cf.Token = *token
}
clientConfig, err = loader.GetClientConfigFromConfigFile(cf)
clientConfig, err = loader.GetClientConfigFromConfigFile(token, cf)
if err != nil {
panic(err)
+6 -2
View File
@@ -33,7 +33,7 @@ func (c *ConfigLoader) LoadClientConfig(token *string) (res *client.ClientConfig
cf.Token = *token
}
return GetClientConfigFromConfigFile(cf)
return GetClientConfigFromConfigFile(token, cf)
}
// LoadClientConfigFile loads the worker config file via viper
@@ -46,7 +46,7 @@ func LoadClientConfigFile(files ...[]byte) (*client.ClientConfigFile, error) {
return configFile, err
}
func GetClientConfigFromConfigFile(cf *client.ClientConfigFile) (res *client.ClientConfig, err error) {
func GetClientConfigFromConfigFile(tokenOverride *string, cf *client.ClientConfigFile) (res *client.ClientConfig, err error) {
f := client.BindAllEnv
_, err = loaderutils.LoadConfigFromViper(f, cf)
@@ -55,6 +55,10 @@ func GetClientConfigFromConfigFile(cf *client.ClientConfigFile) (res *client.Cli
return nil, fmt.Errorf("could not load config from viper: %w", err)
}
if tokenOverride != nil {
cf.Token = *tokenOverride
}
// if token is empty, throw an error
if cf.Token == "" {
return nil, fmt.Errorf("API token is required. Set it via the HATCHET_CLIENT_TOKEN environment variable")