Skip to content

Commit 3c2a20f

Browse files
authored
[featureflags] Simplify feature flags (#325)
## Summary * Simplifies feature flag lib by using a single const as the name and the feature itself. * Removes the need for `init` * Makes callsites more concise * Remove `NixpkgVersion` flag ## How was it tested? unit tests
1 parent 4459e87 commit 3c2a20f

File tree

9 files changed

+38
-57
lines changed

9 files changed

+38
-57
lines changed

boxcli/featureflag/feature.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ type feature struct {
1414

1515
var features = map[string]*feature{}
1616

17-
func disabled(name string) {
18-
features[name] = &feature{name: name}
19-
}
20-
21-
func enabled(name string) {
22-
features[name] = &feature{name: name, enabled: true}
17+
func disabled(name string) *feature {
18+
if features[name] == nil {
19+
features[name] = &feature{name: name}
20+
}
21+
features[name].enabled = false
22+
return features[name]
2323
}
2424

25-
func Get(name string) *feature {
25+
func enabled(name string) *feature {
26+
if features[name] == nil {
27+
features[name] = &feature{name: name}
28+
}
29+
features[name].enabled = true
2630
return features[name]
2731
}
2832

boxcli/featureflag/feature_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,33 @@ package featureflag
33
import "testing"
44

55
func TestEnabledFeature(t *testing.T) {
6-
enabled("TEST")
7-
if !Get("TEST").Enabled() {
8-
t.Errorf("got Get(%q).Enabled() = false, want true.", "TEST")
6+
name := "TestEnabledFeature"
7+
enabled(name)
8+
if !features[name].Enabled() {
9+
t.Errorf("got %s.Enabled() = false, want true.", name)
910
}
1011
}
1112

1213
func TestDisabledFeature(t *testing.T) {
13-
disabled("TEST")
14-
if Get("TEST").Enabled() {
15-
t.Errorf("got Get(%q).Enabled() = true, want false.", "TEST")
14+
name := "TestDisabledFeature"
15+
disabled(name)
16+
if features[name].Enabled() {
17+
t.Errorf("got %s.Enabled() = true, want false.", name)
1618
}
1719
}
1820

1921
func TestEnabledFeatureEnv(t *testing.T) {
20-
disabled("TEST")
21-
t.Setenv("DEVBOX_FEATURE_TEST", "1")
22-
if !Get("TEST").Enabled() {
23-
t.Errorf("got Get(%q).Enabled() = false, want true.", "TEST")
22+
name := "TestEnabledFeatureEnv"
23+
disabled(name)
24+
t.Setenv("DEVBOX_FEATURE_"+name, "1")
25+
if !features[name].Enabled() {
26+
t.Errorf("got %s.Enabled() = false, want true.", name)
2427
}
2528
}
2629

2730
func TestNonExistentFeature(t *testing.T) {
28-
if Get("TEST").Enabled() {
29-
t.Errorf("got Get(%q).Enabled() = true, want false.", "TEST")
31+
name := "TestNonExistentFeature"
32+
if features[name].Enabled() {
33+
t.Errorf("got %s.Enabled() = true, want false.", name)
3034
}
3135
}

boxcli/featureflag/nixpkgversion.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

boxcli/featureflag/pkgconfig.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
package featureflag
22

3-
const PKGConfig = "PKG_CONFIG" // DEVBOX_FEATURE_PKG_CONFIG
4-
5-
func init() {
6-
disabled(PKGConfig)
7-
}
3+
var PKGConfig = disabled("PKG_CONFIG") // DEVBOX_FEATURE_PKG_CONFIG

boxcli/info.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ type infoCmdFlags struct {
1818

1919
func InfoCmd() *cobra.Command {
2020
flags := infoCmdFlags{}
21-
2221
command := &cobra.Command{
2322
Use: "info <pkg>",
24-
Hidden: !featureflag.Get(featureflag.PKGConfig).Enabled(),
23+
Hidden: !featureflag.PKGConfig.Enabled(),
2524
Short: "Display package info",
2625
Args: cobra.ExactArgs(1),
2726
PersistentPreRunE: nixShellPersistentPreRunE,

config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"unicode"
1313

1414
"github.com/pkg/errors"
15-
"go.jetpack.io/devbox/boxcli/featureflag"
1615
"go.jetpack.io/devbox/boxcli/usererr"
1716
"go.jetpack.io/devbox/cuecfg"
1817
"go.jetpack.io/devbox/debug"
@@ -76,7 +75,7 @@ func ReadConfig(path string) (*Config, error) {
7675
}
7776

7877
func upgradeConfig(cfg *Config, absFilePath string) error {
79-
if cfg.Nixpkgs.Commit == "" && featureflag.Get(featureflag.NixpkgVersion).Enabled() {
78+
if cfg.Nixpkgs.Commit == "" {
8079
// For now, we add the hardcoded value corresponding to the commit hash as of 2022-08-16 in:
8180
// `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
8281
// In the near future, this will be changed to the commit-hash of the unstable tag in nixpkgs github repository

devbox.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ const (
4040
func InitConfig(dir string) (created bool, err error) {
4141
cfgPath := filepath.Join(dir, configFilename)
4242

43-
config := &Config{}
44-
if featureflag.Get(featureflag.NixpkgVersion).Enabled() {
45-
config.Nixpkgs = NixpkgsConfig{
43+
config := &Config{
44+
Nixpkgs: NixpkgsConfig{
4645
Commit: plansdk.DefaultNixpkgsCommit,
47-
}
46+
},
4847
}
4948
return cuecfg.InitFile(cfgPath, config)
5049
}
@@ -115,7 +114,7 @@ func (d *Devbox) Add(pkgs ...string) error {
115114
if err := d.ensurePackagesAreInstalled(install); err != nil {
116115
return err
117116
}
118-
if featureflag.Get(featureflag.PKGConfig).Enabled() {
117+
if featureflag.PKGConfig.Enabled() {
119118
for _, pkg := range pkgs {
120119
if err := pkgcfg.PrintReadme(pkg, d.configDir, d.writer, IsDevboxShellEnabled()); err != nil {
121120
return err
@@ -147,7 +146,7 @@ func (d *Devbox) Remove(pkgs ...string) error {
147146
return err
148147
}
149148

150-
if featureflag.Get(featureflag.PKGConfig).Enabled() {
149+
if featureflag.PKGConfig.Enabled() {
151150
if err := pkgcfg.Remove(d.configDir, uninstalledPackages); err != nil {
152151
return err
153152
}
@@ -248,7 +247,7 @@ func (d *Devbox) Shell() error {
248247
nix.WithConfigDir(d.configDir),
249248
}
250249

251-
if featureflag.Get(featureflag.PKGConfig).Enabled() {
250+
if featureflag.PKGConfig.Enabled() {
252251
env, err := pkgcfg.Env(plan.DevPackages, d.configDir)
253252
if err != nil {
254253
return err
@@ -458,7 +457,7 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
458457
}
459458
fmt.Println("done.")
460459

461-
if featureflag.Get(featureflag.PKGConfig).Enabled() {
460+
if featureflag.PKGConfig.Enabled() {
462461
if err := pkgcfg.RemoveInvalidSymlinks(d.configDir); err != nil {
463462
return err
464463
}

generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func generateForShell(rootPath string, plan *plansdk.ShellPlan) error {
5050
}
5151
}
5252

53-
if featureflag.Get(featureflag.PKGConfig).Enabled() {
53+
if featureflag.PKGConfig.Enabled() {
5454
for _, pkg := range plan.DevPackages {
5555
if err := pkgcfg.CreateFilesAndShowReadme(pkg, rootPath); err != nil {
5656
return err

planner/plansdk/plansdk.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/imdario/mergo"
1212
"github.com/pkg/errors"
13-
"go.jetpack.io/devbox/boxcli/featureflag"
1413
"go.jetpack.io/devbox/pkgslice"
1514
)
1615

@@ -223,18 +222,6 @@ type NixpkgsInfo struct {
223222
const DefaultNixpkgsCommit = "af9e00071d0971eb292fd5abef334e66eda3cb69"
224223

225224
func GetNixpkgsInfo(commitHash string) (*NixpkgsInfo, error) {
226-
227-
// If the featureflag is OFF, then we fallback to the hardcoded commit
228-
// and ignore any value set in the devbox.json
229-
if !featureflag.Get(featureflag.NixpkgVersion).Enabled() {
230-
// sha256 from:
231-
// nix-prefetch-url --unpack https://github.com/nixos/nixpkgs/archive/<commit-hash>.tar.gz
232-
return &NixpkgsInfo{
233-
URL: fmt.Sprintf("https://github.com/nixos/nixpkgs/archive/%s.tar.gz", DefaultNixpkgsCommit),
234-
Sha256: "1mdwy0419m5i9ss6s5frbhgzgyccbwycxm5nal40c8486bai0hwy",
235-
}, nil
236-
}
237-
238225
return &NixpkgsInfo{
239226
URL: fmt.Sprintf("https://github.com/nixos/nixpkgs/archive/%s.tar.gz", commitHash),
240227
}, nil

0 commit comments

Comments
 (0)