mirror of https://github.com/dapr/cli.git
Use compatibleflags approach instead of incompatible
Signed-off-by: twinguy <twinguy17@gmail.com>
This commit is contained in:
parent
5da3528524
commit
c939814420
45
cmd/run.go
45
cmd/run.go
|
@ -20,6 +20,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -27,6 +28,7 @@ import (
|
||||||
"golang.org/x/mod/semver"
|
"golang.org/x/mod/semver"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
daprRuntime "github.com/dapr/dapr/pkg/runtime"
|
daprRuntime "github.com/dapr/dapr/pkg/runtime"
|
||||||
|
@ -78,18 +80,13 @@ const (
|
||||||
runtimeWaitTimeoutInSeconds = 60
|
runtimeWaitTimeoutInSeconds = 60
|
||||||
)
|
)
|
||||||
|
|
||||||
// Flags that are incompatible with --run-file
|
// Flags that are compatible with --run-file
|
||||||
var runFileIncompatibleFlags = []string{
|
var runFileCompatibleFlags = []string{
|
||||||
"app-id", "app-port", "app-protocol", "app-max-concurrency",
|
"kubernetes",
|
||||||
"app-ssl", "app-channel-address", "enable-app-health-check",
|
"help",
|
||||||
"app-health-check-path", "app-health-probe-interval",
|
"version",
|
||||||
"app-health-probe-timeout", "app-health-threshold",
|
"runtime-path",
|
||||||
"config", "dapr-http-port", "dapr-grpc-port",
|
"log-as-json",
|
||||||
"dapr-internal-grpc-port", "enable-profiling", "profile-port",
|
|
||||||
"dapr-http-max-request-size", "dapr-http-read-buffer-size",
|
|
||||||
"metrics-port", "placement-host-address", "scheduler-host-address",
|
|
||||||
"components-path", "resources-path", "unix-domain-socket",
|
|
||||||
"enable-api-logging", "dapr-listen-addresses", "log-level",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var RunCmd = &cobra.Command{
|
var RunCmd = &cobra.Command{
|
||||||
|
@ -1084,6 +1081,18 @@ func getRunFilePath(path string) (string, error) {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getConflictingFlags checks if any flags are set other than the ones passed in the excludedFlags slice.
|
||||||
|
// Used for logic or notifications when any of the flags are conflicting and should not be used together.
|
||||||
|
func getConflictingFlags(cmd *cobra.Command, excludedFlags ...string) []string {
|
||||||
|
var conflictingFlags []string
|
||||||
|
cmd.Flags().Visit(func(f *pflag.Flag) {
|
||||||
|
if !slices.Contains(excludedFlags, f.Name) {
|
||||||
|
conflictingFlags = append(conflictingFlags, f.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return conflictingFlags
|
||||||
|
}
|
||||||
|
|
||||||
// detectIncompatibleFlags checks if any incompatible flags are used with --run-file
|
// detectIncompatibleFlags checks if any incompatible flags are used with --run-file
|
||||||
// and returns a slice of the flag names that were used
|
// and returns a slice of the flag names that were used
|
||||||
func detectIncompatibleFlags(cmd *cobra.Command) []string {
|
func detectIncompatibleFlags(cmd *cobra.Command) []string {
|
||||||
|
@ -1091,14 +1100,6 @@ func detectIncompatibleFlags(cmd *cobra.Command) []string {
|
||||||
return nil // No run file specified, so no incompatibilities
|
return nil // No run file specified, so no incompatibilities
|
||||||
}
|
}
|
||||||
|
|
||||||
var incompatibleFlags []string
|
// Get all flags that are not in the compatible list
|
||||||
|
return getConflictingFlags(cmd, append(runFileCompatibleFlags, "run-file")...)
|
||||||
// Check each incompatible flag to see if it was explicitly set
|
|
||||||
for _, flagName := range runFileIncompatibleFlags {
|
|
||||||
if cmd.Flags().Changed(flagName) {
|
|
||||||
incompatibleFlags = append(incompatibleFlags, flagName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return incompatibleFlags
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,12 +33,16 @@ func TestDetectIncompatibleFlags(t *testing.T) {
|
||||||
cmd := &cobra.Command{Use: "test"}
|
cmd := &cobra.Command{Use: "test"}
|
||||||
cmd.Flags().String("app-id", "", "")
|
cmd.Flags().String("app-id", "", "")
|
||||||
cmd.Flags().String("dapr-http-port", "", "")
|
cmd.Flags().String("dapr-http-port", "", "")
|
||||||
cmd.Flags().String("kubernetes", "", "") // Compatible flag
|
cmd.Flags().String("kubernetes", "", "") // Compatible flag
|
||||||
|
cmd.Flags().String("runtime-path", "", "") // Compatible flag
|
||||||
|
cmd.Flags().String("log-as-json", "", "") // Compatible flag
|
||||||
|
|
||||||
// Mark flags as changed
|
// Mark flags as changed
|
||||||
cmd.Flags().Set("app-id", "myapp")
|
cmd.Flags().Set("app-id", "myapp")
|
||||||
cmd.Flags().Set("dapr-http-port", "3500")
|
cmd.Flags().Set("dapr-http-port", "3500")
|
||||||
cmd.Flags().Set("kubernetes", "true")
|
cmd.Flags().Set("kubernetes", "true")
|
||||||
|
cmd.Flags().Set("runtime-path", "/path/to/runtime")
|
||||||
|
cmd.Flags().Set("log-as-json", "true")
|
||||||
|
|
||||||
// Test detection
|
// Test detection
|
||||||
incompatibleFlags := detectIncompatibleFlags(cmd)
|
incompatibleFlags := detectIncompatibleFlags(cmd)
|
||||||
|
@ -46,5 +50,29 @@ func TestDetectIncompatibleFlags(t *testing.T) {
|
||||||
assert.Contains(t, incompatibleFlags, "app-id")
|
assert.Contains(t, incompatibleFlags, "app-id")
|
||||||
assert.Contains(t, incompatibleFlags, "dapr-http-port")
|
assert.Contains(t, incompatibleFlags, "dapr-http-port")
|
||||||
assert.NotContains(t, incompatibleFlags, "kubernetes")
|
assert.NotContains(t, incompatibleFlags, "kubernetes")
|
||||||
|
assert.NotContains(t, incompatibleFlags, "runtime-path")
|
||||||
|
assert.NotContains(t, incompatibleFlags, "log-as-json")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("no incompatible flags when run file not specified", func(t *testing.T) {
|
||||||
|
// Create a test command with flags
|
||||||
|
cmd := &cobra.Command{Use: "test"}
|
||||||
|
cmd.Flags().String("app-id", "", "")
|
||||||
|
cmd.Flags().String("dapr-http-port", "", "")
|
||||||
|
|
||||||
|
// Mark flags as changed
|
||||||
|
cmd.Flags().Set("app-id", "myapp")
|
||||||
|
cmd.Flags().Set("dapr-http-port", "3500")
|
||||||
|
|
||||||
|
// Temporarily clear runFilePath
|
||||||
|
originalRunFilePath := runFilePath
|
||||||
|
runFilePath = ""
|
||||||
|
defer func() {
|
||||||
|
runFilePath = originalRunFilePath
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Test detection
|
||||||
|
incompatibleFlags := detectIncompatibleFlags(cmd)
|
||||||
|
assert.Nil(t, incompatibleFlags)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue