Skip to content

Commit dd2dc3d

Browse files
authored
chore: add GetValueOrDefault for environment variables (#984)
## Summary 1. Add `GetValueOrDefault` in package `envir` 2. Use the function in `searcher` and `impl` 3. simplify some functions in `initrec` and `plansdk` ## How was it tested?
1 parent 88feade commit dd2dc3d

File tree

8 files changed

+24
-25
lines changed

8 files changed

+24
-25
lines changed

internal/envir/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ func IsCI() bool {
4747
ci, err := strconv.ParseBool(os.Getenv("CI"))
4848
return ci && err == nil
4949
}
50+
51+
// GetValueOrDefault gets the value of an environment variable.
52+
// If it's empty, it will return the given default value instead.
53+
func GetValueOrDefault(key, def string) string {
54+
val := os.Getenv(key)
55+
if val == "" {
56+
val = def
57+
}
58+
59+
return val
60+
}

internal/impl/devbox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ func (d *Devbox) Shell(ctx context.Context) error {
197197
return err
198198
}
199199

200-
shellStartTime := os.Getenv(envir.DevboxShellStartTime)
201-
if shellStartTime == "" {
202-
shellStartTime = telemetry.UnixTimestampFromTime(telemetry.CommandStartTime())
203-
}
200+
shellStartTime := envir.GetValueOrDefault(
201+
envir.DevboxShellStartTime,
202+
telemetry.UnixTimestampFromTime(telemetry.CommandStartTime()),
203+
)
204204

205205
opts := []ShellOption{
206206
WithHooksFilePath(d.scriptPath(hooksFilename)),

internal/initrec/recommenders/javascript/nodejs.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ type Recommender struct {
2020
var _ recommenders.Recommender = (*Recommender)(nil)
2121

2222
func (r *Recommender) IsRelevant() bool {
23-
packageJSONPath := filepath.Join(r.SrcDir, "package.json")
24-
return fileutil.Exists(packageJSONPath)
23+
return fileutil.Exists(filepath.Join(r.SrcDir, "package.json"))
2524
}
2625

2726
func (r *Recommender) Packages() []string {
@@ -74,8 +73,7 @@ func (r *Recommender) nodeVersion(project *nodeProject) *plansdk.Version {
7473
}
7574

7675
func (r *Recommender) packageManager() string {
77-
yarnPkgLockPath := filepath.Join(r.SrcDir, "yarn.lock")
78-
if fileutil.Exists(yarnPkgLockPath) {
76+
if fileutil.Exists(filepath.Join(r.SrcDir, "yarn.lock")) {
7977
return "yarn"
8078
}
8179
return "npm"

internal/initrec/recommenders/python/python_poetry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *RecommenderPoetry) Packages() []string {
3939
// TODO: This can be generalized to all python planners
4040
func (r *RecommenderPoetry) PythonVersion() *plansdk.Version {
4141
defaultVersion, _ := plansdk.NewVersion("3.10.6")
42-
project := r.PyProject()
42+
project := r.pyProject()
4343

4444
if project == nil {
4545
return defaultVersion
@@ -67,7 +67,7 @@ type pyProject struct {
6767
} `toml:"tool"`
6868
}
6969

70-
func (r *RecommenderPoetry) PyProject() *pyProject {
70+
func (r *RecommenderPoetry) pyProject() *pyProject {
7171
pyProjectPath := filepath.Join(r.SrcDir, "pyproject.toml")
7272
content, err := os.ReadFile(pyProjectPath)
7373
if err != nil {

internal/initrec/recommenders/ruby/ruby.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ func parseRubyVersion(gemfile string) string {
6464
return matches[2]
6565
}
6666
}
67-
if err := s.Err(); err != nil {
68-
return ""
69-
}
70-
return "" // not found
67+
68+
return ""
7169
}

internal/planner/plansdk/version.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ func (v Version) parts() []string {
3535
}
3636

3737
func (v Version) Exact() string {
38-
parts := v.parts()
39-
if len(parts) == 0 {
40-
return ""
41-
}
42-
return strings.Join(parts, "")
38+
return strings.Join(v.parts(), "")
4339
}
4440

4541
func (v Version) Major() string {

internal/searcher/client.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
"os"
1312
"strings"
1413

1514
"go.jetpack.io/devbox/internal/boxcli/usererr"
@@ -20,11 +19,7 @@ import (
2019
const searchAPIEndpoint = "https://search.devbox.sh"
2120

2221
func searchHost() string {
23-
endpoint := searchAPIEndpoint
24-
if os.Getenv(envir.DevboxSearchHost) != "" {
25-
endpoint = os.Getenv(envir.DevboxSearchHost)
26-
}
27-
return endpoint
22+
return envir.GetValueOrDefault(envir.DevboxSearchHost, searchAPIEndpoint)
2823
}
2924

3025
type client struct {

internal/searcher/searcher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/samber/lo"
12+
1213
"go.jetpack.io/devbox/internal/redact"
1314
)
1415

0 commit comments

Comments
 (0)