Skip to content

Commit

Permalink
Fix log (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Nov 30, 2020
1 parent d04218d commit d2c550d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,28 @@ func readFile(path string) ([]byte, error) {
return bytea, nil
}

func getOptOrEnvOrDefault(optKey, optValue, envKey, defaultValue string) (string, error) {
if optKey == "" {
return "", fmt.Errorf("optKey is empty")
func getOptOrEnvOrDefault(optName, optValue, envName, defaultValue string) (string, error) {
if optName == "" {
return "", fmt.Errorf("optName is empty")
}

if optValue != "" {
log.Println("use option: " + optKey + "=" + optValue)
log.Println("use option value: -" + optName + "=" + optValue)
return optValue, nil
}

envValue := os.Getenv(envKey)
envValue := os.Getenv(envName)
if envValue != "" {
log.Println("use env: " + envKey + "=" + envValue)
log.Println("use environment variable: " + envName + "=" + envValue)
return envValue, nil
}

if defaultValue != "" {
log.Println("use default: " + optKey + "=" + defaultValue)
log.Println("use default option value: -" + optName + "=" + defaultValue)
return defaultValue, nil
}

return "", fmt.Errorf("set option -%s, or set environment variable %s", optKey, envKey)
return "", fmt.Errorf("set option -%s, or set environment variable %s", optName, envName)
}

func capitalizeInitial(s string) (capitalized string) {
Expand Down
16 changes: 8 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const (
testProbablyExistsPath = "go.mod"

// getOptOrEnvOrDefault
testOptKey = "testOptKey"
testOptName = "test-opt-key"
testOptValue = "testOptValue"
testEnvKey = "TEST_ENV_KEY"
testEnvName = "TEST_ENV_KEY"
testEnvValue = "testEnvValue"
testDefaultValue = "testDefaultValue"

Expand Down Expand Up @@ -286,7 +286,7 @@ func Test_readFile_NG_2(t *testing.T) {
}

func Test_getOptOrEnvOrDefault_OK_1(t *testing.T) {
v, err := getOptOrEnvOrDefault(testOptKey, testOptValue, testEnvKey, testDefaultValue)
v, err := getOptOrEnvOrDefault(testOptName, testOptValue, testEnvName, testDefaultValue)
if err != nil {
t.Error(err)
}
Expand All @@ -295,24 +295,24 @@ func Test_getOptOrEnvOrDefault_OK_1(t *testing.T) {
}
}
func Test_getOptOrEnvOrDefault_OK_2(t *testing.T) {
if err := os.Setenv(testEnvKey, testEnvValue); err != nil {
if err := os.Setenv(testEnvName, testEnvValue); err != nil {
t.Error(err)
}
v, err := getOptOrEnvOrDefault(testOptKey, testEmptyString, testEnvKey, testDefaultValue)
v, err := getOptOrEnvOrDefault(testOptName, testEmptyString, testEnvName, testDefaultValue)
if err != nil {
t.Error(err)
}
if v != testEnvValue {
t.Error(err)
}
if err := os.Unsetenv(testEnvKey); err != nil {
if err := os.Unsetenv(testEnvName); err != nil {
t.Error(err)
}

}

func Test_getOptOrEnvOrDefault_OK_3(t *testing.T) {
v, err := getOptOrEnvOrDefault(testOptKey, testEmptyString, testEnvKey, testDefaultValue)
v, err := getOptOrEnvOrDefault(testOptName, testEmptyString, testEnvName, testDefaultValue)
if err != nil {
t.Error(err)
}
Expand All @@ -332,7 +332,7 @@ func Test_getOptOrEnvOrDefault_NG_1(t *testing.T) {
}

func Test_getOptOrEnvOrDefault_NG_2(t *testing.T) {
v, err := getOptOrEnvOrDefault(testOptKey, testEmptyString, testEnvKey, testEmptyString)
v, err := getOptOrEnvOrDefault(testOptName, testEmptyString, testEnvName, testEmptyString)
if err == nil {
t.Error(err)
}
Expand Down

0 comments on commit d2c550d

Please sign in to comment.