feat: add initial implementation of Go utilities and CI workflows

This commit is contained in:
2025-06-18 16:46:01 -04:00
commit c588ade142
12 changed files with 475 additions and 0 deletions

55
Env/env.go Executable file
View File

@@ -0,0 +1,55 @@
package Env
import (
"log"
"os"
"strconv"
"strings"
)
type EnvironmentVariable string
func (v EnvironmentVariable) GetName() string {
return string(v)
}
func (v EnvironmentVariable) GetEnvString(fallback string) string {
if value, ok := os.LookupEnv(string(v)); ok {
return value
}
return fallback
}
func (v EnvironmentVariable) GetEnvBool(fallback bool) bool {
value, ok := os.LookupEnv(string(v))
if !ok {
return fallback
}
value = strings.ToLower(value)
trueValues := []string{"1", "true"}
for _, x := range trueValues {
if value == x {
return true
}
}
return false
}
func (v EnvironmentVariable) GetEnvInt(key string, fallback int64) int64 {
if value, ok := os.LookupEnv(key); ok {
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
log.Fatal(err)
}
return i
}
return fallback
}

69
Env/env_test.go Normal file
View File

@@ -0,0 +1,69 @@
package Env
import (
"os"
"testing"
)
func TestEnvironmentVariable_GetName(t *testing.T) {
v := EnvironmentVariable("TEST_VAR")
if v.GetName() != "TEST_VAR" {
t.Errorf("expected 'TEST_VAR', got '%s'", v.GetName())
}
}
func TestEnvironmentVariable_GetEnvString(t *testing.T) {
const envName = "TEST_STRING"
_ = os.Setenv(envName, "value")
defer func() {
_ = os.Unsetenv(envName)
}()
v := EnvironmentVariable(envName)
got := v.GetEnvString("fallback")
if got != "value" {
t.Errorf("expected 'value', got '%s'", got)
}
_ = os.Unsetenv(envName)
got = v.GetEnvString("fallback")
if got != "fallback" {
t.Errorf("expected 'fallback', got '%s'", got)
}
}
func TestEnvironmentVariable_GetEnvBool(t *testing.T) {
const envName = "TEST_BOOL"
v := EnvironmentVariable(envName)
_ = os.Setenv(envName, "true")
if !v.GetEnvBool(false) {
t.Errorf("expected true for 'true'")
}
_ = os.Setenv(envName, "1")
if !v.GetEnvBool(false) {
t.Errorf("expected true for '1'")
}
_ = os.Setenv(envName, "false")
if v.GetEnvBool(true) {
t.Errorf("expected false for 'false'")
}
_ = os.Unsetenv(envName)
if v.GetEnvBool(true) != true {
t.Errorf("expected fallback true")
}
}
func TestEnvironmentVariable_GetEnvInt(t *testing.T) {
const envName = "TEST_INT"
v := EnvironmentVariable(envName)
_ = os.Setenv(envName, "42")
if v.GetEnvInt(envName, 10) != 42 {
t.Errorf("expected 42")
}
_ = os.Unsetenv(envName)
if v.GetEnvInt(envName, 10) != 10 {
t.Errorf("expected fallback 10")
}
}