cli: apply follow-up review fixes
- commit
- 3eb7cb6f3b8fce2a342ca9bc0d7b4ae2679b2d79
- author
- brice · · 2026-05-15 09:40 UTC
- parent
- 1e6e9b3
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
5 files · +107 -61
M internal/adapters/in/cli/attachments.go +10 -10
| @@ -52,7 +52,7 @@ Examples: | |||
| 52 | 52 | gordon attachments list backend # List attachments for network group`, | |
| 53 | 53 | Args: cobra.MaximumNArgs(1), | |
| 54 | 54 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 55 | - | ctx := context.Background() | |
| 55 | + | ctx := cmd.Context() | |
| 56 | 56 | target := "" | |
| 57 | 57 | if len(args) > 0 { | |
| 58 | 58 | target = args[0] | |
| @@ -110,14 +110,14 @@ func runAttachmentsList(ctx context.Context, cp ControlPlane, target string, jso | |||
| 110 | 110 | } | |
| 111 | 111 | ||
| 112 | 112 | func renderAttachmentTargetList(out io.Writer, title string, images []string) error { | |
| 113 | - | if _, err := fmt.Fprintln(out, styles.Theme.Title.Render(title)); err != nil { | |
| 113 | + | if err := cliWriteLine(out, cliRenderTitle(title)); err != nil { | |
| 114 | 114 | return err | |
| 115 | 115 | } | |
| 116 | - | if _, err := fmt.Fprintln(out); err != nil { | |
| 116 | + | if err := cliWriteLine(out, ""); err != nil { | |
| 117 | 117 | return err | |
| 118 | 118 | } | |
| 119 | 119 | for _, img := range images { | |
| 120 | - | if _, err := fmt.Fprintf(out, " %s\n", img); err != nil { | |
| 120 | + | if err := cliWritef(out, " %s\n", img); err != nil { | |
| 121 | 121 | return err | |
| 122 | 122 | } | |
| 123 | 123 | } | |
| @@ -131,19 +131,19 @@ func renderAllAttachmentsList(out io.Writer, title string, attachments map[strin | |||
| 131 | 131 | } | |
| 132 | 132 | sort.Strings(targets) | |
| 133 | 133 | ||
| 134 | - | if _, err := fmt.Fprintln(out, styles.Theme.Title.Render(title)); err != nil { | |
| 134 | + | if err := cliWriteLine(out, cliRenderTitle(title)); err != nil { | |
| 135 | 135 | return err | |
| 136 | 136 | } | |
| 137 | - | if _, err := fmt.Fprintln(out); err != nil { | |
| 137 | + | if err := cliWriteLine(out, ""); err != nil { | |
| 138 | 138 | return err | |
| 139 | 139 | } | |
| 140 | 140 | for _, t := range targets { | |
| 141 | 141 | images := attachments[t] | |
| 142 | - | if _, err := fmt.Fprintf(out, "%s\n", styles.Theme.Bold.Render(t)); err != nil { | |
| 142 | + | if err := cliWritef(out, "%s\n", styles.Theme.Bold.Render(t)); err != nil { | |
| 143 | 143 | return err | |
| 144 | 144 | } | |
| 145 | 145 | for _, img := range images { | |
| 146 | - | if _, err := fmt.Fprintf(out, " %s\n", img); err != nil { | |
| 146 | + | if err := cliWritef(out, " %s\n", img); err != nil { | |
| 147 | 147 | return err | |
| 148 | 148 | } | |
| 149 | 149 | } | |
| @@ -295,7 +295,7 @@ Examples: | |||
| 295 | 295 | gordon --remote https://gordon.mydomain.com attachments add api.mydomain.com memcached:latest`, | |
| 296 | 296 | Args: cobra.ExactArgs(2), | |
| 297 | 297 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 298 | - | ctx := context.Background() | |
| 298 | + | ctx := cmd.Context() | |
| 299 | 299 | target := args[0] | |
| 300 | 300 | image := args[1] | |
| 301 | 301 | ||
| @@ -347,7 +347,7 @@ Examples: | |||
| 347 | 347 | gordon attachments remove backend redis:7-alpine --force`, | |
| 348 | 348 | Args: cobra.ExactArgs(2), | |
| 349 | 349 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 350 | - | ctx := context.Background() | |
| 350 | + | ctx := cmd.Context() | |
| 351 | 351 | target := args[0] | |
| 352 | 352 | image := args[1] | |
| 353 | 353 | ||
| @@ -52,7 +52,7 @@ Examples: | |||||
| 52 | gordon attachments list backend # List attachments for network group`, | 52 | gordon attachments list backend # List attachments for network group`, | ||
| 53 | Args: cobra.MaximumNArgs(1), | 53 | Args: cobra.MaximumNArgs(1), | ||
| 54 | RunE: func(cmd *cobra.Command, args []string) error { | 54 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 55 | - | ctx := context.Background() | 55 | + | ctx := cmd.Context() |
| 56 | target := "" | 56 | target := "" | ||
| 57 | if len(args) > 0 { | 57 | if len(args) > 0 { | ||
| 58 | target = args[0] | 58 | target = args[0] | ||
| @@ -110,14 +110,14 @@ func runAttachmentsList(ctx context.Context, cp ControlPlane, target string, jso | |||||
| 110 | } | 110 | } | ||
| 111 | 111 | ||||
| 112 | func renderAttachmentTargetList(out io.Writer, title string, images []string) error { | 112 | func renderAttachmentTargetList(out io.Writer, title string, images []string) error { | ||
| 113 | - | if _, err := fmt.Fprintln(out, styles.Theme.Title.Render(title)); err != nil { | 113 | + | if err := cliWriteLine(out, cliRenderTitle(title)); err != nil { |
| 114 | return err | 114 | return err | ||
| 115 | } | 115 | } | ||
| 116 | - | if _, err := fmt.Fprintln(out); err != nil { | 116 | + | if err := cliWriteLine(out, ""); err != nil { |
| 117 | return err | 117 | return err | ||
| 118 | } | 118 | } | ||
| 119 | for _, img := range images { | 119 | for _, img := range images { | ||
| 120 | - | if _, err := fmt.Fprintf(out, " %s\n", img); err != nil { | 120 | + | if err := cliWritef(out, " %s\n", img); err != nil { |
| 121 | return err | 121 | return err | ||
| 122 | } | 122 | } | ||
| 123 | } | 123 | } | ||
| @@ -131,19 +131,19 @@ func renderAllAttachmentsList(out io.Writer, title string, attachments map[strin | |||||
| 131 | } | 131 | } | ||
| 132 | sort.Strings(targets) | 132 | sort.Strings(targets) | ||
| 133 | 133 | ||||
| 134 | - | if _, err := fmt.Fprintln(out, styles.Theme.Title.Render(title)); err != nil { | 134 | + | if err := cliWriteLine(out, cliRenderTitle(title)); err != nil { |
| 135 | return err | 135 | return err | ||
| 136 | } | 136 | } | ||
| 137 | - | if _, err := fmt.Fprintln(out); err != nil { | 137 | + | if err := cliWriteLine(out, ""); err != nil { |
| 138 | return err | 138 | return err | ||
| 139 | } | 139 | } | ||
| 140 | for _, t := range targets { | 140 | for _, t := range targets { | ||
| 141 | images := attachments[t] | 141 | images := attachments[t] | ||
| 142 | - | if _, err := fmt.Fprintf(out, "%s\n", styles.Theme.Bold.Render(t)); err != nil { | 142 | + | if err := cliWritef(out, "%s\n", styles.Theme.Bold.Render(t)); err != nil { |
| 143 | return err | 143 | return err | ||
| 144 | } | 144 | } | ||
| 145 | for _, img := range images { | 145 | for _, img := range images { | ||
| 146 | - | if _, err := fmt.Fprintf(out, " %s\n", img); err != nil { | 146 | + | if err := cliWritef(out, " %s\n", img); err != nil { |
| 147 | return err | 147 | return err | ||
| 148 | } | 148 | } | ||
| 149 | } | 149 | } | ||
| @@ -295,7 +295,7 @@ Examples: | |||||
| 295 | gordon --remote https://gordon.mydomain.com attachments add api.mydomain.com memcached:latest`, | 295 | gordon --remote https://gordon.mydomain.com attachments add api.mydomain.com memcached:latest`, | ||
| 296 | Args: cobra.ExactArgs(2), | 296 | Args: cobra.ExactArgs(2), | ||
| 297 | RunE: func(cmd *cobra.Command, args []string) error { | 297 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 298 | - | ctx := context.Background() | 298 | + | ctx := cmd.Context() |
| 299 | target := args[0] | 299 | target := args[0] | ||
| 300 | image := args[1] | 300 | image := args[1] | ||
| 301 | 301 | ||||
| @@ -347,7 +347,7 @@ Examples: | |||||
| 347 | gordon attachments remove backend redis:7-alpine --force`, | 347 | gordon attachments remove backend redis:7-alpine --force`, | ||
| 348 | Args: cobra.ExactArgs(2), | 348 | Args: cobra.ExactArgs(2), | ||
| 349 | RunE: func(cmd *cobra.Command, args []string) error { | 349 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 350 | - | ctx := context.Background() | 350 | + | ctx := cmd.Context() |
| 351 | target := args[0] | 351 | target := args[0] | ||
| 352 | image := args[1] | 352 | image := args[1] | ||
| 353 | 353 | ||||
M internal/adapters/in/cli/push.go +14 -6
| @@ -227,7 +227,7 @@ func resolveVersion(ctx context.Context, tag string) (string, error) { | |||
| 227 | 227 | } | |
| 228 | 228 | ||
| 229 | 229 | func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | |
| 230 | - | dockerfile, inferredRemote, handle, err := resolvePushTarget(ctx, req) | |
| 230 | + | dockerfile, inferredRemote, handle, err := resolvePushTarget(ctx, out, req) | |
| 231 | 231 | if err != nil { | |
| 232 | 232 | return err | |
| 233 | 233 | } | |
| @@ -254,11 +254,17 @@ func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | |||
| 254 | 254 | return err | |
| 255 | 255 | } | |
| 256 | 256 | ||
| 257 | - | fmt.Printf("Image: %s\n", styles.Theme.Bold.Render(img.VersionRef)) | |
| 257 | + | if err := cliWritef(out, "Image: %s\n", styles.Theme.Bold.Render(img.VersionRef)); err != nil { | |
| 258 | + | return err | |
| 259 | + | } | |
| 258 | 260 | if version != "latest" { | |
| 259 | - | fmt.Printf("Also: %s\n", styles.Theme.Bold.Render(img.LatestRef)) | |
| 261 | + | if err := cliWritef(out, "Also: %s\n", styles.Theme.Bold.Render(img.LatestRef)); err != nil { | |
| 262 | + | return err | |
| 263 | + | } | |
| 264 | + | } | |
| 265 | + | if err := cliWritef(out, "Domain: %s\n", styles.Theme.Bold.Render(pushDomain)); err != nil { | |
| 266 | + | return err | |
| 260 | 267 | } | |
| 261 | - | fmt.Printf("Domain: %s\n", styles.Theme.Bold.Render(pushDomain)) | |
| 262 | 268 | ||
| 263 | 269 | skipExplicitDeploy := shouldSkipDeploy(ctx, handle.plane, imageName, req.NoDeploy) | |
| 264 | 270 | build := buildConfig{Enabled: req.Build.Enabled, Platform: req.Build.Platform, Dockerfile: dockerfile, BuildArgs: req.Build.BuildArgs} | |
| @@ -275,7 +281,7 @@ func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | |||
| 275 | 281 | return nil | |
| 276 | 282 | } | |
| 277 | 283 | ||
| 278 | - | func resolvePushTarget(ctx context.Context, req pushRequest) (dockerfile string, inferredRemote *remote.ResolvedRemote, handle *controlPlaneHandle, err error) { | |
| 284 | + | func resolvePushTarget(ctx context.Context, out io.Writer, req pushRequest) (dockerfile string, inferredRemote *remote.ResolvedRemote, handle *controlPlaneHandle, err error) { | |
| 279 | 285 | dockerfile, err = resolveDockerfile(req.Build.Dockerfile, req.Build.Enabled) | |
| 280 | 286 | if err != nil { | |
| 281 | 287 | return "", nil, nil, err | |
| @@ -286,7 +292,9 @@ func resolvePushTarget(ctx context.Context, req pushRequest) (dockerfile string, | |||
| 286 | 292 | } | |
| 287 | 293 | if inferredRemote != nil { | |
| 288 | 294 | handle = newRemoteControlPlaneHandle(inferredRemote) | |
| 289 | - | fmt.Printf("Remote: %s %s\n", styles.Theme.Bold.Render(inferredRemote.DisplayName()), styles.Theme.Muted.Render("(auto-detected)")) | |
| 295 | + | if err := cliWritef(out, "Remote: %s %s\n", styles.Theme.Bold.Render(inferredRemote.DisplayName()), styles.Theme.Muted.Render("(auto-detected)")); err != nil { | |
| 296 | + | return "", nil, nil, err | |
| 297 | + | } | |
| 290 | 298 | return dockerfile, inferredRemote, handle, nil | |
| 291 | 299 | } | |
| 292 | 300 | handle, err = resolveControlPlane(configPath) | |
| @@ -227,7 +227,7 @@ func resolveVersion(ctx context.Context, tag string) (string, error) { | |||||
| 227 | } | 227 | } | ||
| 228 | 228 | ||||
| 229 | func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | 229 | func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | ||
| 230 | - | dockerfile, inferredRemote, handle, err := resolvePushTarget(ctx, req) | 230 | + | dockerfile, inferredRemote, handle, err := resolvePushTarget(ctx, out, req) |
| 231 | if err != nil { | 231 | if err != nil { | ||
| 232 | return err | 232 | return err | ||
| 233 | } | 233 | } | ||
| @@ -254,11 +254,17 @@ func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | |||||
| 254 | return err | 254 | return err | ||
| 255 | } | 255 | } | ||
| 256 | 256 | ||||
| 257 | - | fmt.Printf("Image: %s\n", styles.Theme.Bold.Render(img.VersionRef)) | 257 | + | if err := cliWritef(out, "Image: %s\n", styles.Theme.Bold.Render(img.VersionRef)); err != nil { |
| 258 | + | return err | |||
| 259 | + | } | |||
| 258 | if version != "latest" { | 260 | if version != "latest" { | ||
| 259 | - | fmt.Printf("Also: %s\n", styles.Theme.Bold.Render(img.LatestRef)) | 261 | + | if err := cliWritef(out, "Also: %s\n", styles.Theme.Bold.Render(img.LatestRef)); err != nil { |
| 262 | + | return err | |||
| 263 | + | } | |||
| 264 | + | } | |||
| 265 | + | if err := cliWritef(out, "Domain: %s\n", styles.Theme.Bold.Render(pushDomain)); err != nil { | |||
| 266 | + | return err | |||
| 260 | } | 267 | } | ||
| 261 | - | fmt.Printf("Domain: %s\n", styles.Theme.Bold.Render(pushDomain)) | |||
| 262 | 268 | ||||
| 263 | skipExplicitDeploy := shouldSkipDeploy(ctx, handle.plane, imageName, req.NoDeploy) | 269 | skipExplicitDeploy := shouldSkipDeploy(ctx, handle.plane, imageName, req.NoDeploy) | ||
| 264 | build := buildConfig{Enabled: req.Build.Enabled, Platform: req.Build.Platform, Dockerfile: dockerfile, BuildArgs: req.Build.BuildArgs} | 270 | build := buildConfig{Enabled: req.Build.Enabled, Platform: req.Build.Platform, Dockerfile: dockerfile, BuildArgs: req.Build.BuildArgs} | ||
| @@ -275,7 +281,7 @@ func runPush(ctx context.Context, out io.Writer, req pushRequest) error { | |||||
| 275 | return nil | 281 | return nil | ||
| 276 | } | 282 | } | ||
| 277 | 283 | ||||
| 278 | - | func resolvePushTarget(ctx context.Context, req pushRequest) (dockerfile string, inferredRemote *remote.ResolvedRemote, handle *controlPlaneHandle, err error) { | 284 | + | func resolvePushTarget(ctx context.Context, out io.Writer, req pushRequest) (dockerfile string, inferredRemote *remote.ResolvedRemote, handle *controlPlaneHandle, err error) { |
| 279 | dockerfile, err = resolveDockerfile(req.Build.Dockerfile, req.Build.Enabled) | 285 | dockerfile, err = resolveDockerfile(req.Build.Dockerfile, req.Build.Enabled) | ||
| 280 | if err != nil { | 286 | if err != nil { | ||
| 281 | return "", nil, nil, err | 287 | return "", nil, nil, err | ||
| @@ -286,7 +292,9 @@ func resolvePushTarget(ctx context.Context, req pushRequest) (dockerfile string, | |||||
| 286 | } | 292 | } | ||
| 287 | if inferredRemote != nil { | 293 | if inferredRemote != nil { | ||
| 288 | handle = newRemoteControlPlaneHandle(inferredRemote) | 294 | handle = newRemoteControlPlaneHandle(inferredRemote) | ||
| 289 | - | fmt.Printf("Remote: %s %s\n", styles.Theme.Bold.Render(inferredRemote.DisplayName()), styles.Theme.Muted.Render("(auto-detected)")) | 295 | + | if err := cliWritef(out, "Remote: %s %s\n", styles.Theme.Bold.Render(inferredRemote.DisplayName()), styles.Theme.Muted.Render("(auto-detected)")); err != nil { |
| 296 | + | return "", nil, nil, err | |||
| 297 | + | } | |||
| 290 | return dockerfile, inferredRemote, handle, nil | 298 | return dockerfile, inferredRemote, handle, nil | ||
| 291 | } | 299 | } | ||
| 292 | handle, err = resolveControlPlane(configPath) | 300 | handle, err = resolveControlPlane(configPath) | ||
M internal/adapters/in/cli/push_remote_inference_test.go +57 -20
| @@ -11,9 +11,10 @@ import ( | |||
| 11 | 11 | "strings" | |
| 12 | 12 | "testing" | |
| 13 | 13 | ||
| 14 | - | "github.com/bnema/gordon/internal/domain" | |
| 15 | 14 | "github.com/stretchr/testify/assert" | |
| 16 | 15 | "github.com/stretchr/testify/require" | |
| 16 | + | ||
| 17 | + | "github.com/bnema/gordon/internal/domain" | |
| 17 | 18 | ) | |
| 18 | 19 | ||
| 19 | 20 | func TestInferPushRemote_UsesUniqueMatchingSavedRemote(t *testing.T) { | |
| @@ -305,21 +306,27 @@ func newFindRoutesByImageTestServer(t *testing.T, handler func(image string) ([] | |||
| 305 | 306 | } | |
| 306 | 307 | ||
| 307 | 308 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/by-image/")) | |
| 308 | - | require.NoError(t, err) | |
| 309 | + | if !assert.NoError(t, err) { | |
| 310 | + | return | |
| 311 | + | } | |
| 309 | 312 | ||
| 310 | 313 | routes, status := handler(image) | |
| 311 | 314 | if status >= http.StatusBadRequest { | |
| 312 | 315 | w.Header().Set("Content-Type", "application/json") | |
| 313 | 316 | w.WriteHeader(status) | |
| 314 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |
| 317 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |
| 318 | + | return | |
| 319 | + | } | |
| 315 | 320 | return | |
| 316 | 321 | } | |
| 317 | 322 | ||
| 318 | 323 | w.Header().Set("Content-Type", "application/json") | |
| 319 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{ | |
| 324 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{ | |
| 320 | 325 | "image": image, | |
| 321 | 326 | "routes": routes, | |
| 322 | - | })) | |
| 327 | + | })) { | |
| 328 | + | return | |
| 329 | + | } | |
| 323 | 330 | })) | |
| 324 | 331 | t.Cleanup(server.Close) | |
| 325 | 332 | return server | |
| @@ -375,20 +382,28 @@ func handleGetRouteProbe(t *testing.T, w http.ResponseWriter, r *http.Request, h | |||
| 375 | 382 | return | |
| 376 | 383 | } | |
| 377 | 384 | domainName, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/")) | |
| 378 | - | require.NoError(t, err) | |
| 385 | + | if !assert.NoError(t, err) { | |
| 386 | + | return | |
| 387 | + | } | |
| 379 | 388 | route, status := handler(domainName) | |
| 380 | 389 | if status >= http.StatusBadRequest { | |
| 381 | 390 | w.Header().Set("Content-Type", "application/json") | |
| 382 | 391 | w.WriteHeader(status) | |
| 383 | 392 | if status == http.StatusNotFound { | |
| 384 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": domain.ErrRouteNotFound.Error()})) | |
| 393 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": domain.ErrRouteNotFound.Error()})) { | |
| 394 | + | return | |
| 395 | + | } | |
| 396 | + | return | |
| 397 | + | } | |
| 398 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |
| 385 | 399 | return | |
| 386 | 400 | } | |
| 387 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |
| 388 | 401 | return | |
| 389 | 402 | } | |
| 390 | 403 | w.Header().Set("Content-Type", "application/json") | |
| 391 | - | require.NoError(t, json.NewEncoder(w).Encode(route)) | |
| 404 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(route)) { | |
| 405 | + | return | |
| 406 | + | } | |
| 392 | 407 | } | |
| 393 | 408 | ||
| 394 | 409 | func handleAttachmentTargetsByImageProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(image string) ([]string, int)) { | |
| @@ -398,16 +413,22 @@ func handleAttachmentTargetsByImageProbe(t *testing.T, w http.ResponseWriter, r | |||
| 398 | 413 | return | |
| 399 | 414 | } | |
| 400 | 415 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/by-image/")) | |
| 401 | - | require.NoError(t, err) | |
| 416 | + | if !assert.NoError(t, err) { | |
| 417 | + | return | |
| 418 | + | } | |
| 402 | 419 | targets, status := handler(image) | |
| 403 | 420 | if status >= http.StatusBadRequest { | |
| 404 | 421 | w.Header().Set("Content-Type", "application/json") | |
| 405 | 422 | w.WriteHeader(status) | |
| 406 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |
| 423 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |
| 424 | + | return | |
| 425 | + | } | |
| 407 | 426 | return | |
| 408 | 427 | } | |
| 409 | 428 | w.Header().Set("Content-Type", "application/json") | |
| 410 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"image": image, "targets": targets})) | |
| 429 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"image": image, "targets": targets})) { | |
| 430 | + | return | |
| 431 | + | } | |
| 411 | 432 | } | |
| 412 | 433 | ||
| 413 | 434 | func handleGetAttachmentsConfigProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(target string) ([]string, int)) { | |
| @@ -417,20 +438,28 @@ func handleGetAttachmentsConfigProbe(t *testing.T, w http.ResponseWriter, r *htt | |||
| 417 | 438 | return | |
| 418 | 439 | } | |
| 419 | 440 | target, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/")) | |
| 420 | - | require.NoError(t, err) | |
| 441 | + | if !assert.NoError(t, err) { | |
| 442 | + | return | |
| 443 | + | } | |
| 421 | 444 | images, status := handler(target) | |
| 422 | 445 | if status >= http.StatusBadRequest { | |
| 423 | 446 | w.Header().Set("Content-Type", "application/json") | |
| 424 | 447 | w.WriteHeader(status) | |
| 425 | 448 | if status == http.StatusNotFound { | |
| 426 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "no attachments found for target"})) | |
| 449 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "no attachments found for target"})) { | |
| 450 | + | return | |
| 451 | + | } | |
| 452 | + | return | |
| 453 | + | } | |
| 454 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |
| 427 | 455 | return | |
| 428 | 456 | } | |
| 429 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |
| 430 | 457 | return | |
| 431 | 458 | } | |
| 432 | 459 | w.Header().Set("Content-Type", "application/json") | |
| 433 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"target": target, "images": images})) | |
| 460 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"target": target, "images": images})) { | |
| 461 | + | return | |
| 462 | + | } | |
| 434 | 463 | } | |
| 435 | 464 | ||
| 436 | 465 | func handleListTagsProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(repository string) ([]string, int)) { | |
| @@ -440,18 +469,26 @@ func handleListTagsProbe(t *testing.T, w http.ResponseWriter, r *http.Request, h | |||
| 440 | 469 | return | |
| 441 | 470 | } | |
| 442 | 471 | repository, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/tags/")) | |
| 443 | - | require.NoError(t, err) | |
| 472 | + | if !assert.NoError(t, err) { | |
| 473 | + | return | |
| 474 | + | } | |
| 444 | 475 | tags, status := handler(repository) | |
| 445 | 476 | if status >= http.StatusBadRequest { | |
| 446 | 477 | w.Header().Set("Content-Type", "application/json") | |
| 447 | 478 | w.WriteHeader(status) | |
| 448 | 479 | if status == http.StatusNotFound { | |
| 449 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "repository not found"})) | |
| 480 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "repository not found"})) { | |
| 481 | + | return | |
| 482 | + | } | |
| 483 | + | return | |
| 484 | + | } | |
| 485 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |
| 450 | 486 | return | |
| 451 | 487 | } | |
| 452 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |
| 453 | 488 | return | |
| 454 | 489 | } | |
| 455 | 490 | w.Header().Set("Content-Type", "application/json") | |
| 456 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"repository": repository, "tags": tags})) | |
| 491 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"repository": repository, "tags": tags})) { | |
| 492 | + | return | |
| 493 | + | } | |
| 457 | 494 | } | |
| @@ -11,9 +11,10 @@ import ( | |||||
| 11 | "strings" | 11 | "strings" | ||
| 12 | "testing" | 12 | "testing" | ||
| 13 | 13 | ||||
| 14 | - | "github.com/bnema/gordon/internal/domain" | |||
| 15 | "github.com/stretchr/testify/assert" | 14 | "github.com/stretchr/testify/assert" | ||
| 16 | "github.com/stretchr/testify/require" | 15 | "github.com/stretchr/testify/require" | ||
| 16 | + | ||||
| 17 | + | "github.com/bnema/gordon/internal/domain" | |||
| 17 | ) | 18 | ) | ||
| 18 | 19 | ||||
| 19 | func TestInferPushRemote_UsesUniqueMatchingSavedRemote(t *testing.T) { | 20 | func TestInferPushRemote_UsesUniqueMatchingSavedRemote(t *testing.T) { | ||
| @@ -305,21 +306,27 @@ func newFindRoutesByImageTestServer(t *testing.T, handler func(image string) ([] | |||||
| 305 | } | 306 | } | ||
| 306 | 307 | ||||
| 307 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/by-image/")) | 308 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/by-image/")) | ||
| 308 | - | require.NoError(t, err) | 309 | + | if !assert.NoError(t, err) { |
| 310 | + | return | |||
| 311 | + | } | |||
| 309 | 312 | ||||
| 310 | routes, status := handler(image) | 313 | routes, status := handler(image) | ||
| 311 | if status >= http.StatusBadRequest { | 314 | if status >= http.StatusBadRequest { | ||
| 312 | w.Header().Set("Content-Type", "application/json") | 315 | w.Header().Set("Content-Type", "application/json") | ||
| 313 | w.WriteHeader(status) | 316 | w.WriteHeader(status) | ||
| 314 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | 317 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { |
| 318 | + | return | |||
| 319 | + | } | |||
| 315 | return | 320 | return | ||
| 316 | } | 321 | } | ||
| 317 | 322 | ||||
| 318 | w.Header().Set("Content-Type", "application/json") | 323 | w.Header().Set("Content-Type", "application/json") | ||
| 319 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{ | 324 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{ |
| 320 | "image": image, | 325 | "image": image, | ||
| 321 | "routes": routes, | 326 | "routes": routes, | ||
| 322 | - | })) | 327 | + | })) { |
| 328 | + | return | |||
| 329 | + | } | |||
| 323 | })) | 330 | })) | ||
| 324 | t.Cleanup(server.Close) | 331 | t.Cleanup(server.Close) | ||
| 325 | return server | 332 | return server | ||
| @@ -375,20 +382,28 @@ func handleGetRouteProbe(t *testing.T, w http.ResponseWriter, r *http.Request, h | |||||
| 375 | return | 382 | return | ||
| 376 | } | 383 | } | ||
| 377 | domainName, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/")) | 384 | domainName, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/routes/")) | ||
| 378 | - | require.NoError(t, err) | 385 | + | if !assert.NoError(t, err) { |
| 386 | + | return | |||
| 387 | + | } | |||
| 379 | route, status := handler(domainName) | 388 | route, status := handler(domainName) | ||
| 380 | if status >= http.StatusBadRequest { | 389 | if status >= http.StatusBadRequest { | ||
| 381 | w.Header().Set("Content-Type", "application/json") | 390 | w.Header().Set("Content-Type", "application/json") | ||
| 382 | w.WriteHeader(status) | 391 | w.WriteHeader(status) | ||
| 383 | if status == http.StatusNotFound { | 392 | if status == http.StatusNotFound { | ||
| 384 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": domain.ErrRouteNotFound.Error()})) | 393 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": domain.ErrRouteNotFound.Error()})) { |
| 394 | + | return | |||
| 395 | + | } | |||
| 396 | + | return | |||
| 397 | + | } | |||
| 398 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |||
| 385 | return | 399 | return | ||
| 386 | } | 400 | } | ||
| 387 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |||
| 388 | return | 401 | return | ||
| 389 | } | 402 | } | ||
| 390 | w.Header().Set("Content-Type", "application/json") | 403 | w.Header().Set("Content-Type", "application/json") | ||
| 391 | - | require.NoError(t, json.NewEncoder(w).Encode(route)) | 404 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(route)) { |
| 405 | + | return | |||
| 406 | + | } | |||
| 392 | } | 407 | } | ||
| 393 | 408 | ||||
| 394 | func handleAttachmentTargetsByImageProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(image string) ([]string, int)) { | 409 | func handleAttachmentTargetsByImageProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(image string) ([]string, int)) { | ||
| @@ -398,16 +413,22 @@ func handleAttachmentTargetsByImageProbe(t *testing.T, w http.ResponseWriter, r | |||||
| 398 | return | 413 | return | ||
| 399 | } | 414 | } | ||
| 400 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/by-image/")) | 415 | image, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/by-image/")) | ||
| 401 | - | require.NoError(t, err) | 416 | + | if !assert.NoError(t, err) { |
| 417 | + | return | |||
| 418 | + | } | |||
| 402 | targets, status := handler(image) | 419 | targets, status := handler(image) | ||
| 403 | if status >= http.StatusBadRequest { | 420 | if status >= http.StatusBadRequest { | ||
| 404 | w.Header().Set("Content-Type", "application/json") | 421 | w.Header().Set("Content-Type", "application/json") | ||
| 405 | w.WriteHeader(status) | 422 | w.WriteHeader(status) | ||
| 406 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | 423 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { |
| 424 | + | return | |||
| 425 | + | } | |||
| 407 | return | 426 | return | ||
| 408 | } | 427 | } | ||
| 409 | w.Header().Set("Content-Type", "application/json") | 428 | w.Header().Set("Content-Type", "application/json") | ||
| 410 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"image": image, "targets": targets})) | 429 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"image": image, "targets": targets})) { |
| 430 | + | return | |||
| 431 | + | } | |||
| 411 | } | 432 | } | ||
| 412 | 433 | ||||
| 413 | func handleGetAttachmentsConfigProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(target string) ([]string, int)) { | 434 | func handleGetAttachmentsConfigProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(target string) ([]string, int)) { | ||
| @@ -417,20 +438,28 @@ func handleGetAttachmentsConfigProbe(t *testing.T, w http.ResponseWriter, r *htt | |||||
| 417 | return | 438 | return | ||
| 418 | } | 439 | } | ||
| 419 | target, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/")) | 440 | target, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/attachments/")) | ||
| 420 | - | require.NoError(t, err) | 441 | + | if !assert.NoError(t, err) { |
| 442 | + | return | |||
| 443 | + | } | |||
| 421 | images, status := handler(target) | 444 | images, status := handler(target) | ||
| 422 | if status >= http.StatusBadRequest { | 445 | if status >= http.StatusBadRequest { | ||
| 423 | w.Header().Set("Content-Type", "application/json") | 446 | w.Header().Set("Content-Type", "application/json") | ||
| 424 | w.WriteHeader(status) | 447 | w.WriteHeader(status) | ||
| 425 | if status == http.StatusNotFound { | 448 | if status == http.StatusNotFound { | ||
| 426 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "no attachments found for target"})) | 449 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "no attachments found for target"})) { |
| 450 | + | return | |||
| 451 | + | } | |||
| 452 | + | return | |||
| 453 | + | } | |||
| 454 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |||
| 427 | return | 455 | return | ||
| 428 | } | 456 | } | ||
| 429 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |||
| 430 | return | 457 | return | ||
| 431 | } | 458 | } | ||
| 432 | w.Header().Set("Content-Type", "application/json") | 459 | w.Header().Set("Content-Type", "application/json") | ||
| 433 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"target": target, "images": images})) | 460 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"target": target, "images": images})) { |
| 461 | + | return | |||
| 462 | + | } | |||
| 434 | } | 463 | } | ||
| 435 | 464 | ||||
| 436 | func handleListTagsProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(repository string) ([]string, int)) { | 465 | func handleListTagsProbe(t *testing.T, w http.ResponseWriter, r *http.Request, handler func(repository string) ([]string, int)) { | ||
| @@ -440,18 +469,26 @@ func handleListTagsProbe(t *testing.T, w http.ResponseWriter, r *http.Request, h | |||||
| 440 | return | 469 | return | ||
| 441 | } | 470 | } | ||
| 442 | repository, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/tags/")) | 471 | repository, err := url.PathUnescape(strings.TrimPrefix(r.URL.Path, "/admin/tags/")) | ||
| 443 | - | require.NoError(t, err) | 472 | + | if !assert.NoError(t, err) { |
| 473 | + | return | |||
| 474 | + | } | |||
| 444 | tags, status := handler(repository) | 475 | tags, status := handler(repository) | ||
| 445 | if status >= http.StatusBadRequest { | 476 | if status >= http.StatusBadRequest { | ||
| 446 | w.Header().Set("Content-Type", "application/json") | 477 | w.Header().Set("Content-Type", "application/json") | ||
| 447 | w.WriteHeader(status) | 478 | w.WriteHeader(status) | ||
| 448 | if status == http.StatusNotFound { | 479 | if status == http.StatusNotFound { | ||
| 449 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "repository not found"})) | 480 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "repository not found"})) { |
| 481 | + | return | |||
| 482 | + | } | |||
| 483 | + | return | |||
| 484 | + | } | |||
| 485 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) { | |||
| 450 | return | 486 | return | ||
| 451 | } | 487 | } | ||
| 452 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]string{"error": "boom"})) | |||
| 453 | return | 488 | return | ||
| 454 | } | 489 | } | ||
| 455 | w.Header().Set("Content-Type", "application/json") | 490 | w.Header().Set("Content-Type", "application/json") | ||
| 456 | - | require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"repository": repository, "tags": tags})) | 491 | + | if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"repository": repository, "tags": tags})) { |
| 492 | + | return | |||
| 493 | + | } | |||
| 457 | } | 494 | } | ||
M internal/adapters/in/cli/root.go +25 -24
| @@ -5,6 +5,7 @@ package cli | |||
| 5 | 5 | import ( | |
| 6 | 6 | "context" | |
| 7 | 7 | "fmt" | |
| 8 | + | "io" | |
| 8 | 9 | "os" | |
| 9 | 10 | "os/signal" | |
| 10 | 11 | "syscall" | |
| @@ -298,7 +299,7 @@ Remote mode: | |||
| 298 | 299 | if len(args) > 0 { | |
| 299 | 300 | logDomain = args[0] | |
| 300 | 301 | } | |
| 301 | - | return runLogs(cmd.Context(), logsConfigPath, logDomain, follow, lines) | |
| 302 | + | return runLogs(cmd.Context(), logsConfigPath, logDomain, follow, lines, cmd.OutOrStdout()) | |
| 302 | 303 | }, | |
| 303 | 304 | } | |
| 304 | 305 | ||
| @@ -310,30 +311,30 @@ Remote mode: | |||
| 310 | 311 | } | |
| 311 | 312 | ||
| 312 | 313 | // runLogs handles the logs command logic. | |
| 313 | - | func runLogs(ctx context.Context, logsConfigPath, logDomain string, follow bool, lines int) error { | |
| 314 | + | func runLogs(ctx context.Context, logsConfigPath, logDomain string, follow bool, lines int, out io.Writer) error { | |
| 314 | 315 | if logDomain != "" { | |
| 315 | 316 | handle, err := resolveControlPlaneForRouteDomain(ctx, logDomain) | |
| 316 | 317 | if err != nil { | |
| 317 | 318 | return err | |
| 318 | 319 | } | |
| 319 | 320 | defer handle.close() | |
| 320 | - | return runContainerLogs(ctx, handle.plane, logDomain, follow, lines) | |
| 321 | + | return runContainerLogs(ctx, handle.plane, logDomain, follow, lines, out) | |
| 321 | 322 | } | |
| 322 | 323 | ||
| 323 | 324 | client, isRemote := GetRemoteClient() | |
| 324 | 325 | if isRemote { | |
| 325 | - | return runLogsRemote(client, logDomain, follow, lines) | |
| 326 | + | return runLogsRemote(ctx, client, logDomain, follow, lines, out) | |
| 326 | 327 | } | |
| 327 | - | return runLogsLocal(logsConfigPath, logDomain, follow, lines) | |
| 328 | + | return runLogsLocal(logsConfigPath, logDomain, follow, lines, out) | |
| 328 | 329 | } | |
| 329 | 330 | ||
| 330 | 331 | // runLogsRemote fetches logs from a remote Gordon instance. | |
| 331 | - | func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines int) error { | |
| 332 | - | ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | |
| 332 | + | func runLogsRemote(ctx context.Context, client *remote.Client, logDomain string, follow bool, lines int, out io.Writer) error { | |
| 333 | + | ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) | |
| 333 | 334 | defer cancel() | |
| 334 | 335 | ||
| 335 | 336 | if follow { | |
| 336 | - | return streamLogsRemote(ctx, client, logDomain, lines) | |
| 337 | + | return streamLogsRemote(ctx, client, logDomain, lines, out) | |
| 337 | 338 | } | |
| 338 | 339 | ||
| 339 | 340 | if logDomain == "" { | |
| @@ -343,7 +344,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||
| 343 | 344 | return fmt.Errorf("failed to get process logs: %w", err) | |
| 344 | 345 | } | |
| 345 | 346 | for _, line := range logLines { | |
| 346 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | |
| 347 | + | if err := cliWriteLine(out, line); err != nil { | |
| 347 | 348 | return err | |
| 348 | 349 | } | |
| 349 | 350 | } | |
| @@ -354,7 +355,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||
| 354 | 355 | return fmt.Errorf("failed to get container logs: %w", err) | |
| 355 | 356 | } | |
| 356 | 357 | for _, line := range logLines { | |
| 357 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | |
| 358 | + | if err := cliWriteLine(out, line); err != nil { | |
| 358 | 359 | return err | |
| 359 | 360 | } | |
| 360 | 361 | } | |
| @@ -363,7 +364,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||
| 363 | 364 | } | |
| 364 | 365 | ||
| 365 | 366 | // streamLogsRemote streams logs from a remote Gordon instance. | |
| 366 | - | func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain string, lines int) error { | |
| 367 | + | func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain string, lines int, out io.Writer) error { | |
| 367 | 368 | var ch <-chan string | |
| 368 | 369 | var err error | |
| 369 | 370 | ||
| @@ -377,7 +378,7 @@ func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain stri | |||
| 377 | 378 | } | |
| 378 | 379 | ||
| 379 | 380 | for line := range ch { | |
| 380 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | |
| 381 | + | if err := cliWriteLine(out, line); err != nil { | |
| 381 | 382 | return err | |
| 382 | 383 | } | |
| 383 | 384 | } | |
| @@ -385,16 +386,16 @@ func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain stri | |||
| 385 | 386 | } | |
| 386 | 387 | ||
| 387 | 388 | // runLogsLocal shows logs from a local Gordon instance. | |
| 388 | - | func runLogsLocal(logsConfigPath, logDomain string, follow bool, lines int) error { | |
| 389 | + | func runLogsLocal(logsConfigPath, logDomain string, follow bool, lines int, out io.Writer) error { | |
| 389 | 390 | if logDomain == "" { | |
| 390 | 391 | // Process logs - use existing app.ShowLogs | |
| 391 | 392 | return app.ShowLogs(logsConfigPath, follow, lines) | |
| 392 | 393 | } | |
| 393 | 394 | ||
| 394 | - | return showContainerLogsLocal(logsConfigPath, logDomain, follow, lines) | |
| 395 | + | return showContainerLogsLocal(out, logsConfigPath, logDomain, follow, lines) | |
| 395 | 396 | } | |
| 396 | 397 | ||
| 397 | - | func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, follow bool, lines int) error { | |
| 398 | + | func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, follow bool, lines int, out io.Writer) error { | |
| 398 | 399 | ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) | |
| 399 | 400 | defer cancel() | |
| 400 | 401 | ||
| @@ -404,7 +405,7 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||
| 404 | 405 | return fmt.Errorf("failed to stream container logs: %w", err) | |
| 405 | 406 | } | |
| 406 | 407 | for line := range ch { | |
| 407 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | |
| 408 | + | if err := cliWriteLine(out, line); err != nil { | |
| 408 | 409 | return err | |
| 409 | 410 | } | |
| 410 | 411 | } | |
| @@ -416,7 +417,7 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||
| 416 | 417 | return fmt.Errorf("failed to get container logs: %w", err) | |
| 417 | 418 | } | |
| 418 | 419 | for _, line := range logLines { | |
| 419 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | |
| 420 | + | if err := cliWriteLine(out, line); err != nil { | |
| 420 | 421 | return err | |
| 421 | 422 | } | |
| 422 | 423 | } | |
| @@ -424,25 +425,25 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||
| 424 | 425 | } | |
| 425 | 426 | ||
| 426 | 427 | // showContainerLogsLocal is retained for UI adoption coverage and fallback messaging. | |
| 427 | - | func showContainerLogsLocal(_ string, logDomain string, follow bool, lines int) error { | |
| 428 | - | if err := cliWriteLine(os.Stdout, cliRenderTitle(fmt.Sprintf("Container logs for %s", logDomain))); err != nil { | |
| 428 | + | func showContainerLogsLocal(out io.Writer, _ string, logDomain string, follow bool, lines int) error { | |
| 429 | + | if err := cliWriteLine(out, cliRenderTitle(fmt.Sprintf("Container logs for %s", logDomain))); err != nil { | |
| 429 | 430 | return err | |
| 430 | 431 | } | |
| 431 | - | if err := cliWriteLine(os.Stdout, cliRenderInfo("To view container logs locally, use:")); err != nil { | |
| 432 | + | if err := cliWriteLine(out, cliRenderInfo("To view container logs locally, use:")); err != nil { | |
| 432 | 433 | return err | |
| 433 | 434 | } | |
| 434 | - | if err := cliWritef(os.Stdout, " docker logs --tail %d %s\n", lines, logDomain); err != nil { | |
| 435 | + | if err := cliWritef(out, " docker logs --tail %d %s\n", lines, logDomain); err != nil { | |
| 435 | 436 | return err | |
| 436 | 437 | } | |
| 437 | 438 | if follow { | |
| 438 | - | if err := cliWritef(os.Stdout, " docker logs -f --tail %d %s\n", lines, logDomain); err != nil { | |
| 439 | + | if err := cliWritef(out, " docker logs -f --tail %d %s\n", lines, logDomain); err != nil { | |
| 439 | 440 | return err | |
| 440 | 441 | } | |
| 441 | 442 | } | |
| 442 | - | if err := cliWriteLine(os.Stdout, ""); err != nil { | |
| 443 | + | if err := cliWriteLine(out, ""); err != nil { | |
| 443 | 444 | return err | |
| 444 | 445 | } | |
| 445 | - | if err := cliWriteLine(os.Stdout, cliRenderMuted("Or use remote mode to access logs via the admin API.")); err != nil { | |
| 446 | + | if err := cliWriteLine(out, cliRenderMuted("Or use remote mode to access logs via the admin API.")); err != nil { | |
| 446 | 447 | return err | |
| 447 | 448 | } | |
| 448 | 449 | return nil | |
| @@ -5,6 +5,7 @@ package cli | |||||
| 5 | import ( | 5 | import ( | ||
| 6 | "context" | 6 | "context" | ||
| 7 | "fmt" | 7 | "fmt" | ||
| 8 | + | "io" | |||
| 8 | "os" | 9 | "os" | ||
| 9 | "os/signal" | 10 | "os/signal" | ||
| 10 | "syscall" | 11 | "syscall" | ||
| @@ -298,7 +299,7 @@ Remote mode: | |||||
| 298 | if len(args) > 0 { | 299 | if len(args) > 0 { | ||
| 299 | logDomain = args[0] | 300 | logDomain = args[0] | ||
| 300 | } | 301 | } | ||
| 301 | - | return runLogs(cmd.Context(), logsConfigPath, logDomain, follow, lines) | 302 | + | return runLogs(cmd.Context(), logsConfigPath, logDomain, follow, lines, cmd.OutOrStdout()) |
| 302 | }, | 303 | }, | ||
| 303 | } | 304 | } | ||
| 304 | 305 | ||||
| @@ -310,30 +311,30 @@ Remote mode: | |||||
| 310 | } | 311 | } | ||
| 311 | 312 | ||||
| 312 | // runLogs handles the logs command logic. | 313 | // runLogs handles the logs command logic. | ||
| 313 | - | func runLogs(ctx context.Context, logsConfigPath, logDomain string, follow bool, lines int) error { | 314 | + | func runLogs(ctx context.Context, logsConfigPath, logDomain string, follow bool, lines int, out io.Writer) error { |
| 314 | if logDomain != "" { | 315 | if logDomain != "" { | ||
| 315 | handle, err := resolveControlPlaneForRouteDomain(ctx, logDomain) | 316 | handle, err := resolveControlPlaneForRouteDomain(ctx, logDomain) | ||
| 316 | if err != nil { | 317 | if err != nil { | ||
| 317 | return err | 318 | return err | ||
| 318 | } | 319 | } | ||
| 319 | defer handle.close() | 320 | defer handle.close() | ||
| 320 | - | return runContainerLogs(ctx, handle.plane, logDomain, follow, lines) | 321 | + | return runContainerLogs(ctx, handle.plane, logDomain, follow, lines, out) |
| 321 | } | 322 | } | ||
| 322 | 323 | ||||
| 323 | client, isRemote := GetRemoteClient() | 324 | client, isRemote := GetRemoteClient() | ||
| 324 | if isRemote { | 325 | if isRemote { | ||
| 325 | - | return runLogsRemote(client, logDomain, follow, lines) | 326 | + | return runLogsRemote(ctx, client, logDomain, follow, lines, out) |
| 326 | } | 327 | } | ||
| 327 | - | return runLogsLocal(logsConfigPath, logDomain, follow, lines) | 328 | + | return runLogsLocal(logsConfigPath, logDomain, follow, lines, out) |
| 328 | } | 329 | } | ||
| 329 | 330 | ||||
| 330 | // runLogsRemote fetches logs from a remote Gordon instance. | 331 | // runLogsRemote fetches logs from a remote Gordon instance. | ||
| 331 | - | func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines int) error { | 332 | + | func runLogsRemote(ctx context.Context, client *remote.Client, logDomain string, follow bool, lines int, out io.Writer) error { |
| 332 | - | ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | 333 | + | ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) |
| 333 | defer cancel() | 334 | defer cancel() | ||
| 334 | 335 | ||||
| 335 | if follow { | 336 | if follow { | ||
| 336 | - | return streamLogsRemote(ctx, client, logDomain, lines) | 337 | + | return streamLogsRemote(ctx, client, logDomain, lines, out) |
| 337 | } | 338 | } | ||
| 338 | 339 | ||||
| 339 | if logDomain == "" { | 340 | if logDomain == "" { | ||
| @@ -343,7 +344,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||||
| 343 | return fmt.Errorf("failed to get process logs: %w", err) | 344 | return fmt.Errorf("failed to get process logs: %w", err) | ||
| 344 | } | 345 | } | ||
| 345 | for _, line := range logLines { | 346 | for _, line := range logLines { | ||
| 346 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | 347 | + | if err := cliWriteLine(out, line); err != nil { |
| 347 | return err | 348 | return err | ||
| 348 | } | 349 | } | ||
| 349 | } | 350 | } | ||
| @@ -354,7 +355,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||||
| 354 | return fmt.Errorf("failed to get container logs: %w", err) | 355 | return fmt.Errorf("failed to get container logs: %w", err) | ||
| 355 | } | 356 | } | ||
| 356 | for _, line := range logLines { | 357 | for _, line := range logLines { | ||
| 357 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | 358 | + | if err := cliWriteLine(out, line); err != nil { |
| 358 | return err | 359 | return err | ||
| 359 | } | 360 | } | ||
| 360 | } | 361 | } | ||
| @@ -363,7 +364,7 @@ func runLogsRemote(client *remote.Client, logDomain string, follow bool, lines i | |||||
| 363 | } | 364 | } | ||
| 364 | 365 | ||||
| 365 | // streamLogsRemote streams logs from a remote Gordon instance. | 366 | // streamLogsRemote streams logs from a remote Gordon instance. | ||
| 366 | - | func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain string, lines int) error { | 367 | + | func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain string, lines int, out io.Writer) error { |
| 367 | var ch <-chan string | 368 | var ch <-chan string | ||
| 368 | var err error | 369 | var err error | ||
| 369 | 370 | ||||
| @@ -377,7 +378,7 @@ func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain stri | |||||
| 377 | } | 378 | } | ||
| 378 | 379 | ||||
| 379 | for line := range ch { | 380 | for line := range ch { | ||
| 380 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | 381 | + | if err := cliWriteLine(out, line); err != nil { |
| 381 | return err | 382 | return err | ||
| 382 | } | 383 | } | ||
| 383 | } | 384 | } | ||
| @@ -385,16 +386,16 @@ func streamLogsRemote(ctx context.Context, client *remote.Client, logDomain stri | |||||
| 385 | } | 386 | } | ||
| 386 | 387 | ||||
| 387 | // runLogsLocal shows logs from a local Gordon instance. | 388 | // runLogsLocal shows logs from a local Gordon instance. | ||
| 388 | - | func runLogsLocal(logsConfigPath, logDomain string, follow bool, lines int) error { | 389 | + | func runLogsLocal(logsConfigPath, logDomain string, follow bool, lines int, out io.Writer) error { |
| 389 | if logDomain == "" { | 390 | if logDomain == "" { | ||
| 390 | // Process logs - use existing app.ShowLogs | 391 | // Process logs - use existing app.ShowLogs | ||
| 391 | return app.ShowLogs(logsConfigPath, follow, lines) | 392 | return app.ShowLogs(logsConfigPath, follow, lines) | ||
| 392 | } | 393 | } | ||
| 393 | 394 | ||||
| 394 | - | return showContainerLogsLocal(logsConfigPath, logDomain, follow, lines) | 395 | + | return showContainerLogsLocal(out, logsConfigPath, logDomain, follow, lines) |
| 395 | } | 396 | } | ||
| 396 | 397 | ||||
| 397 | - | func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, follow bool, lines int) error { | 398 | + | func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, follow bool, lines int, out io.Writer) error { |
| 398 | ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) | 399 | ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) | ||
| 399 | defer cancel() | 400 | defer cancel() | ||
| 400 | 401 | ||||
| @@ -404,7 +405,7 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||||
| 404 | return fmt.Errorf("failed to stream container logs: %w", err) | 405 | return fmt.Errorf("failed to stream container logs: %w", err) | ||
| 405 | } | 406 | } | ||
| 406 | for line := range ch { | 407 | for line := range ch { | ||
| 407 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | 408 | + | if err := cliWriteLine(out, line); err != nil { |
| 408 | return err | 409 | return err | ||
| 409 | } | 410 | } | ||
| 410 | } | 411 | } | ||
| @@ -416,7 +417,7 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||||
| 416 | return fmt.Errorf("failed to get container logs: %w", err) | 417 | return fmt.Errorf("failed to get container logs: %w", err) | ||
| 417 | } | 418 | } | ||
| 418 | for _, line := range logLines { | 419 | for _, line := range logLines { | ||
| 419 | - | if err := cliWriteLine(os.Stdout, line); err != nil { | 420 | + | if err := cliWriteLine(out, line); err != nil { |
| 420 | return err | 421 | return err | ||
| 421 | } | 422 | } | ||
| 422 | } | 423 | } | ||
| @@ -424,25 +425,25 @@ func runContainerLogs(ctx context.Context, cp ControlPlane, logDomain string, fo | |||||
| 424 | } | 425 | } | ||
| 425 | 426 | ||||
| 426 | // showContainerLogsLocal is retained for UI adoption coverage and fallback messaging. | 427 | // showContainerLogsLocal is retained for UI adoption coverage and fallback messaging. | ||
| 427 | - | func showContainerLogsLocal(_ string, logDomain string, follow bool, lines int) error { | 428 | + | func showContainerLogsLocal(out io.Writer, _ string, logDomain string, follow bool, lines int) error { |
| 428 | - | if err := cliWriteLine(os.Stdout, cliRenderTitle(fmt.Sprintf("Container logs for %s", logDomain))); err != nil { | 429 | + | if err := cliWriteLine(out, cliRenderTitle(fmt.Sprintf("Container logs for %s", logDomain))); err != nil { |
| 429 | return err | 430 | return err | ||
| 430 | } | 431 | } | ||
| 431 | - | if err := cliWriteLine(os.Stdout, cliRenderInfo("To view container logs locally, use:")); err != nil { | 432 | + | if err := cliWriteLine(out, cliRenderInfo("To view container logs locally, use:")); err != nil { |
| 432 | return err | 433 | return err | ||
| 433 | } | 434 | } | ||
| 434 | - | if err := cliWritef(os.Stdout, " docker logs --tail %d %s\n", lines, logDomain); err != nil { | 435 | + | if err := cliWritef(out, " docker logs --tail %d %s\n", lines, logDomain); err != nil { |
| 435 | return err | 436 | return err | ||
| 436 | } | 437 | } | ||
| 437 | if follow { | 438 | if follow { | ||
| 438 | - | if err := cliWritef(os.Stdout, " docker logs -f --tail %d %s\n", lines, logDomain); err != nil { | 439 | + | if err := cliWritef(out, " docker logs -f --tail %d %s\n", lines, logDomain); err != nil { |
| 439 | return err | 440 | return err | ||
| 440 | } | 441 | } | ||
| 441 | } | 442 | } | ||
| 442 | - | if err := cliWriteLine(os.Stdout, ""); err != nil { | 443 | + | if err := cliWriteLine(out, ""); err != nil { |
| 443 | return err | 444 | return err | ||
| 444 | } | 445 | } | ||
| 445 | - | if err := cliWriteLine(os.Stdout, cliRenderMuted("Or use remote mode to access logs via the admin API.")); err != nil { | 446 | + | if err := cliWriteLine(out, cliRenderMuted("Or use remote mode to access logs via the admin API.")); err != nil { |
| 446 | return err | 447 | return err | ||
| 447 | } | 448 | } | ||
| 448 | return nil | 449 | return nil | ||
M internal/adapters/in/cli/routes.go +1 -1
| @@ -1162,7 +1162,7 @@ Examples: | |||
| 1162 | 1162 | gordon routes remove app.mydomain.com --force`, | |
| 1163 | 1163 | Args: cobra.ExactArgs(1), | |
| 1164 | 1164 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 1165 | - | ctx := context.Background() | |
| 1165 | + | ctx := cmd.Context() | |
| 1166 | 1166 | routeDomain := args[0] | |
| 1167 | 1167 | ||
| 1168 | 1168 | // Confirm unless --force | |
| @@ -1162,7 +1162,7 @@ Examples: | |||||
| 1162 | gordon routes remove app.mydomain.com --force`, | 1162 | gordon routes remove app.mydomain.com --force`, | ||
| 1163 | Args: cobra.ExactArgs(1), | 1163 | Args: cobra.ExactArgs(1), | ||
| 1164 | RunE: func(cmd *cobra.Command, args []string) error { | 1164 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 1165 | - | ctx := context.Background() | 1165 | + | ctx := cmd.Context() |
| 1166 | routeDomain := args[0] | 1166 | routeDomain := args[0] | ||
| 1167 | 1167 | ||||
| 1168 | // Confirm unless --force | 1168 | // Confirm unless --force | ||