Skip to content

Commit eb4d56a

Browse files
authored
Merge pull request #6 from tampakrap/fix_config_flag
fix(config): Fix flag parsing with space-separated value
2 parents 10bac99 + 9de9449 commit eb4d56a

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

cmd/crossplane/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ func configFlag(args []string) (string, error) {
141141
continue
142142
}
143143

144-
if v := strings.TrimPrefix(a, "--config="); v != "" {
144+
if v, ok := strings.CutPrefix(a, "--config="); ok {
145+
if v == "" {
146+
return "", errors.New("flag --config requires a value")
147+
}
145148
return v, nil
146149
}
147150

cmd/crossplane/main_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2026 The Crossplane Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
"github.com/google/go-cmp/cmp/cmpopts"
24+
)
25+
26+
func TestConfigFlag(t *testing.T) {
27+
type want struct {
28+
path string
29+
err error
30+
}
31+
32+
cases := map[string]struct {
33+
reason string
34+
args []string
35+
want want
36+
}{
37+
"Empty": {
38+
reason: "No args should yield no path and no error.",
39+
args: nil,
40+
},
41+
"NoConfigFlag": {
42+
reason: "Argv without --config should yield no path and no error.",
43+
args: []string{"version"},
44+
},
45+
"EqualsForm": {
46+
reason: "--config=PATH should return PATH.",
47+
args: []string{"--config=/tmp/config.yaml"},
48+
want: want{path: "/tmp/config.yaml"},
49+
},
50+
"SpaceForm": {
51+
reason: "--config PATH should return PATH from the next argv.",
52+
args: []string{"--config", "/tmp/config.yaml"},
53+
want: want{path: "/tmp/config.yaml"},
54+
},
55+
"EmptyEquals": {
56+
reason: "--config= is an explicitly empty value and should return an error.",
57+
args: []string{"--config="},
58+
want: want{err: cmpopts.AnyError},
59+
},
60+
"MissingValue": {
61+
reason: "Trailing --config with no following argv should return an error.",
62+
args: []string{"--config"},
63+
want: want{err: cmpopts.AnyError},
64+
},
65+
"FirstWins": {
66+
reason: "If --config appears more than once, the first occurrence wins.",
67+
args: []string{"--config=/first.yaml", "--config=/second.yaml"},
68+
want: want{path: "/first.yaml"},
69+
},
70+
}
71+
72+
for name, tc := range cases {
73+
t.Run(name, func(t *testing.T) {
74+
got, err := configFlag(tc.args)
75+
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {
76+
t.Errorf("\n%s\nconfigFlag(%v): -want err, +got err:\n%s", tc.reason, tc.args, diff)
77+
}
78+
if diff := cmp.Diff(tc.want.path, got); diff != "" {
79+
t.Errorf("\n%s\nconfigFlag(%v): -want, +got:\n%s", tc.reason, tc.args, diff)
80+
}
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)