From f5322f34e8c50a53ca9a2e24dab309dbeaacc088 Mon Sep 17 00:00:00 2001 From: Su Yang Date: Wed, 31 Jan 2024 10:53:48 +0800 Subject: [PATCH] feat: add vision flag --- cmd/cmd.go | 8 ++-- cmd/flags.go | 10 +++-- internal/fn/cmd.go | 13 ++++++ internal/fn/cmd_test.go | 92 +++++++++++++++++++++++++++++++++++++++++ internal/model/flags.go | 7 ++-- 5 files changed, 121 insertions(+), 9 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 42ca6fa..daf7d10 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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" ) diff --git a/cmd/flags.go b/cmd/flags.go index 5cb2f3e..50b2e31 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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 @@ -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 diff --git a/internal/fn/cmd.go b/internal/fn/cmd.go index fd92995..bd3537f 100644 --- a/internal/fn/cmd.go +++ b/internal/fn/cmd.go @@ -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 } diff --git a/internal/fn/cmd_test.go b/internal/fn/cmd_test.go index 64e8873..7b640ba 100644 --- a/internal/fn/cmd_test.go +++ b/internal/fn/cmd_test.go @@ -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) + } + }) + +} diff --git a/internal/model/flags.go b/internal/model/flags.go index 3613871..ca171ac 100644 --- a/internal/model/flags.go +++ b/internal/model/flags.go @@ -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 }