Skip to content

Commit 3198d9c

Browse files
authored
Merge branch 'master' into fix-perfsprint-linter-error
2 parents a85b813 + b51eab0 commit 3198d9c

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

cmd/run.go

Lines changed: 42 additions & 0 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,6 +80,15 @@ const (
7880
runtimeWaitTimeoutInSeconds = 60
7981
)
8082

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",
90+
}
91+
8192
var RunCmd = &cobra.Command{
8293
Use: "run",
8394
Short: "Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted",
@@ -128,6 +139,14 @@ dapr run --run-file /path/to/directory -k
128139
},
129140
Run: func(cmd *cobra.Command, args []string) {
130141
if len(runFilePath) > 0 {
142+
// Check for incompatible flags
143+
incompatibleFlags := detectIncompatibleFlags(cmd)
144+
if len(incompatibleFlags) > 0 {
145+
// Print warning message about incompatible flags
146+
warningMsg := "The following flags are ignored when using --run-file and should be configured in the run file instead: " + strings.Join(incompatibleFlags, ", ")
147+
print.WarningStatusEvent(os.Stdout, warningMsg)
148+
}
149+
131150
runConfigFilePath, err := getRunFilePath(runFilePath)
132151
if err != nil {
133152
print.FailureStatusEvent(os.Stderr, "Failed to get run file path: %v", err)
@@ -1061,3 +1080,26 @@ func getRunFilePath(path string) (string, error) {
10611080
}
10621081
return path, nil
10631082
}
1083+
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+
1096+
// detectIncompatibleFlags checks if any incompatible flags are used with --run-file
1097+
// and returns a slice of the flag names that were used
1098+
func detectIncompatibleFlags(cmd *cobra.Command) []string {
1099+
if runFilePath == "" {
1100+
return nil // No run file specified, so no incompatibilities
1101+
}
1102+
1103+
// Get all flags that are not in the compatible list
1104+
return getConflictingFlags(cmd, append(runFileCompatibleFlags, "run-file")...)
1105+
}

cmd/run_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"testing"
55

6+
"github.com/spf13/cobra"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -17,3 +18,61 @@ func TestValidateSchedulerHostAddress(t *testing.T) {
1718
assert.Equal(t, "localhost:50006", address)
1819
})
1920
}
21+
22+
func TestDetectIncompatibleFlags(t *testing.T) {
23+
// Setup a temporary run file path to trigger the incompatible flag check
24+
originalRunFilePath := runFilePath
25+
runFilePath = "some/path"
26+
defer func() {
27+
// Restore the original runFilePath
28+
runFilePath = originalRunFilePath
29+
}()
30+
31+
t.Run("detect incompatible flags", func(t *testing.T) {
32+
// Create a test command with flags
33+
cmd := &cobra.Command{Use: "test"}
34+
cmd.Flags().String("app-id", "", "")
35+
cmd.Flags().String("dapr-http-port", "", "")
36+
cmd.Flags().String("kubernetes", "", "") // Compatible flag
37+
cmd.Flags().String("runtime-path", "", "") // Compatible flag
38+
cmd.Flags().String("log-as-json", "", "") // Compatible flag
39+
40+
// Mark flags as changed
41+
cmd.Flags().Set("app-id", "myapp")
42+
cmd.Flags().Set("dapr-http-port", "3500")
43+
cmd.Flags().Set("kubernetes", "true")
44+
cmd.Flags().Set("runtime-path", "/path/to/runtime")
45+
cmd.Flags().Set("log-as-json", "true")
46+
47+
// Test detection
48+
incompatibleFlags := detectIncompatibleFlags(cmd)
49+
assert.Len(t, incompatibleFlags, 2)
50+
assert.Contains(t, incompatibleFlags, "app-id")
51+
assert.Contains(t, incompatibleFlags, "dapr-http-port")
52+
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)
77+
})
78+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
2525
github.com/shirou/gopsutil v3.21.11+incompatible
2626
github.com/spf13/cobra v1.8.1
27+
github.com/spf13/pflag v1.0.5
2728
github.com/spf13/viper v1.13.0
2829
github.com/stretchr/testify v1.10.0
2930
golang.org/x/mod v0.22.0
@@ -186,7 +187,6 @@ require (
186187
github.com/spf13/afero v1.8.2 // indirect
187188
github.com/spf13/cast v1.7.0 // indirect
188189
github.com/spf13/jwalterweatherman v1.1.0 // indirect
189-
github.com/spf13/pflag v1.0.5 // indirect
190190
github.com/spiffe/go-spiffe/v2 v2.1.7 // indirect
191191
github.com/stoewer/go-strcase v1.3.0 // indirect
192192
github.com/subosito/gotenv v1.4.1 // indirect

0 commit comments

Comments
 (0)