Skip to content

Commit

Permalink
Added greater granularity for boolean environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrodetta committed Feb 22, 2024
1 parent 3d7f907 commit 5ebc02a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"errors"
"fmt"
"github.com/joomcode/errorx"
"os"
"os/signal"
"strings"
"syscall"

"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func main() {
opts.BindHost = host
}

opts.Verbose = opts.Verbose || os.Getenv("DEBUG") != ""
opts.Verbose = opts.Verbose || env.ParseEnvAsBool("DEBUG", false)
setupLogging(opts.Verbose)

server := dashboard.Server{
Expand Down
4 changes: 2 additions & 2 deletions pkg/dashboard/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"html"
"net/http"
"os"
"path"

"github.com/gin-gonic/gin"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/komodorio/helm-dashboard/pkg/dashboard/handlers"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
"github.com/komodorio/helm-dashboard/pkg/frontend"
Expand Down Expand Up @@ -95,7 +95,7 @@ func NewRouter(abortWeb context.CancelFunc, data *objects.DataLayer, debug bool)
api.Use(errorHandler)
api.Use(corsMiddleware())

if os.Getenv("HD_CORS") != "" {
if env.ParseEnvAsBool("HD_CORS", false) {
api.Use(allowCORS)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/dashboard/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/komodorio/helm-dashboard/pkg/dashboard/handlers"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
log "github.com/sirupsen/logrus"
Expand All @@ -27,7 +28,7 @@ var inMemStorage *storage.Storage
var repoFile string

func TestMain(m *testing.M) { // fixture to set logging level via env variable
if os.Getenv("DEBUG") != "" {
if env.ParseEnvAsBool("DEBUG", false) {
log.SetLevel(log.DebugLevel)
log.Debugf("Set logging level")
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/dashboard/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package env

import (
"os"
"slices"
)

func ParseEnvAsBool(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
}
}
56 changes: 56 additions & 0 deletions pkg/dashboard/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package env

import (
"os"
"testing"
)

func TestParseEnvAsBool(t *testing.T) {
// value: "true" | "1", default: false -> expect true
t.Setenv("TEST", "true")
want := true
if ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("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 ParseEnvAsBool("TEST", true) != want {
t.Errorf("Env 'TEST' value '%v' should be parsed to %v", os.Getenv("TEST"), want)
}
}
4 changes: 2 additions & 2 deletions pkg/dashboard/objects/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package objects
import (
"context"
"encoding/json"
"os"
"strings"
"sync"
"time"

"io"

"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -193,7 +193,7 @@ func (d *DataLayer) nsForCtx(ctx string) string {
}

func (d *DataLayer) PeriodicTasks(ctx context.Context) {
if os.Getenv("HD_NO_AUTOUPDATE") == "" {
if !env.ParseEnvAsBool("HD_NO_AUTOUPDATE", false) {
// auto-update repos
go d.loopUpdateRepos(ctx, 10*time.Minute) // TODO: parameterize interval?
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/dashboard/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
Expand Down Expand Up @@ -38,9 +39,7 @@ func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (st
}

data.LocalCharts = s.LocalCharts

isDevModeWithAnalytics := os.Getenv("HD_DEV_ANALYTICS") == "true"
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || isDevModeWithAnalytics
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || env.ParseEnvAsBool("HD_DEV_ANALYTICS", false)

err = s.detectClusterMode(data)
if err != nil {
Expand All @@ -58,7 +57,7 @@ func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (st
}

func (s *Server) detectClusterMode(data *objects.DataLayer) error {
data.StatusInfo.ClusterMode = os.Getenv("HD_CLUSTER_MODE") != ""
data.StatusInfo.ClusterMode = env.ParseEnvAsBool("HD_CLUSTER_MODE", false)
if data.StatusInfo.ClusterMode {
return nil
}
Expand Down

0 comments on commit 5ebc02a

Please sign in to comment.