fix(cli): improve backward compatibility and add validation guards
- commit
- a8446a91a8dfecc517920df0c2b483482a472ce5
- author
- bnema · · 2026-01-28 21:05 UTC
- parent
- 9494436
- signature
- unsigned
2 files · +17 -3
M internal/adapters/in/cli/routes.go +16 -2
| @@ -257,12 +257,26 @@ func newRoutesAddCmd() *cobra.Command { | |||
| 257 | 257 | Examples: | |
| 258 | 258 | gordon routes add app.mydomain.com myapp:latest | |
| 259 | 259 | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com api:v2`, | |
| 260 | - | Args: cobra.ExactArgs(2), | |
| 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 == "" { | |
| 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 { | |
| 266 | 280 | image = args[1] | |
| 267 | 281 | } | |
| 268 | 282 | ||
| @@ -257,12 +257,26 @@ func newRoutesAddCmd() *cobra.Command { | |||||
| 257 | Examples: | 257 | Examples: | ||
| 258 | gordon routes add app.mydomain.com myapp:latest | 258 | gordon routes add app.mydomain.com myapp:latest | ||
| 259 | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com api:v2`, | 259 | gordon --remote https://gordon.mydomain.com routes add api.mydomain.com api:v2`, | ||
| 260 | - | Args: cobra.ExactArgs(2), | 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 | + | 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 { | |||
| 266 | image = args[1] | 280 | image = args[1] | ||
| 267 | } | 281 | } | ||
| 268 | 282 | ||||
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) { | ||