Added greater granularity for boolean environment variables (#511)

* Added greater granularity for boolean environment variables

* Improved solution as reviewed
This commit is contained in:
Alessandro Detta
2024-02-22 11:35:53 +01:00
committed by GitHub
parent 3d7f907a33
commit b5bed10571
7 changed files with 73 additions and 11 deletions

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/joomcode/errorx"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@@ -12,7 +11,9 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard" "github.com/komodorio/helm-dashboard/pkg/dashboard"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
"github.com/pkg/browser" "github.com/pkg/browser"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -50,7 +51,7 @@ func main() {
opts.BindHost = host opts.BindHost = host
} }
opts.Verbose = opts.Verbose || os.Getenv("DEBUG") != "" opts.Verbose = opts.Verbose || utils.EnvAsBool("DEBUG", false)
setupLogging(opts.Verbose) setupLogging(opts.Verbose)
server := dashboard.Server{ server := dashboard.Server{

View File

@@ -4,12 +4,12 @@ import (
"context" "context"
"html" "html"
"net/http" "net/http"
"os"
"path" "path"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/komodorio/helm-dashboard/pkg/dashboard/handlers" "github.com/komodorio/helm-dashboard/pkg/dashboard/handlers"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects" "github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
"github.com/komodorio/helm-dashboard/pkg/frontend" "github.com/komodorio/helm-dashboard/pkg/frontend"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -95,7 +95,7 @@ func NewRouter(abortWeb context.CancelFunc, data *objects.DataLayer, debug bool)
api.Use(errorHandler) api.Use(errorHandler)
api.Use(corsMiddleware()) api.Use(corsMiddleware())
if os.Getenv("HD_CORS") != "" { if utils.EnvAsBool("HD_CORS", false) {
api.Use(allowCORS) api.Use(allowCORS)
} }

View File

@@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/komodorio/helm-dashboard/pkg/dashboard/handlers" "github.com/komodorio/helm-dashboard/pkg/dashboard/handlers"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects" "github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
@@ -27,7 +28,7 @@ var inMemStorage *storage.Storage
var repoFile string var repoFile string
func TestMain(m *testing.M) { // fixture to set logging level via env variable func TestMain(m *testing.M) { // fixture to set logging level via env variable
if os.Getenv("DEBUG") != "" { if utils.EnvAsBool("DEBUG", false) {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Debugf("Set logging level") log.Debugf("Set logging level")
} }

View File

@@ -3,7 +3,6 @@ package objects
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"os"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -11,6 +10,7 @@ import (
"io" "io"
"github.com/joomcode/errorx" "github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
@@ -193,7 +193,7 @@ func (d *DataLayer) nsForCtx(ctx string) string {
} }
func (d *DataLayer) PeriodicTasks(ctx context.Context) { func (d *DataLayer) PeriodicTasks(ctx context.Context) {
if os.Getenv("HD_NO_AUTOUPDATE") == "" { if !utils.EnvAsBool("HD_NO_AUTOUPDATE", false) {
// auto-update repos // auto-update repos
go d.loopUpdateRepos(ctx, 10*time.Minute) // TODO: parameterize interval? go d.loopUpdateRepos(ctx, 10*time.Minute) // TODO: parameterize interval?
} }

View File

@@ -38,9 +38,7 @@ func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (st
} }
data.LocalCharts = s.LocalCharts data.LocalCharts = s.LocalCharts
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || utils.EnvAsBool("HD_DEV_ANALYTICS", false)
isDevModeWithAnalytics := os.Getenv("HD_DEV_ANALYTICS") == "true"
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || isDevModeWithAnalytics
err = s.detectClusterMode(data) err = s.detectClusterMode(data)
if err != nil { if err != nil {
@@ -58,7 +56,7 @@ func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (st
} }
func (s *Server) detectClusterMode(data *objects.DataLayer) error { func (s *Server) detectClusterMode(data *objects.DataLayer) error {
data.StatusInfo.ClusterMode = os.Getenv("HD_CLUSTER_MODE") != "" data.StatusInfo.ClusterMode = utils.EnvAsBool("HD_CLUSTER_MODE", false)
if data.StatusInfo.ClusterMode { if data.StatusInfo.ClusterMode {
return nil return nil
} }

View File

@@ -6,6 +6,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
"slices"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -116,3 +117,13 @@ func GetQueryProps(c *gin.Context) (*QueryProps, error) {
return &qp, nil return &qp, nil
} }
func EnvAsBool(envKey string, envDef bool) bool {
validSettableValues := []string{"false", "true", "0", "1"}
envValue := os.Getenv(envKey)
if slices.Contains(validSettableValues, envValue) {
return envValue == "true" || envValue == "1"
} else {
return envDef
}
}

View File

@@ -2,6 +2,7 @@ package utils
import ( import (
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -106,3 +107,53 @@ func TestChartAndVersion(t *testing.T) {
}) })
} }
} }
func TestEnvAsBool(t *testing.T) {
// value: "true" | "1", default: false -> expect true
t.Setenv("TEST", "true")
want := true
if EnvAsBool("TEST", false) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
t.Setenv("TEST", "1")
want = true
if EnvAsBool("TEST", false) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
// value: "false" | "0", default: true -> expect false
t.Setenv("TEST", "false")
want = false
if EnvAsBool("TEST", true) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
t.Setenv("TEST", "0")
want = false
if EnvAsBool("TEST", true) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
// value: "" | *, default: false -> expect false
t.Setenv("TEST", "")
want = false
if EnvAsBool("TEST", false) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
t.Setenv("TEST", "10random")
want = false
if EnvAsBool("TEST", false) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
// value: "" | *, default: true -> expect true
t.Setenv("TEST", "")
want = true
if EnvAsBool("TEST", true) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
t.Setenv("TEST", "10random")
want = true
if EnvAsBool("TEST", true) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
}