mirror of https://github.com/dapr/cli.git
Add detection for incompatible flags with --run-file
Signed-off-by: twinguy <twinguy17@gmail.com>
This commit is contained in:
parent
29f8962111
commit
ce0b9fb4d9
49
cmd/run.go
49
cmd/run.go
|
|
@ -78,6 +78,20 @@ const (
|
||||||
runtimeWaitTimeoutInSeconds = 60
|
runtimeWaitTimeoutInSeconds = 60
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Flags that are incompatible with --run-file
|
||||||
|
var runFileIncompatibleFlags = []string{
|
||||||
|
"app-id", "app-port", "app-protocol", "app-max-concurrency",
|
||||||
|
"app-ssl", "app-channel-address", "enable-app-health-check",
|
||||||
|
"app-health-check-path", "app-health-probe-interval",
|
||||||
|
"app-health-probe-timeout", "app-health-threshold",
|
||||||
|
"config", "dapr-http-port", "dapr-grpc-port",
|
||||||
|
"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{
|
||||||
Use: "run",
|
Use: "run",
|
||||||
Short: "Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted",
|
Short: "Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted",
|
||||||
|
|
@ -128,6 +142,14 @@ dapr run --run-file /path/to/directory -k
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if len(runFilePath) > 0 {
|
if len(runFilePath) > 0 {
|
||||||
|
// Check for incompatible flags
|
||||||
|
incompatibleFlags := detectIncompatibleFlags(cmd)
|
||||||
|
if len(incompatibleFlags) > 0 {
|
||||||
|
// Print warning message about incompatible flags
|
||||||
|
warningMsg := generateWarningMessage(incompatibleFlags)
|
||||||
|
print.WarningStatusEvent(os.Stdout, warningMsg)
|
||||||
|
}
|
||||||
|
|
||||||
runConfigFilePath, err := getRunFilePath(runFilePath)
|
runConfigFilePath, err := getRunFilePath(runFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print.FailureStatusEvent(os.Stderr, "Failed to get run file path: %v", err)
|
print.FailureStatusEvent(os.Stderr, "Failed to get run file path: %v", err)
|
||||||
|
|
@ -1061,3 +1083,30 @@ func getRunFilePath(path string) (string, error) {
|
||||||
}
|
}
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detectIncompatibleFlags checks if any incompatible flags are used with --run-file
|
||||||
|
// and returns a slice of the flag names that were used
|
||||||
|
func detectIncompatibleFlags(cmd *cobra.Command) []string {
|
||||||
|
if runFilePath == "" {
|
||||||
|
return nil // No run file specified, so no incompatibilities
|
||||||
|
}
|
||||||
|
|
||||||
|
var incompatibleFlags []string
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateWarningMessage creates an appropriate warning message based on the
|
||||||
|
// number and types of incompatible flags
|
||||||
|
func generateWarningMessage(incompatibleFlags []string) string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"The following flags are ignored when using --run-file and should be configured in the run file instead: %s",
|
||||||
|
strings.Join(incompatibleFlags, ", "))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,3 +18,51 @@ func TestValidateSchedulerHostAddress(t *testing.T) {
|
||||||
assert.Equal(t, "localhost:50006", address)
|
assert.Equal(t, "localhost:50006", address)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDetectIncompatibleFlags(t *testing.T) {
|
||||||
|
// Setup a temporary run file path to trigger the incompatible flag check
|
||||||
|
originalRunFilePath := runFilePath
|
||||||
|
runFilePath = "some/path"
|
||||||
|
defer func() {
|
||||||
|
// Restore the original runFilePath
|
||||||
|
runFilePath = originalRunFilePath
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Run("detect incompatible flags", 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", "", "")
|
||||||
|
cmd.Flags().String("kubernetes", "", "") // Compatible flag
|
||||||
|
|
||||||
|
// Mark flags as changed
|
||||||
|
cmd.Flags().Set("app-id", "myapp")
|
||||||
|
cmd.Flags().Set("dapr-http-port", "3500")
|
||||||
|
cmd.Flags().Set("kubernetes", "true")
|
||||||
|
|
||||||
|
// Test detection
|
||||||
|
incompatibleFlags := detectIncompatibleFlags(cmd)
|
||||||
|
assert.Len(t, incompatibleFlags, 2)
|
||||||
|
assert.Contains(t, incompatibleFlags, "app-id")
|
||||||
|
assert.Contains(t, incompatibleFlags, "dapr-http-port")
|
||||||
|
assert.NotContains(t, incompatibleFlags, "kubernetes")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateWarningMessage(t *testing.T) {
|
||||||
|
t.Run("warning message", func(t *testing.T) {
|
||||||
|
warning := generateWarningMessage([]string{"app-id", "app-port"})
|
||||||
|
assert.Contains(t, warning, "app-id, app-port")
|
||||||
|
assert.Contains(t, warning, "ignored when using --run-file")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("warning message with multiple flags", func(t *testing.T) {
|
||||||
|
warning := generateWarningMessage([]string{
|
||||||
|
"app-id", "app-port", "app-protocol", "app-max-concurrency",
|
||||||
|
"dapr-http-port", "dapr-grpc-port", "resources-path",
|
||||||
|
})
|
||||||
|
assert.Contains(t, warning, "app-id")
|
||||||
|
assert.Contains(t, warning, "dapr-http-port")
|
||||||
|
assert.Contains(t, warning, "ignored when using --run-file")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue