Skip to content

Commit 412feb8

Browse files
committed
feat: add feature flags functionality + tests
1 parent 1b52473 commit 412feb8

File tree

6 files changed

+115
-12
lines changed

6 files changed

+115
-12
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
GO_VERSION := 1.22
55
GO_SYSTEM_VERSION := $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1-2)
66
REQUIRE_GO_VERSION := $(GO_VERSION)
7-
ENABLED_FLAGS :=
7+
ENABLED_FLAGS := minitia_launch
88

99
# Project version
1010
WEAVE_VERSION := v0.0.1

cmd/root.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spf13/cobra"
77
"github.com/spf13/viper"
88

9+
"github.com/initia-labs/weave/flags"
910
"github.com/initia-labs/weave/utils"
1011
)
1112

@@ -28,7 +29,9 @@ func Execute() error {
2829

2930
rootCmd.AddCommand(InitCommand())
3031
rootCmd.AddCommand(InitiaCommand())
31-
rootCmd.AddCommand(MinitiaCommand())
32+
if flags.IsEnabled(flags.MinitiaLaunch) {
33+
rootCmd.AddCommand(MinitiaCommand())
34+
}
3235

3336
return rootCmd.ExecuteContext(context.Background())
3437
}

flags/flag.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var EnabledFlags string
77
type FeatureFlag string
88

99
const (
10-
MinitiaLaunch FeatureFlag = "minitia_launch"
11-
OPInitBotsExecutor FeatureFlag = "opinit-bots-executor"
12-
Relayer FeatureFlag = "relayer"
10+
MinitiaLaunch FeatureFlag = "minitia_launch"
11+
OPInitBots FeatureFlag = "opinit_bots"
12+
Relayer FeatureFlag = "relayer"
1313
)
1414

1515
func IsEnabled(flag FeatureFlag) bool {

flags/flag_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package flags
2+
3+
import (
4+
"testing"
5+
6+
"github.com/test-go/testify/assert"
7+
)
8+
9+
func TestIsEnabled(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
enabledFlags string
13+
flag FeatureFlag
14+
expectedValue bool
15+
}{
16+
{
17+
name: "Single flag enabled",
18+
enabledFlags: "minitia_launch",
19+
flag: MinitiaLaunch,
20+
expectedValue: true,
21+
},
22+
{
23+
name: "Multiple flags enabled, target flag present",
24+
enabledFlags: "minitia_launch,opinit_bots,relayer",
25+
flag: OPInitBots,
26+
expectedValue: true,
27+
},
28+
{
29+
name: "Multiple flags enabled, target flag not present",
30+
enabledFlags: "minitia_launch,opinit_bots",
31+
flag: Relayer,
32+
expectedValue: false,
33+
},
34+
{
35+
name: "No flags enabled",
36+
enabledFlags: "",
37+
flag: MinitiaLaunch,
38+
expectedValue: false,
39+
},
40+
}
41+
42+
for _, tc := range testCases {
43+
t.Run(tc.name, func(t *testing.T) {
44+
EnabledFlags = tc.enabledFlags
45+
result := IsEnabled(tc.flag)
46+
assert.Equal(t, tc.expectedValue, result)
47+
})
48+
}
49+
}

models/weaveinit/weaveinit.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package weaveinit
33
import (
44
tea "github.com/charmbracelet/bubbletea"
55

6+
"github.com/initia-labs/weave/flags"
67
"github.com/initia-labs/weave/models/initia"
78
"github.com/initia-labs/weave/models/minitia"
89
"github.com/initia-labs/weave/styles"
@@ -22,16 +23,31 @@ const (
2223
StartRelayerOption WeaveInitOption = "Start a Relayer"
2324
)
2425

26+
func GetWeaveInitOptions() []WeaveInitOption {
27+
options := []WeaveInitOption{
28+
RunL1NodeOption,
29+
}
30+
31+
if flags.IsEnabled(flags.MinitiaLaunch) {
32+
options = append(options, LaunchNewMinitiaOption)
33+
}
34+
35+
if flags.IsEnabled(flags.OPInitBots) {
36+
options = append(options, InitializeOPBotsOption)
37+
}
38+
39+
if flags.IsEnabled(flags.Relayer) {
40+
options = append(options, StartRelayerOption)
41+
}
42+
43+
return options
44+
}
45+
2546
func NewWeaveInit() *WeaveInit {
2647
return &WeaveInit{
2748
Selector: utils.Selector[WeaveInitOption]{
28-
Options: []WeaveInitOption{
29-
RunL1NodeOption,
30-
LaunchNewMinitiaOption,
31-
InitializeOPBotsOption,
32-
StartRelayerOption,
33-
},
34-
Cursor: 0,
49+
Options: GetWeaveInitOptions(),
50+
Cursor: 0,
3551
},
3652
}
3753
}

models/weaveinit/weaveinit_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package weaveinit
2+
3+
import (
4+
"testing"
5+
6+
"github.com/initia-labs/weave/flags"
7+
)
8+
9+
func TestGetWeaveInitOptions(t *testing.T) {
10+
// Test with different flag combinations
11+
testCases := []struct {
12+
flags string
13+
expected []WeaveInitOption
14+
}{
15+
{"", []WeaveInitOption{RunL1NodeOption}},
16+
{"minitia_launch", []WeaveInitOption{RunL1NodeOption, LaunchNewMinitiaOption}},
17+
{"minitia_launch,opinit_bots", []WeaveInitOption{RunL1NodeOption, LaunchNewMinitiaOption, InitializeOPBotsOption}},
18+
{"minitia_launch,opinit_bots,relayer", []WeaveInitOption{RunL1NodeOption, LaunchNewMinitiaOption, InitializeOPBotsOption, StartRelayerOption}},
19+
}
20+
21+
for _, tc := range testCases {
22+
flags.EnabledFlags = tc.flags
23+
options := GetWeaveInitOptions()
24+
25+
if len(options) != len(tc.expected) {
26+
t.Errorf("For flags '%s': Expected %d options, but got %d", tc.flags, len(tc.expected), len(options))
27+
}
28+
29+
for i, option := range options {
30+
if option != tc.expected[i] {
31+
t.Errorf("For flags '%s': Expected option %d to be %s, but got %s", tc.flags, i, tc.expected[i], option)
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)