Merge pull request #40 from bnema/fix/routes-add-positional-image
cli(routes): allow image as positional argument instead of --image flag
2 files · +22 -10
M internal/adapters/in/cli/routes.go +21 -9
| @@ -250,21 +250,34 @@ func newRoutesAddCmd() *cobra.Command { | |||
| 250 | 250 | var image string | |
| 251 | 251 | ||
| 252 | 252 | cmd := &cobra.Command{ | |
| 253 | - | Use: "add <domain>", | |
| 253 | + | Use: "add <domain> <image>", | |
| 254 | 254 | Short: "Add a new route", | |
| 255 | 255 | Long: `Add a new route mapping a domain to a container image. | |
| 256 | 256 | ||
| 257 | 257 | Examples: | |
| 258 | - | gordon routes add app.mydomain.com --image myapp:latest | |
| 259 | - | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com --image api:v2`, | |
| 260 | - | Args: cobra.ExactArgs(1), | |
| 258 | + | gordon routes add app.mydomain.com myapp:latest | |
| 259 | + | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com api:v2`, | |
| 260 | + | Args: cobra.RangeArgs(1, 2), | |
| 261 | 261 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 262 | 262 | ctx := context.Background() | |
| 263 | 263 | routeDomain := args[0] | |
| 264 | 264 | ||
| 265 | - | if image == "" { | |
| 266 | - | fmt.Println(styles.RenderError("--image flag is required")) | |
| 267 | - | return nil | |
| 265 | + | // Resolve image from positional argument or flag | |
| 266 | + | hasPositionalImage := len(args) > 1 | |
| 267 | + | hasFlagImage := image != "" | |
| 268 | + | ||
| 269 | + | // Check for conflicts: both positional and flag provided | |
| 270 | + | if hasPositionalImage && hasFlagImage { | |
| 271 | + | return fmt.Errorf("error: cannot use both positional image argument and --image flag") | |
| 272 | + | } | |
| 273 | + | ||
| 274 | + | // Check that image is provided via either method | |
| 275 | + | if !hasPositionalImage && !hasFlagImage { | |
| 276 | + | return fmt.Errorf("error: image is required (use positional argument or --image flag)") | |
| 277 | + | } | |
| 278 | + | ||
| 279 | + | if hasPositionalImage { | |
| 280 | + | image = args[1] | |
| 268 | 281 | } | |
| 269 | 282 | ||
| 270 | 283 | route := domain.Route{ | |
| @@ -292,8 +305,7 @@ Examples: | |||
| 292 | 305 | }, | |
| 293 | 306 | } | |
| 294 | 307 | ||
| 295 | - | cmd.Flags().StringVarP(&image, "image", "i", "", "Container image (required)") | |
| 296 | - | _ = cmd.MarkFlagRequired("image") | |
| 308 | + | cmd.Flags().StringVarP(&image, "image", "i", "", "Container image") | |
| 297 | 309 | ||
| 298 | 310 | return cmd | |
| 299 | 311 | } | |
| @@ -250,21 +250,34 @@ func newRoutesAddCmd() *cobra.Command { | |||||
| 250 | var image string | 250 | var image string | ||
| 251 | 251 | ||||
| 252 | cmd := &cobra.Command{ | 252 | cmd := &cobra.Command{ | ||
| 253 | - | Use: "add <domain>", | 253 | + | Use: "add <domain> <image>", |
| 254 | Short: "Add a new route", | 254 | Short: "Add a new route", | ||
| 255 | Long: `Add a new route mapping a domain to a container image. | 255 | Long: `Add a new route mapping a domain to a container image. | ||
| 256 | 256 | ||||
| 257 | Examples: | 257 | Examples: | ||
| 258 | - | gordon routes add app.mydomain.com --image myapp:latest | 258 | + | gordon routes add app.mydomain.com myapp:latest |
| 259 | - | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com --image api:v2`, | 259 | + | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com api:v2`, |
| 260 | - | Args: cobra.ExactArgs(1), | 260 | + | Args: cobra.RangeArgs(1, 2), |
| 261 | RunE: func(cmd *cobra.Command, args []string) error { | 261 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 262 | ctx := context.Background() | 262 | ctx := context.Background() | ||
| 263 | routeDomain := args[0] | 263 | routeDomain := args[0] | ||
| 264 | 264 | ||||
| 265 | - | if image == "" { | 265 | + | // Resolve image from positional argument or flag |
| 266 | - | fmt.Println(styles.RenderError("--image flag is required")) | 266 | + | hasPositionalImage := len(args) > 1 |
| 267 | - | return nil | 267 | + | hasFlagImage := image != "" |
| 268 | + | ||||
| 269 | + | // Check for conflicts: both positional and flag provided | |||
| 270 | + | if hasPositionalImage && hasFlagImage { | |||
| 271 | + | return fmt.Errorf("error: cannot use both positional image argument and --image flag") | |||
| 272 | + | } | |||
| 273 | + | ||||
| 274 | + | // Check that image is provided via either method | |||
| 275 | + | if !hasPositionalImage && !hasFlagImage { | |||
| 276 | + | return fmt.Errorf("error: image is required (use positional argument or --image flag)") | |||
| 277 | + | } | |||
| 278 | + | ||||
| 279 | + | if hasPositionalImage { | |||
| 280 | + | image = args[1] | |||
| 268 | } | 281 | } | ||
| 269 | 282 | ||||
| 270 | route := domain.Route{ | 283 | route := domain.Route{ | ||
| @@ -292,8 +305,7 @@ Examples: | |||||
| 292 | }, | 305 | }, | ||
| 293 | } | 306 | } | ||
| 294 | 307 | ||||
| 295 | - | cmd.Flags().StringVarP(&image, "image", "i", "", "Container image (required)") | 308 | + | cmd.Flags().StringVarP(&image, "image", "i", "", "Container image") |
| 296 | - | _ = cmd.MarkFlagRequired("image") | |||
| 297 | 309 | ||||
| 298 | return cmd | 310 | return cmd | ||
| 299 | } | 311 | } | ||
M internal/adapters/in/http/admin/handler.go +1 -1
| @@ -279,7 +279,7 @@ func (h *Handler) handleRoutesPost(w http.ResponseWriter, r *http.Request) { | |||
| 279 | 279 | } | |
| 280 | 280 | ||
| 281 | 281 | // Validate image exists in registry before adding route | |
| 282 | - | if h.registrySvc != nil { | |
| 282 | + | if h.registrySvc != nil && route.Image != "" { | |
| 283 | 283 | imageName, imageRef := validation.ParseImageReference(route.Image) | |
| 284 | 284 | if _, err := h.registrySvc.GetManifest(ctx, imageName, imageRef); err != nil { | |
| 285 | 285 | if errors.Is(err, domain.ErrManifestNotFound) { | |
| @@ -279,7 +279,7 @@ func (h *Handler) handleRoutesPost(w http.ResponseWriter, r *http.Request) { | |||||
| 279 | } | 279 | } | ||
| 280 | 280 | ||||
| 281 | // Validate image exists in registry before adding route | 281 | // Validate image exists in registry before adding route | ||
| 282 | - | if h.registrySvc != nil { | 282 | + | if h.registrySvc != nil && route.Image != "" { |
| 283 | imageName, imageRef := validation.ParseImageReference(route.Image) | 283 | imageName, imageRef := validation.ParseImageReference(route.Image) | ||
| 284 | if _, err := h.registrySvc.GetManifest(ctx, imageName, imageRef); err != nil { | 284 | if _, err := h.registrySvc.GetManifest(ctx, imageName, imageRef); err != nil { | ||
| 285 | if errors.Is(err, domain.ErrManifestNotFound) { | 285 | if errors.Is(err, domain.ErrManifestNotFound) { | ||