-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor options/config file parsing
Rather than having config file parsing be part of the logs package, let's move it out so that it can easily be shared between additional commands as they arise.
- Loading branch information
Showing
8 changed files
with
244 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"gopkg.in/yaml.v3" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
DefaultConfigFile = "~/.swo-cli.yml" | ||
DefaultAPIURL = "https://api.na-01.cloud.solarwinds.com" | ||
APIURLContextKey = "api-url" | ||
TokenContextKey = "token" | ||
) | ||
|
||
var ( | ||
errMissingToken = errors.New("failed to find token") | ||
errMissingAPIURL = errors.New("failed to find API URL") | ||
) | ||
|
||
type Config struct { | ||
APIURL string `yaml:"api-url"` | ||
Token string `yaml:"token"` | ||
} | ||
|
||
/* | ||
* Precedence: CLI flags, environment, config file | ||
*/ | ||
func Init(configPath string, apiURL string, apiToken string) (*Config, error) { | ||
config := &Config{ | ||
APIURL: apiURL, | ||
Token: apiToken, | ||
} | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
localConfig := filepath.Join(cwd, ".swo-cli.yaml") | ||
if _, err := os.Stat(localConfig); err == nil { | ||
configPath = localConfig | ||
} else if strings.HasPrefix(configPath, "~/") { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return nil, fmt.Errorf("error while resolving current user to read configuration file: %w", err) | ||
} | ||
|
||
configPath = filepath.Join(usr.HomeDir, configPath[2:]) | ||
} | ||
|
||
if content, err := os.ReadFile(configPath); err == nil { | ||
err = yaml.Unmarshal(content, config) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while unmarshaling %s config file: %w", configPath, err) | ||
} | ||
} | ||
|
||
if token := os.Getenv("SWO_API_TOKEN"); token != "" { | ||
config.Token = token | ||
} | ||
|
||
if url := os.Getenv("SWO_API_URL"); url != "" { | ||
config.APIURL = url | ||
} | ||
|
||
if config.Token == "" { | ||
return nil, errMissingToken | ||
} | ||
|
||
if config.APIURL == "" { | ||
return nil, errMissingAPIURL | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func createConfigFile(t *testing.T, content string) string { | ||
f, err := os.CreateTemp(os.TempDir(), "swo-config-test") | ||
require.NoError(t, err, "creating a temporary file should not fail") | ||
|
||
n, err := f.Write([]byte(content)) | ||
require.Equal(t, n, len(content)) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
os.Remove(f.Name()) | ||
}) | ||
|
||
return f.Name() | ||
} | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
configFile string | ||
apiURL string | ||
token string | ||
expected Config | ||
expectedError error | ||
action func() | ||
}{ | ||
{ | ||
name: "read full config file", | ||
expected: Config{ | ||
APIURL: "https://api.solarwinds.com", | ||
Token: "123456", | ||
}, | ||
configFile: func() string { | ||
yamlStr := ` | ||
token: 123456 | ||
api-url: https://api.solarwinds.com | ||
` | ||
return createConfigFile(t, yamlStr) | ||
}(), | ||
}, | ||
{ | ||
name: "read token from config file", | ||
expected: Config{ | ||
APIURL: DefaultAPIURL, | ||
Token: "123456", | ||
}, | ||
configFile: func() string { | ||
yamlStr := "token: 123456" | ||
return createConfigFile(t, yamlStr) | ||
}(), | ||
}, | ||
{ | ||
name: "read token from env var", | ||
expected: Config{ | ||
APIURL: DefaultAPIURL, | ||
Token: "tokenFromEnvVar", | ||
}, | ||
action: func() { | ||
err := os.Setenv("SWO_API_TOKEN", "tokenFromEnvVar") | ||
require.NoError(t, err) | ||
}, | ||
}, | ||
{ | ||
name: "missing token", | ||
expectedError: errMissingToken, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Setenv("SWO_API_TOKEN", "") | ||
os.Setenv("SWO_API_URL", "") | ||
|
||
if tc.action != nil { | ||
tc.action() | ||
} | ||
|
||
cfg, err := Init(tc.configFile, DefaultAPIURL, "") | ||
require.True(t, errors.Is(err, tc.expectedError), "error: %v, expected: %v", err, tc.expectedError) | ||
if tc.expectedError != nil { | ||
return | ||
} | ||
|
||
require.Equal(t, &tc.expected, cfg) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.