Skip to content

Commit

Permalink
feat: add vision flag
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jan 31, 2024
1 parent 15d841a commit f5322f3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
8 changes: 5 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
)

const (
_DEFAULT_PORT = 8080
_DEFAULT_HOST = "0.0.0.0"
_DEFAULT_TYPE = "azure"
_DEFAULT_PORT = 8080
_DEFAULT_HOST = "0.0.0.0"
_DEFAULT_TYPE = "azure"
_DEFAULT_VISION = false

_ENV_KEY_NAME_PORT = "AOA_PORT"
_ENV_KEY_USE_VISION = "AOA_VISION"
_ENV_KEY_NAME_HOST = "AOA_HOST"
_ENV_KEY_SERVICE_TYPE = "AOA_TYPE"
)
Expand Down
10 changes: 7 additions & 3 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func parseEnvVars() AoaModel.Flags {
ShowVersion: false,
ShowHelp: false,

Type: _DEFAULT_TYPE,
Port: _DEFAULT_PORT,
Host: _DEFAULT_HOST,
Type: _DEFAULT_TYPE,
Vision: _DEFAULT_VISION,
Port: _DEFAULT_PORT,
Host: _DEFAULT_HOST,
}

// check and set port
Expand All @@ -32,6 +33,9 @@ func parseEnvVars() AoaModel.Flags {
flags.Host = _DEFAULT_HOST
}

// check and set vision
flags.Vision = fn.GetBoolOrDefaultFromEnv(_ENV_KEY_USE_VISION, _DEFAULT_VISION)

// check and set type
flags.Type = strings.ToLower(fn.GetStringOrDefaultFromEnv(_ENV_KEY_SERVICE_TYPE, _DEFAULT_TYPE))
// TODO support all types
Expand Down
13 changes: 13 additions & 0 deletions internal/fn/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ func GetStringOrDefaultFromEnv(key string, defaultValue string) string {
return value
}

func GetBoolOrDefaultFromEnv(key string, defaultValue bool) bool {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
return defaultValue
}

s := strings.ToLower(value)
if s == "true" || s == "on" || s == "yes" || s == "1" {
return true
}
return false
}

func IsValidIPAddress(ip string) bool {
return net.ParseIP(ip) != nil
}
92 changes: 92 additions & 0 deletions internal/fn/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,95 @@ func TestIsValidIPAddress(t *testing.T) {
})
}
}

func TestGetBoolOrDefaultFromEnv(t *testing.T) {
const envKey = "TEST_BOOL_ENV_VAR"

t.Run("ReturnsDefaultValueForUnset", func(t *testing.T) {
os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != false {
t.Errorf("Expected default value %v, got %v", false, got)
}

os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != true {
t.Errorf("Expected default value %v, got %v", true, got)
}
})

t.Run("test on", func(t *testing.T) {
expected := "on"
os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}

os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}
})

t.Run("test true", func(t *testing.T) {
expected := "true"
os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}

os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}
})

t.Run("test 1", func(t *testing.T) {
expected := "1"
os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}

os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}
})

t.Run("test yes", func(t *testing.T) {
expected := "yes"
os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}

os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != true {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}
})

t.Run("test 0", func(t *testing.T) {
expected := "0"
os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, false); got != false {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}

os.Setenv(envKey, expected)
defer os.Unsetenv(envKey)
if got := fn.GetBoolOrDefaultFromEnv(envKey, true); got != false {
t.Errorf("Expected non-empty value %v, got %v", expected, got)
}
})

}
7 changes: 4 additions & 3 deletions internal/model/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ type Flags struct {
ShowVersion bool
ShowHelp bool

Type string
Port int
Host string
Type string
Vision bool
Port int
Host string
}

0 comments on commit f5322f3

Please sign in to comment.