@@ -97,13 +97,9 @@ func RegisterCreate(cmd *cobra.Command, commentPrefix string) {
97
97
98
98
flags .Bool ("plain" , false , commentPrefix + "Plain mode. Disables mounts, port forwarding, containerd, etc." )
99
99
100
- flags .StringSlice ("port-forward" , nil , commentPrefix + "Port forwards (host:guest), e.g., '8080:80,2222:22' " )
100
+ flags .StringArray ("port-forward" , nil , commentPrefix + "Port forwards (host:guest), e.g., '8080:80' or '9090:9090,static=true' for static port-forwards " )
101
101
_ = cmd .RegisterFlagCompletionFunc ("port-forward" , func (* cobra.Command , []string , string ) ([]string , cobra.ShellCompDirective ) {
102
- return []string {"8080:80" , "3000:3000" }, cobra .ShellCompDirectiveNoFileComp
103
- })
104
- flags .StringSlice ("static-port-forward" , nil , commentPrefix + "Static port forwards (host:guest), works even in plain mode, e.g., '8080:80,2222:22'" )
105
- _ = cmd .RegisterFlagCompletionFunc ("static-port-forward" , func (* cobra.Command , []string , string ) ([]string , cobra.ShellCompDirective ) {
106
- return []string {"8080:80" , "3000:3000" }, cobra .ShellCompDirectiveNoFileComp
102
+ return []string {"8080:80" , "3000:3000" , "8080:80,static=true" }, cobra .ShellCompDirectiveNoFileComp
107
103
})
108
104
}
109
105
@@ -113,6 +109,56 @@ func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) {
113
109
}
114
110
}
115
111
112
+ func ParsePortForward (spec string ) (hostPort , guestPort string , isStatic bool , err error ) {
113
+ parts := strings .Split (spec , "," )
114
+ if len (parts ) > 2 {
115
+ return "" , "" , false , fmt .Errorf ("invalid port forward format %q, expected HOST:GUEST or HOST:GUEST,static=true" , spec )
116
+ }
117
+
118
+ portParts := strings .Split (strings .TrimSpace (parts [0 ]), ":" )
119
+ if len (portParts ) != 2 {
120
+ return "" , "" , false , fmt .Errorf ("invalid port forward format %q, expected HOST:GUEST" , parts [0 ])
121
+ }
122
+
123
+ hostPort = strings .TrimSpace (portParts [0 ])
124
+ guestPort = strings .TrimSpace (portParts [1 ])
125
+
126
+ if len (parts ) == 2 {
127
+ staticPart := strings .TrimSpace (parts [1 ])
128
+ if strings .HasPrefix (staticPart , "static=" ) {
129
+ staticValue := strings .TrimPrefix (staticPart , "static=" )
130
+ isStatic , err = strconv .ParseBool (staticValue )
131
+ if err != nil {
132
+ return "" , "" , false , fmt .Errorf ("invalid value for static parameter: %q" , staticValue )
133
+ }
134
+ } else {
135
+ return "" , "" , false , fmt .Errorf ("invalid parameter %q, expected 'static=' followed by a boolean value" , staticPart )
136
+ }
137
+ }
138
+
139
+ return hostPort , guestPort , isStatic , nil
140
+ }
141
+
142
+ func BuildPortForwardExpression (portForwards []string ) (string , error ) {
143
+ if len (portForwards ) == 0 {
144
+ return "" , nil
145
+ }
146
+
147
+ expr := `.portForwards += [`
148
+ for i , spec := range portForwards {
149
+ hostPort , guestPort , isStatic , err := ParsePortForward (spec )
150
+ if err != nil {
151
+ return "" , err
152
+ }
153
+ expr += fmt .Sprintf (`{"guestPort": %q, "hostPort": %q, "static": %v}` , guestPort , hostPort , isStatic )
154
+ if i < len (portForwards )- 1 {
155
+ expr += ","
156
+ }
157
+ }
158
+ expr += `]`
159
+ return expr , nil
160
+ }
161
+
116
162
// YQExpressions returns YQ expressions.
117
163
func YQExpressions (flags * flag.FlagSet , newInstance bool ) ([]string , error ) {
118
164
type def struct {
@@ -272,31 +318,13 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
272
318
{"vm-type" , d (".vmType = %q" ), true , false },
273
319
{"plain" , d (".plain = %s" ), true , false },
274
320
{
275
- "static- port-forward" ,
321
+ "port-forward" ,
276
322
func (_ * flag.Flag ) (string , error ) {
277
- ss , err := flags .GetStringSlice ( "static- port-forward" )
323
+ ss , err := flags .GetStringArray ( " port-forward" )
278
324
if err != nil {
279
325
return "" , err
280
326
}
281
- if len (ss ) == 0 {
282
- return "" , nil
283
- }
284
-
285
- expr := `.portForwards += [`
286
- for i , s := range ss {
287
- parts := strings .Split (s , ":" )
288
- if len (parts ) != 2 {
289
- return "" , fmt .Errorf ("invalid static port forward format %q, expected HOST:GUEST" , s )
290
- }
291
- guestPort := strings .TrimSpace (parts [0 ])
292
- hostPort := strings .TrimSpace (parts [1 ])
293
- expr += fmt .Sprintf (`{"guestPort": %s, "hostPort": %s}` , guestPort , hostPort )
294
- if i < len (ss )- 1 {
295
- expr += ","
296
- }
297
- }
298
- expr += `]`
299
- return expr , nil
327
+ return BuildPortForwardExpression (ss )
300
328
},
301
329
false ,
302
330
false ,
0 commit comments