Add detection for incompatible flags with --run-file

Signed-off-by: twinguy <twinguy17@gmail.com>
This commit is contained in:
twinguy 2025-03-23 23:39:44 -05:00
parent 29f8962111
commit ce0b9fb4d9
No known key found for this signature in database
GPG Key ID: 70E5665641CA5676
2 changed files with 98 additions and 0 deletions

View File

@ -78,6 +78,20 @@ const (
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{
Use: "run",
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) {
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)
if err != nil {
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
}
// 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, ", "))
}

View File

@ -3,6 +3,7 @@ package cmd
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
@ -17,3 +18,51 @@ func TestValidateSchedulerHostAddress(t *testing.T) {
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")
})
}