Skip to content

Commit c939814

Browse files
committed
Use compatibleflags approach instead of incompatible
Signed-off-by: twinguy <[email protected]>
1 parent 5da3528 commit c939814

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

cmd/run.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"os"
2121
"path/filepath"
2222
"runtime"
23+
"slices"
2324
"strconv"
2425
"strings"
2526
"time"
2627

2728
"golang.org/x/mod/semver"
2829

2930
"github.com/spf13/cobra"
31+
"github.com/spf13/pflag"
3032
"github.com/spf13/viper"
3133

3234
daprRuntime "github.com/dapr/dapr/pkg/runtime"
@@ -78,18 +80,13 @@ const (
7880
runtimeWaitTimeoutInSeconds = 60
7981
)
8082

81-
// Flags that are incompatible with --run-file
82-
var runFileIncompatibleFlags = []string{
83-
"app-id", "app-port", "app-protocol", "app-max-concurrency",
84-
"app-ssl", "app-channel-address", "enable-app-health-check",
85-
"app-health-check-path", "app-health-probe-interval",
86-
"app-health-probe-timeout", "app-health-threshold",
87-
"config", "dapr-http-port", "dapr-grpc-port",
88-
"dapr-internal-grpc-port", "enable-profiling", "profile-port",
89-
"dapr-http-max-request-size", "dapr-http-read-buffer-size",
90-
"metrics-port", "placement-host-address", "scheduler-host-address",
91-
"components-path", "resources-path", "unix-domain-socket",
92-
"enable-api-logging", "dapr-listen-addresses", "log-level",
83+
// Flags that are compatible with --run-file
84+
var runFileCompatibleFlags = []string{
85+
"kubernetes",
86+
"help",
87+
"version",
88+
"runtime-path",
89+
"log-as-json",
9390
}
9491

9592
var RunCmd = &cobra.Command{
@@ -1084,21 +1081,25 @@ func getRunFilePath(path string) (string, error) {
10841081
return path, nil
10851082
}
10861083

1084+
// getConflictingFlags checks if any flags are set other than the ones passed in the excludedFlags slice.
1085+
// Used for logic or notifications when any of the flags are conflicting and should not be used together.
1086+
func getConflictingFlags(cmd *cobra.Command, excludedFlags ...string) []string {
1087+
var conflictingFlags []string
1088+
cmd.Flags().Visit(func(f *pflag.Flag) {
1089+
if !slices.Contains(excludedFlags, f.Name) {
1090+
conflictingFlags = append(conflictingFlags, f.Name)
1091+
}
1092+
})
1093+
return conflictingFlags
1094+
}
1095+
10871096
// detectIncompatibleFlags checks if any incompatible flags are used with --run-file
10881097
// and returns a slice of the flag names that were used
10891098
func detectIncompatibleFlags(cmd *cobra.Command) []string {
10901099
if runFilePath == "" {
10911100
return nil // No run file specified, so no incompatibilities
10921101
}
10931102

1094-
var incompatibleFlags []string
1095-
1096-
// Check each incompatible flag to see if it was explicitly set
1097-
for _, flagName := range runFileIncompatibleFlags {
1098-
if cmd.Flags().Changed(flagName) {
1099-
incompatibleFlags = append(incompatibleFlags, flagName)
1100-
}
1101-
}
1102-
1103-
return incompatibleFlags
1103+
// Get all flags that are not in the compatible list
1104+
return getConflictingFlags(cmd, append(runFileCompatibleFlags, "run-file")...)
11041105
}

cmd/run_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,46 @@ func TestDetectIncompatibleFlags(t *testing.T) {
3333
cmd := &cobra.Command{Use: "test"}
3434
cmd.Flags().String("app-id", "", "")
3535
cmd.Flags().String("dapr-http-port", "", "")
36-
cmd.Flags().String("kubernetes", "", "") // Compatible flag
36+
cmd.Flags().String("kubernetes", "", "") // Compatible flag
37+
cmd.Flags().String("runtime-path", "", "") // Compatible flag
38+
cmd.Flags().String("log-as-json", "", "") // Compatible flag
3739

3840
// Mark flags as changed
3941
cmd.Flags().Set("app-id", "myapp")
4042
cmd.Flags().Set("dapr-http-port", "3500")
4143
cmd.Flags().Set("kubernetes", "true")
44+
cmd.Flags().Set("runtime-path", "/path/to/runtime")
45+
cmd.Flags().Set("log-as-json", "true")
4246

4347
// Test detection
4448
incompatibleFlags := detectIncompatibleFlags(cmd)
4549
assert.Len(t, incompatibleFlags, 2)
4650
assert.Contains(t, incompatibleFlags, "app-id")
4751
assert.Contains(t, incompatibleFlags, "dapr-http-port")
4852
assert.NotContains(t, incompatibleFlags, "kubernetes")
53+
assert.NotContains(t, incompatibleFlags, "runtime-path")
54+
assert.NotContains(t, incompatibleFlags, "log-as-json")
55+
})
56+
57+
t.Run("no incompatible flags when run file not specified", func(t *testing.T) {
58+
// Create a test command with flags
59+
cmd := &cobra.Command{Use: "test"}
60+
cmd.Flags().String("app-id", "", "")
61+
cmd.Flags().String("dapr-http-port", "", "")
62+
63+
// Mark flags as changed
64+
cmd.Flags().Set("app-id", "myapp")
65+
cmd.Flags().Set("dapr-http-port", "3500")
66+
67+
// Temporarily clear runFilePath
68+
originalRunFilePath := runFilePath
69+
runFilePath = ""
70+
defer func() {
71+
runFilePath = originalRunFilePath
72+
}()
73+
74+
// Test detection
75+
incompatibleFlags := detectIncompatibleFlags(cmd)
76+
assert.Nil(t, incompatibleFlags)
4977
})
5078
}

0 commit comments

Comments
 (0)