Skip to content

Commit

Permalink
extract the name upctl to a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Marin committed Jan 3, 2025
1 parent 37e5376 commit 653ccde
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/clierrors/missing_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import "fmt"
var _ ClientError = MissingCredentialsError{}

type MissingCredentialsError struct {
ConfigFile string
ConfigFile string
ServiceName string
}

func (err MissingCredentialsError) ErrorCode() int {
return MissingCredentials
}

func (err MissingCredentialsError) Error() string {
return fmt.Sprintf("user credentials not found, these must be set in config file (%s) or via environment variables or in a keyring (upctl)", err.ConfigFile)
return fmt.Sprintf("user credentials not found, these must be set in config file (%s) or via environment variables or in a keyring (%s)", err.ConfigFile, err.ServiceName)
}
11 changes: 7 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (

// env vars custom prefix
envPrefix = "UPCLOUD"

// serviceName is the name of the service
serviceName = "upctl"
)

var (
Expand Down Expand Up @@ -77,7 +80,7 @@ func (s *Config) Load() error {
v.SetEnvPrefix(envPrefix)
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
v.AutomaticEnv()
v.SetConfigName("upctl")
v.SetConfigName(serviceName)
v.SetConfigType("yaml")

configFile := s.GlobalFlags.ConfigFile
Expand All @@ -97,7 +100,7 @@ func (s *Config) Load() error {
}

if v.GetString("username") != "" && v.GetString("password") == "" {
password, err := keyring.Get("upctl", v.GetString("username"))
password, err := keyring.Get(serviceName, v.GetString("username"))
if err == nil {
v.MergeConfigMap(map[string]interface{}{"password": password})

Check failure on line 105 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `v.MergeConfigMap` is not checked (errcheck)
}
Expand Down Expand Up @@ -203,11 +206,11 @@ func (s *Config) CreateService() (internal.AllServices, error) {
if s.GetString("config") != "" {
configDetails = fmt.Sprintf("used %s", s.GetString("config"))
}
return nil, clierrors.MissingCredentialsError{ConfigFile: configDetails}
return nil, clierrors.MissingCredentialsError{ConfigFile: configDetails, ServiceName: serviceName}
}

client := client.New(username, password, client.WithTimeout(s.ClientTimeout()))
client.UserAgent = fmt.Sprintf("upctl/%s", GetVersion())
client.UserAgent = fmt.Sprintf("%s/%s", serviceName, GetVersion())

svc := service.New(client)
return svc, nil
Expand Down

0 comments on commit 653ccde

Please sign in to comment.