Skip to content

Commit

Permalink
Improved solution as reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrodetta committed Feb 20, 2024
1 parent 5710fb5 commit ee11461
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 83 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"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/komodorio/helm-dashboard/pkg/dashboard/utils"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func main() {
opts.BindHost = host
}

opts.Verbose = opts.Verbose || env.ParseEnvAsBool("DEBUG", false)
opts.Verbose = opts.Verbose || utils.EnvAsBool("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 @@ -7,9 +7,9 @@ import (
"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/dashboard/utils"
"github.com/komodorio/helm-dashboard/pkg/frontend"
log "github.com/sirupsen/logrus"
)
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 env.ParseEnvAsBool("HD_CORS", false) {
if utils.EnvAsBool("HD_CORS", false) {
api.Use(allowCORS)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/dashboard/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
log "github.com/sirupsen/logrus"
"gotest.tools/v3/assert"
"helm.sh/helm/v3/pkg/action"
Expand All @@ -28,7 +28,7 @@ var inMemStorage *storage.Storage
var repoFile string

func TestMain(m *testing.M) { // fixture to set logging level via env variable
if env.ParseEnvAsBool("DEBUG", false) {
if utils.EnvAsBool("DEBUG", false) {
log.SetLevel(log.DebugLevel)
log.Debugf("Set logging level")
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/dashboard/env/env.go

This file was deleted.

56 changes: 0 additions & 56 deletions pkg/dashboard/env/env_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/dashboard/objects/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"io"

"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard/env"
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
"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 !env.ParseEnvAsBool("HD_NO_AUTOUPDATE", false) {
if !utils.EnvAsBool("HD_NO_AUTOUPDATE", false) {
// auto-update repos
go d.loopUpdateRepos(ctx, 10*time.Minute) // TODO: parameterize interval?
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/dashboard/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -39,7 +38,7 @@ func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (st
}

data.LocalCharts = s.LocalCharts
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || env.ParseEnvAsBool("HD_DEV_ANALYTICS", false)
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || utils.EnvAsBool("HD_DEV_ANALYTICS", false)

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

func (s *Server) detectClusterMode(data *objects.DataLayer) error {
data.StatusInfo.ClusterMode = env.ParseEnvAsBool("HD_CLUSTER_MODE", false)
data.StatusInfo.ClusterMode = utils.EnvAsBool("HD_CLUSTER_MODE", false)
if data.StatusInfo.ClusterMode {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/dashboard/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -116,3 +117,13 @@ func GetQueryProps(c *gin.Context) (*QueryProps, error) {

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
}
}
51 changes: 51 additions & 0 deletions pkg/dashboard/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"net/http/httptest"
"os"
"testing"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit ee11461

Please sign in to comment.