Skip to content

Commit

Permalink
Slightly better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonodonnell committed Apr 2, 2024
1 parent a7b83ca commit 0a49de7
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ type BaseCommand struct {
func (c *BaseCommand) Client() (*api.Client, error) {
// Read the test client if present
if c.client != nil {
if err := c.applyHCPConfig(); err != nil {
return nil, err
// Ignoring homedir errors here and moving on to avoid
// spamming user with warnings/errors that homedir isn't set.
path, err := homedir.Dir()
if err == nil {
if err := c.applyHCPConfig(path); err != nil {
return nil, err
}
}

return c.client, nil
Expand Down Expand Up @@ -197,8 +202,13 @@ func (c *BaseCommand) Client() (*api.Client, error) {

c.client = client

if err := c.applyHCPConfig(); err != nil {
return nil, err
// Ignoring homedir errors here and moving on to avoid
// spamming user with warnings/errors that homedir isn't set.
path, err := homedir.Dir()
if err == nil {
if err := c.applyHCPConfig(path); err != nil {
return nil, err
}
}

if c.addrWarning != "" && c.UI != nil {
Expand All @@ -212,18 +222,11 @@ func (c *BaseCommand) Client() (*api.Client, error) {
return client, nil
}

func (c *BaseCommand) applyHCPConfig() error {
func (c *BaseCommand) applyHCPConfig(path string) error {
if c.hcpTokenHelper == nil {
c.hcpTokenHelper = c.HCPTokenHelper()
}

// Silently bailing here because if HOME is not set it's going
// to spam the user with errors or warnings.
path, err := homedir.Dir()
if err != nil {
return nil
}

hcpToken, err := c.hcpTokenHelper.GetHCPToken(path)
if err != nil {
return err
Expand Down

0 comments on commit 0a49de7

Please sign in to comment.