@@ -105,13 +105,9 @@ func RegisterCreate(cmd *cobra.Command, commentPrefix string) {
105
105
106
106
flags .Bool ("plain" , false , commentPrefix + "Plain mode. Disables mounts, port forwarding, containerd, etc." )
107
107
108
- flags .StringSlice ("port-forward" , nil , commentPrefix + "Port forwards (host:guest), e.g., '8080:80,2222:22' " )
108
+ flags .StringArray ("port-forward" , nil , commentPrefix + "Port forwards (host:guest), e.g., '8080:80' or '9090:9090,static=true' for static port-forwards " )
109
109
_ = cmd .RegisterFlagCompletionFunc ("port-forward" , func (* cobra.Command , []string , string ) ([]string , cobra.ShellCompDirective ) {
110
- return []string {"8080:80" , "3000:3000" }, cobra .ShellCompDirectiveNoFileComp
111
- })
112
- flags .StringSlice ("static-port-forward" , nil , commentPrefix + "Static port forwards (host:guest), works even in plain mode, e.g., '8080:80,2222:22'" )
113
- _ = cmd .RegisterFlagCompletionFunc ("static-port-forward" , func (* cobra.Command , []string , string ) ([]string , cobra.ShellCompDirective ) {
114
- return []string {"8080:80" , "3000:3000" }, cobra .ShellCompDirectiveNoFileComp
110
+ return []string {"8080:80" , "3000:3000" , "8080:80,static=true" }, cobra .ShellCompDirectiveNoFileComp
115
111
})
116
112
}
117
113
@@ -121,6 +117,56 @@ func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) {
121
117
}
122
118
}
123
119
120
+ func ParsePortForward (spec string ) (hostPort , guestPort string , isStatic bool , err error ) {
121
+ parts := strings .Split (spec , "," )
122
+ if len (parts ) > 2 {
123
+ return "" , "" , false , fmt .Errorf ("invalid port forward format %q, expected HOST:GUEST or HOST:GUEST,static=true" , spec )
124
+ }
125
+
126
+ portParts := strings .Split (strings .TrimSpace (parts [0 ]), ":" )
127
+ if len (portParts ) != 2 {
128
+ return "" , "" , false , fmt .Errorf ("invalid port forward format %q, expected HOST:GUEST" , parts [0 ])
129
+ }
130
+
131
+ hostPort = strings .TrimSpace (portParts [0 ])
132
+ guestPort = strings .TrimSpace (portParts [1 ])
133
+
134
+ if len (parts ) == 2 {
135
+ staticPart := strings .TrimSpace (parts [1 ])
136
+ if strings .HasPrefix (staticPart , "static=" ) {
137
+ staticValue := strings .TrimPrefix (staticPart , "static=" )
138
+ isStatic , err = strconv .ParseBool (staticValue )
139
+ if err != nil {
140
+ return "" , "" , false , fmt .Errorf ("invalid value for static parameter: %q" , staticValue )
141
+ }
142
+ } else {
143
+ return "" , "" , false , fmt .Errorf ("invalid parameter %q, expected 'static=' followed by a boolean value" , staticPart )
144
+ }
145
+ }
146
+
147
+ return hostPort , guestPort , isStatic , nil
148
+ }
149
+
150
+ func BuildPortForwardExpression (portForwards []string ) (string , error ) {
151
+ if len (portForwards ) == 0 {
152
+ return "" , nil
153
+ }
154
+
155
+ expr := `.portForwards += [`
156
+ for i , spec := range portForwards {
157
+ hostPort , guestPort , isStatic , err := ParsePortForward (spec )
158
+ if err != nil {
159
+ return "" , err
160
+ }
161
+ expr += fmt .Sprintf (`{"guestPort": %q, "hostPort": %q, "static": %v}` , guestPort , hostPort , isStatic )
162
+ if i < len (portForwards )- 1 {
163
+ expr += ","
164
+ }
165
+ }
166
+ expr += `]`
167
+ return expr , nil
168
+ }
169
+
124
170
// YQExpressions returns YQ expressions.
125
171
func YQExpressions (flags * flag.FlagSet , newInstance bool ) ([]string , error ) {
126
172
type def struct {
@@ -281,31 +327,13 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
281
327
{"disk" , d (".disk= \" %sGiB\" " ), false , false },
282
328
{"plain" , d (".plain = %s" ), true , false },
283
329
{
284
- "static- port-forward" ,
330
+ "port-forward" ,
285
331
func (_ * flag.Flag ) (string , error ) {
286
- ss , err := flags .GetStringSlice ( "static- port-forward" )
332
+ ss , err := flags .GetStringArray ( " port-forward" )
287
333
if err != nil {
288
334
return "" , err
289
335
}
290
- if len (ss ) == 0 {
291
- return "" , nil
292
- }
293
-
294
- expr := `.portForwards += [`
295
- for i , s := range ss {
296
- parts := strings .Split (s , ":" )
297
- if len (parts ) != 2 {
298
- return "" , fmt .Errorf ("invalid static port forward format %q, expected HOST:GUEST" , s )
299
- }
300
- hostPort := strings .TrimSpace (parts [0 ])
301
- guestPort := strings .TrimSpace (parts [1 ])
302
- expr += fmt .Sprintf (`{"hostPort": %s, "guestPort": %s, "static": true}` , hostPort , guestPort )
303
- if i < len (ss )- 1 {
304
- expr += ","
305
- }
306
- }
307
- expr += `]`
308
- return expr , nil
336
+ return BuildPortForwardExpression (ss )
309
337
},
310
338
false ,
311
339
false ,
0 commit comments