refactor(cli): route pin command output through cliWriteLine/cliWritef, wrap errors
- commit
- 90c5cb76110de9d81e9305f42f157e96a2143045
- author
- brice · · 2026-04-28 03:25 UTC
- parent
- 9dd1391
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +22 -20
M internal/adapters/in/cli/pin.go +22 -20
| @@ -4,7 +4,6 @@ import ( | |||
| 4 | 4 | "context" | |
| 5 | 5 | "fmt" | |
| 6 | 6 | "io" | |
| 7 | - | "os" | |
| 8 | 7 | "slices" | |
| 9 | 8 | "sort" | |
| 10 | 9 | "strings" | |
| @@ -32,7 +31,7 @@ Examples: | |||
| 32 | 31 | gordon pin list myapp.example.com --remote ...`, | |
| 33 | 32 | Args: cobra.ExactArgs(1), | |
| 34 | 33 | RunE: func(cmd *cobra.Command, args []string) error { | |
| 35 | - | return runPin(cmd.Context(), args[0], targetTag) | |
| 34 | + | return runPin(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), args[0], targetTag) | |
| 36 | 35 | }, | |
| 37 | 36 | } | |
| 38 | 37 | ||
| @@ -59,10 +58,10 @@ func newPinListCmd() *cobra.Command { | |||
| 59 | 58 | return cmd | |
| 60 | 59 | } | |
| 61 | 60 | ||
| 62 | - | func runPin(ctx context.Context, pinDomain, targetTag string) error { | |
| 61 | + | func runPin(ctx context.Context, out, ew io.Writer, pinDomain, targetTag string) error { | |
| 63 | 62 | handle, err := resolveControlPlane(configPath) | |
| 64 | 63 | if err != nil { | |
| 65 | - | return err | |
| 64 | + | return fmt.Errorf("failed to resolve control plane: %w", err) | |
| 66 | 65 | } | |
| 67 | 66 | defer handle.close() | |
| 68 | 67 | ||
| @@ -78,26 +77,28 @@ func runPin(ctx context.Context, pinDomain, targetTag string) error { | |||
| 78 | 77 | ||
| 79 | 78 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | |
| 80 | 79 | if err != nil { | |
| 81 | - | return err | |
| 80 | + | return fmt.Errorf("failed to fetch tags: %w", err) | |
| 82 | 81 | } | |
| 83 | 82 | ||
| 84 | - | selectedTag, err := selectTag(targetTag, tags, currentTag, pinDomain) | |
| 85 | - | if err != nil || selectedTag == "" { | |
| 86 | - | return err | |
| 83 | + | selectedTag, err := selectTag(targetTag, tags, currentTag, pinDomain, out) | |
| 84 | + | if err != nil { | |
| 85 | + | return fmt.Errorf("failed to select tag: %w", err) | |
| 86 | + | } | |
| 87 | + | if selectedTag == "" { | |
| 88 | + | return nil | |
| 87 | 89 | } | |
| 88 | 90 | ||
| 89 | 91 | if selectedTag == currentTag { | |
| 90 | - | fmt.Println(styles.RenderWarning(fmt.Sprintf("Already running %s", selectedTag))) | |
| 91 | - | return nil | |
| 92 | + | return cliWriteLine(out, styles.RenderWarning(fmt.Sprintf("Already running %s", selectedTag))) | |
| 92 | 93 | } | |
| 93 | 94 | ||
| 94 | - | return deploySelectedTag(ctx, handle.plane, route, pinDomain, imageName, selectedTag) | |
| 95 | + | return deploySelectedTag(ctx, handle.plane, route, out, ew, pinDomain, imageName, selectedTag) | |
| 95 | 96 | } | |
| 96 | 97 | ||
| 97 | 98 | func runPinList(ctx context.Context, w io.Writer, pinDomain string, jsonOut bool) error { | |
| 98 | 99 | handle, err := resolveControlPlane(configPath) | |
| 99 | 100 | if err != nil { | |
| 100 | - | return err | |
| 101 | + | return fmt.Errorf("failed to resolve control plane: %w", err) | |
| 101 | 102 | } | |
| 102 | 103 | defer handle.close() | |
| 103 | 104 | ||
| @@ -113,7 +114,7 @@ func runPinList(ctx context.Context, w io.Writer, pinDomain string, jsonOut bool | |||
| 113 | 114 | ||
| 114 | 115 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | |
| 115 | 116 | if err != nil { | |
| 116 | - | return err | |
| 117 | + | return fmt.Errorf("failed to fetch tags: %w", err) | |
| 117 | 118 | } | |
| 118 | 119 | ||
| 119 | 120 | return printPinTags(w, pinDomain, currentTag, tags, jsonOut) | |
| @@ -193,7 +194,7 @@ func sortSemverTags(tags []string) []string { | |||
| 193 | 194 | return append(semverTags, otherTags...) | |
| 194 | 195 | } | |
| 195 | 196 | ||
| 196 | - | func selectTag(targetTag string, tags []string, currentTag, pinDomain string) (string, error) { | |
| 197 | + | func selectTag(targetTag string, tags []string, currentTag, pinDomain string, out io.Writer) (string, error) { | |
| 197 | 198 | if targetTag != "" { | |
| 198 | 199 | if !validateTagExists(targetTag, tags) { | |
| 199 | 200 | return "", fmt.Errorf("tag %s not found. Available: %s", targetTag, strings.Join(tags, ", ")) | |
| @@ -210,7 +211,7 @@ func selectTag(targetTag string, tags []string, currentTag, pinDomain string) (s | |||
| 210 | 211 | return "", err | |
| 211 | 212 | } | |
| 212 | 213 | if selectedTag == "" { | |
| 213 | - | fmt.Println("Cancelled.") | |
| 214 | + | _ = cliWriteLine(out, "Cancelled.") | |
| 214 | 215 | } | |
| 215 | 216 | return selectedTag, nil | |
| 216 | 217 | } | |
| @@ -219,12 +220,14 @@ func validateTagExists(targetTag string, tags []string) bool { | |||
| 219 | 220 | return slices.Contains(tags, targetTag) | |
| 220 | 221 | } | |
| 221 | 222 | ||
| 222 | - | func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route, pinDomain, imageName, selectedTag string) error { | |
| 223 | + | func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route, out, ew io.Writer, pinDomain, imageName, selectedTag string) error { | |
| 223 | 224 | registry, _, _ := parseImageRef(route.Image) | |
| 224 | 225 | oldImage := route.Image | |
| 225 | 226 | route.Image = fmt.Sprintf("%s/%s:%s", registry, imageName, selectedTag) | |
| 226 | 227 | ||
| 227 | - | fmt.Printf("Pinning to %s...\n", styles.Theme.Bold.Render(selectedTag)) | |
| 228 | + | if err := cliWritef(out, "Pinning to %s...\n", styles.Theme.Bold.Render(selectedTag)); err != nil { | |
| 229 | + | return err | |
| 230 | + | } | |
| 228 | 231 | ||
| 229 | 232 | if err := cp.UpdateRoute(ctx, *route); err != nil { | |
| 230 | 233 | return fmt.Errorf("failed to update route: %w", err) | |
| @@ -235,12 +238,11 @@ func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route | |||
| 235 | 238 | // Attempt to revert route to previous image | |
| 236 | 239 | route.Image = oldImage | |
| 237 | 240 | if revertErr := cp.UpdateRoute(ctx, *route); revertErr != nil { | |
| 238 | - | fmt.Fprintf(os.Stderr, "WARNING: deploy failed and could not revert route: %v\n", revertErr) | |
| 241 | + | _ = cliWritef(ew, "WARNING: deploy failed and could not revert route: %v\n", revertErr) | |
| 239 | 242 | return fmt.Errorf("failed to deploy; revert failed: %v; deploy error: %w", revertErr, err) | |
| 240 | 243 | } | |
| 241 | 244 | return fmt.Errorf("failed to deploy (route reverted): %w", err) | |
| 242 | 245 | } | |
| 243 | 246 | containerID := shortContainerID(result.ContainerID) | |
| 244 | - | fmt.Println(styles.RenderSuccess(fmt.Sprintf("Pinned %s to %s (container: %s)", pinDomain, selectedTag, containerID))) | |
| 245 | - | return nil | |
| 247 | + | return cliWriteLine(out, styles.RenderSuccess(fmt.Sprintf("Pinned %s to %s (container: %s)", pinDomain, selectedTag, containerID))) | |
| 246 | 248 | } | |
| @@ -4,7 +4,6 @@ import ( | |||||
| 4 | "context" | 4 | "context" | ||
| 5 | "fmt" | 5 | "fmt" | ||
| 6 | "io" | 6 | "io" | ||
| 7 | - | "os" | |||
| 8 | "slices" | 7 | "slices" | ||
| 9 | "sort" | 8 | "sort" | ||
| 10 | "strings" | 9 | "strings" | ||
| @@ -32,7 +31,7 @@ Examples: | |||||
| 32 | gordon pin list myapp.example.com --remote ...`, | 31 | gordon pin list myapp.example.com --remote ...`, | ||
| 33 | Args: cobra.ExactArgs(1), | 32 | Args: cobra.ExactArgs(1), | ||
| 34 | RunE: func(cmd *cobra.Command, args []string) error { | 33 | RunE: func(cmd *cobra.Command, args []string) error { | ||
| 35 | - | return runPin(cmd.Context(), args[0], targetTag) | 34 | + | return runPin(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), args[0], targetTag) |
| 36 | }, | 35 | }, | ||
| 37 | } | 36 | } | ||
| 38 | 37 | ||||
| @@ -59,10 +58,10 @@ func newPinListCmd() *cobra.Command { | |||||
| 59 | return cmd | 58 | return cmd | ||
| 60 | } | 59 | } | ||
| 61 | 60 | ||||
| 62 | - | func runPin(ctx context.Context, pinDomain, targetTag string) error { | 61 | + | func runPin(ctx context.Context, out, ew io.Writer, pinDomain, targetTag string) error { |
| 63 | handle, err := resolveControlPlane(configPath) | 62 | handle, err := resolveControlPlane(configPath) | ||
| 64 | if err != nil { | 63 | if err != nil { | ||
| 65 | - | return err | 64 | + | return fmt.Errorf("failed to resolve control plane: %w", err) |
| 66 | } | 65 | } | ||
| 67 | defer handle.close() | 66 | defer handle.close() | ||
| 68 | 67 | ||||
| @@ -78,26 +77,28 @@ func runPin(ctx context.Context, pinDomain, targetTag string) error { | |||||
| 78 | 77 | ||||
| 79 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | 78 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | ||
| 80 | if err != nil { | 79 | if err != nil { | ||
| 81 | - | return err | 80 | + | return fmt.Errorf("failed to fetch tags: %w", err) |
| 82 | } | 81 | } | ||
| 83 | 82 | ||||
| 84 | - | selectedTag, err := selectTag(targetTag, tags, currentTag, pinDomain) | 83 | + | selectedTag, err := selectTag(targetTag, tags, currentTag, pinDomain, out) |
| 85 | - | if err != nil || selectedTag == "" { | 84 | + | if err != nil { |
| 86 | - | return err | 85 | + | return fmt.Errorf("failed to select tag: %w", err) |
| 86 | + | } | |||
| 87 | + | if selectedTag == "" { | |||
| 88 | + | return nil | |||
| 87 | } | 89 | } | ||
| 88 | 90 | ||||
| 89 | if selectedTag == currentTag { | 91 | if selectedTag == currentTag { | ||
| 90 | - | fmt.Println(styles.RenderWarning(fmt.Sprintf("Already running %s", selectedTag))) | 92 | + | return cliWriteLine(out, styles.RenderWarning(fmt.Sprintf("Already running %s", selectedTag))) |
| 91 | - | return nil | |||
| 92 | } | 93 | } | ||
| 93 | 94 | ||||
| 94 | - | return deploySelectedTag(ctx, handle.plane, route, pinDomain, imageName, selectedTag) | 95 | + | return deploySelectedTag(ctx, handle.plane, route, out, ew, pinDomain, imageName, selectedTag) |
| 95 | } | 96 | } | ||
| 96 | 97 | ||||
| 97 | func runPinList(ctx context.Context, w io.Writer, pinDomain string, jsonOut bool) error { | 98 | func runPinList(ctx context.Context, w io.Writer, pinDomain string, jsonOut bool) error { | ||
| 98 | handle, err := resolveControlPlane(configPath) | 99 | handle, err := resolveControlPlane(configPath) | ||
| 99 | if err != nil { | 100 | if err != nil { | ||
| 100 | - | return err | 101 | + | return fmt.Errorf("failed to resolve control plane: %w", err) |
| 101 | } | 102 | } | ||
| 102 | defer handle.close() | 103 | defer handle.close() | ||
| 103 | 104 | ||||
| @@ -113,7 +114,7 @@ func runPinList(ctx context.Context, w io.Writer, pinDomain string, jsonOut bool | |||||
| 113 | 114 | ||||
| 114 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | 115 | tags, err := fetchAndSortTags(ctx, handle.plane, imageName) | ||
| 115 | if err != nil { | 116 | if err != nil { | ||
| 116 | - | return err | 117 | + | return fmt.Errorf("failed to fetch tags: %w", err) |
| 117 | } | 118 | } | ||
| 118 | 119 | ||||
| 119 | return printPinTags(w, pinDomain, currentTag, tags, jsonOut) | 120 | return printPinTags(w, pinDomain, currentTag, tags, jsonOut) | ||
| @@ -193,7 +194,7 @@ func sortSemverTags(tags []string) []string { | |||||
| 193 | return append(semverTags, otherTags...) | 194 | return append(semverTags, otherTags...) | ||
| 194 | } | 195 | } | ||
| 195 | 196 | ||||
| 196 | - | func selectTag(targetTag string, tags []string, currentTag, pinDomain string) (string, error) { | 197 | + | func selectTag(targetTag string, tags []string, currentTag, pinDomain string, out io.Writer) (string, error) { |
| 197 | if targetTag != "" { | 198 | if targetTag != "" { | ||
| 198 | if !validateTagExists(targetTag, tags) { | 199 | if !validateTagExists(targetTag, tags) { | ||
| 199 | return "", fmt.Errorf("tag %s not found. Available: %s", targetTag, strings.Join(tags, ", ")) | 200 | return "", fmt.Errorf("tag %s not found. Available: %s", targetTag, strings.Join(tags, ", ")) | ||
| @@ -210,7 +211,7 @@ func selectTag(targetTag string, tags []string, currentTag, pinDomain string) (s | |||||
| 210 | return "", err | 211 | return "", err | ||
| 211 | } | 212 | } | ||
| 212 | if selectedTag == "" { | 213 | if selectedTag == "" { | ||
| 213 | - | fmt.Println("Cancelled.") | 214 | + | _ = cliWriteLine(out, "Cancelled.") |
| 214 | } | 215 | } | ||
| 215 | return selectedTag, nil | 216 | return selectedTag, nil | ||
| 216 | } | 217 | } | ||
| @@ -219,12 +220,14 @@ func validateTagExists(targetTag string, tags []string) bool { | |||||
| 219 | return slices.Contains(tags, targetTag) | 220 | return slices.Contains(tags, targetTag) | ||
| 220 | } | 221 | } | ||
| 221 | 222 | ||||
| 222 | - | func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route, pinDomain, imageName, selectedTag string) error { | 223 | + | func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route, out, ew io.Writer, pinDomain, imageName, selectedTag string) error { |
| 223 | registry, _, _ := parseImageRef(route.Image) | 224 | registry, _, _ := parseImageRef(route.Image) | ||
| 224 | oldImage := route.Image | 225 | oldImage := route.Image | ||
| 225 | route.Image = fmt.Sprintf("%s/%s:%s", registry, imageName, selectedTag) | 226 | route.Image = fmt.Sprintf("%s/%s:%s", registry, imageName, selectedTag) | ||
| 226 | 227 | ||||
| 227 | - | fmt.Printf("Pinning to %s...\n", styles.Theme.Bold.Render(selectedTag)) | 228 | + | if err := cliWritef(out, "Pinning to %s...\n", styles.Theme.Bold.Render(selectedTag)); err != nil { |
| 229 | + | return err | |||
| 230 | + | } | |||
| 228 | 231 | ||||
| 229 | if err := cp.UpdateRoute(ctx, *route); err != nil { | 232 | if err := cp.UpdateRoute(ctx, *route); err != nil { | ||
| 230 | return fmt.Errorf("failed to update route: %w", err) | 233 | return fmt.Errorf("failed to update route: %w", err) | ||
| @@ -235,12 +238,11 @@ func deploySelectedTag(ctx context.Context, cp ControlPlane, route *domain.Route | |||||
| 235 | // Attempt to revert route to previous image | 238 | // Attempt to revert route to previous image | ||
| 236 | route.Image = oldImage | 239 | route.Image = oldImage | ||
| 237 | if revertErr := cp.UpdateRoute(ctx, *route); revertErr != nil { | 240 | if revertErr := cp.UpdateRoute(ctx, *route); revertErr != nil { | ||
| 238 | - | fmt.Fprintf(os.Stderr, "WARNING: deploy failed and could not revert route: %v\n", revertErr) | 241 | + | _ = cliWritef(ew, "WARNING: deploy failed and could not revert route: %v\n", revertErr) |
| 239 | return fmt.Errorf("failed to deploy; revert failed: %v; deploy error: %w", revertErr, err) | 242 | return fmt.Errorf("failed to deploy; revert failed: %v; deploy error: %w", revertErr, err) | ||
| 240 | } | 243 | } | ||
| 241 | return fmt.Errorf("failed to deploy (route reverted): %w", err) | 244 | return fmt.Errorf("failed to deploy (route reverted): %w", err) | ||
| 242 | } | 245 | } | ||
| 243 | containerID := shortContainerID(result.ContainerID) | 246 | containerID := shortContainerID(result.ContainerID) | ||
| 244 | - | fmt.Println(styles.RenderSuccess(fmt.Sprintf("Pinned %s to %s (container: %s)", pinDomain, selectedTag, containerID))) | 247 | + | return cliWriteLine(out, styles.RenderSuccess(fmt.Sprintf("Pinned %s to %s (container: %s)", pinDomain, selectedTag, containerID))) |
| 245 | - | return nil | |||
| 246 | } | 248 | } | ||