@@ -105,13 +105,9 @@ func RegisterCreate(cmd *cobra.Command, commentPrefix string) {
105105
106106 flags .Bool ("plain" , false , commentPrefix + "Plain mode. Disables mounts, port forwarding, containerd, etc." )
107107
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 " )
109109 _ = 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
115111 })
116112}
117113
@@ -121,6 +117,56 @@ func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) {
121117 }
122118}
123119
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+
124170// YQExpressions returns YQ expressions.
125171func YQExpressions (flags * flag.FlagSet , newInstance bool ) ([]string , error ) {
126172 type def struct {
@@ -281,31 +327,13 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
281327 {"disk" , d (".disk= \" %sGiB\" " ), false , false },
282328 {"plain" , d (".plain = %s" ), true , false },
283329 {
284- "static- port-forward" ,
330+ "port-forward" ,
285331 func (_ * flag.Flag ) (string , error ) {
286- ss , err := flags .GetStringSlice ( "static- port-forward" )
332+ ss , err := flags .GetStringArray ( " port-forward" )
287333 if err != nil {
288334 return "" , err
289335 }
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 )
309337 },
310338 false ,
311339 false ,
0 commit comments