fix: reduce runRemotesList cyclomatic complexity
Extract remotesListEmpty, sortedRemoteNames, and remotesListJSON helpers to bring runRemotesList under gocyclo threshold.
- commit
- 3cbff78ff4c8bd3b98ae405db1a083ae997a0e17
- author
- brice · · 2026-03-13 21:14 UTC
- parent
- 2889a53
- signature
- unsigned
1 file · +42 -29
M internal/adapters/in/cli/remotes.go +42 -29
| @@ -59,39 +59,13 @@ func runRemotesList(out io.Writer, jsonOut bool) error { | |||
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | 61 | if len(remotes) == 0 { | |
| 62 | - | if jsonOut { | |
| 63 | - | return writeJSON(out, []map[string]any{}) | |
| 64 | - | } | |
| 65 | - | if err := cliWriteLine(out, cliRenderMuted("No remotes configured")); err != nil { | |
| 66 | - | return err | |
| 67 | - | } | |
| 68 | - | if err := cliWriteLine(out, ""); err != nil { | |
| 69 | - | return err | |
| 70 | - | } | |
| 71 | - | if err := cliWriteLine(out, "Add a remote with:"); err != nil { | |
| 72 | - | return err | |
| 73 | - | } | |
| 74 | - | return cliWriteLine(out, styles.Theme.Bold.Render(" gordon remotes add <name> <url>")) | |
| 62 | + | return remotesListEmpty(out, jsonOut) | |
| 75 | 63 | } | |
| 76 | 64 | ||
| 77 | - | names := make([]string, 0, len(remotes)) | |
| 78 | - | for name := range remotes { | |
| 79 | - | names = append(names, name) | |
| 80 | - | } | |
| 81 | - | sort.Strings(names) | |
| 65 | + | names := sortedRemoteNames(remotes) | |
| 82 | 66 | ||
| 83 | 67 | if jsonOut { | |
| 84 | - | payload := make([]map[string]any, 0, len(names)) | |
| 85 | - | for _, name := range names { | |
| 86 | - | r := remotes[name] | |
| 87 | - | payload = append(payload, map[string]any{ | |
| 88 | - | "name": name, | |
| 89 | - | "url": r.URL, | |
| 90 | - | "active": name == active, | |
| 91 | - | "insecure_tls": r.InsecureTLS, | |
| 92 | - | }) | |
| 93 | - | } | |
| 94 | - | return writeJSON(out, payload) | |
| 68 | + | return remotesListJSON(out, remotes, names, active) | |
| 95 | 69 | } | |
| 96 | 70 | ||
| 97 | 71 | if err := cliWriteLine(out, cliRenderTitle("Saved Remotes")); err != nil { | |
| @@ -139,6 +113,45 @@ func runRemotesList(out io.Writer, jsonOut bool) error { | |||
| 139 | 113 | return err | |
| 140 | 114 | } | |
| 141 | 115 | ||
| 116 | + | func remotesListEmpty(out io.Writer, jsonOut bool) error { | |
| 117 | + | if jsonOut { | |
| 118 | + | return writeJSON(out, []map[string]any{}) | |
| 119 | + | } | |
| 120 | + | if err := cliWriteLine(out, cliRenderMuted("No remotes configured")); err != nil { | |
| 121 | + | return err | |
| 122 | + | } | |
| 123 | + | if err := cliWriteLine(out, ""); err != nil { | |
| 124 | + | return err | |
| 125 | + | } | |
| 126 | + | if err := cliWriteLine(out, "Add a remote with:"); err != nil { | |
| 127 | + | return err | |
| 128 | + | } | |
| 129 | + | return cliWriteLine(out, styles.Theme.Bold.Render(" gordon remotes add <name> <url>")) | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | func sortedRemoteNames(remotes map[string]remote.RemoteEntry) []string { | |
| 133 | + | names := make([]string, 0, len(remotes)) | |
| 134 | + | for name := range remotes { | |
| 135 | + | names = append(names, name) | |
| 136 | + | } | |
| 137 | + | sort.Strings(names) | |
| 138 | + | return names | |
| 139 | + | } | |
| 140 | + | ||
| 141 | + | func remotesListJSON(out io.Writer, remotes map[string]remote.RemoteEntry, names []string, active string) error { | |
| 142 | + | payload := make([]map[string]any, 0, len(names)) | |
| 143 | + | for _, name := range names { | |
| 144 | + | r := remotes[name] | |
| 145 | + | payload = append(payload, map[string]any{ | |
| 146 | + | "name": name, | |
| 147 | + | "url": r.URL, | |
| 148 | + | "active": name == active, | |
| 149 | + | "insecure_tls": r.InsecureTLS, | |
| 150 | + | }) | |
| 151 | + | } | |
| 152 | + | return writeJSON(out, payload) | |
| 153 | + | } | |
| 154 | + | ||
| 142 | 155 | // newRemotesAddCmd creates the remotes add command. | |
| 143 | 156 | func newRemotesAddCmd() *cobra.Command { | |
| 144 | 157 | var token string | |
| @@ -59,39 +59,13 @@ func runRemotesList(out io.Writer, jsonOut bool) error { | |||||
| 59 | } | 59 | } | ||
| 60 | 60 | ||||
| 61 | if len(remotes) == 0 { | 61 | if len(remotes) == 0 { | ||
| 62 | - | if jsonOut { | 62 | + | return remotesListEmpty(out, jsonOut) |
| 63 | - | return writeJSON(out, []map[string]any{}) | |||
| 64 | - | } | |||
| 65 | - | if err := cliWriteLine(out, cliRenderMuted("No remotes configured")); err != nil { | |||
| 66 | - | return err | |||
| 67 | - | } | |||
| 68 | - | if err := cliWriteLine(out, ""); err != nil { | |||
| 69 | - | return err | |||
| 70 | - | } | |||
| 71 | - | if err := cliWriteLine(out, "Add a remote with:"); err != nil { | |||
| 72 | - | return err | |||
| 73 | - | } | |||
| 74 | - | return cliWriteLine(out, styles.Theme.Bold.Render(" gordon remotes add <name> <url>")) | |||
| 75 | } | 63 | } | ||
| 76 | 64 | ||||
| 77 | - | names := make([]string, 0, len(remotes)) | 65 | + | names := sortedRemoteNames(remotes) |
| 78 | - | for name := range remotes { | |||
| 79 | - | names = append(names, name) | |||
| 80 | - | } | |||
| 81 | - | sort.Strings(names) | |||
| 82 | 66 | ||||
| 83 | if jsonOut { | 67 | if jsonOut { | ||
| 84 | - | payload := make([]map[string]any, 0, len(names)) | 68 | + | return remotesListJSON(out, remotes, names, active) |
| 85 | - | for _, name := range names { | |||
| 86 | - | r := remotes[name] | |||
| 87 | - | payload = append(payload, map[string]any{ | |||
| 88 | - | "name": name, | |||
| 89 | - | "url": r.URL, | |||
| 90 | - | "active": name == active, | |||
| 91 | - | "insecure_tls": r.InsecureTLS, | |||
| 92 | - | }) | |||
| 93 | - | } | |||
| 94 | - | return writeJSON(out, payload) | |||
| 95 | } | 69 | } | ||
| 96 | 70 | ||||
| 97 | if err := cliWriteLine(out, cliRenderTitle("Saved Remotes")); err != nil { | 71 | if err := cliWriteLine(out, cliRenderTitle("Saved Remotes")); err != nil { | ||
| @@ -139,6 +113,45 @@ func runRemotesList(out io.Writer, jsonOut bool) error { | |||||
| 139 | return err | 113 | return err | ||
| 140 | } | 114 | } | ||
| 141 | 115 | ||||
| 116 | + | func remotesListEmpty(out io.Writer, jsonOut bool) error { | |||
| 117 | + | if jsonOut { | |||
| 118 | + | return writeJSON(out, []map[string]any{}) | |||
| 119 | + | } | |||
| 120 | + | if err := cliWriteLine(out, cliRenderMuted("No remotes configured")); err != nil { | |||
| 121 | + | return err | |||
| 122 | + | } | |||
| 123 | + | if err := cliWriteLine(out, ""); err != nil { | |||
| 124 | + | return err | |||
| 125 | + | } | |||
| 126 | + | if err := cliWriteLine(out, "Add a remote with:"); err != nil { | |||
| 127 | + | return err | |||
| 128 | + | } | |||
| 129 | + | return cliWriteLine(out, styles.Theme.Bold.Render(" gordon remotes add <name> <url>")) | |||
| 130 | + | } | |||
| 131 | + | ||||
| 132 | + | func sortedRemoteNames(remotes map[string]remote.RemoteEntry) []string { | |||
| 133 | + | names := make([]string, 0, len(remotes)) | |||
| 134 | + | for name := range remotes { | |||
| 135 | + | names = append(names, name) | |||
| 136 | + | } | |||
| 137 | + | sort.Strings(names) | |||
| 138 | + | return names | |||
| 139 | + | } | |||
| 140 | + | ||||
| 141 | + | func remotesListJSON(out io.Writer, remotes map[string]remote.RemoteEntry, names []string, active string) error { | |||
| 142 | + | payload := make([]map[string]any, 0, len(names)) | |||
| 143 | + | for _, name := range names { | |||
| 144 | + | r := remotes[name] | |||
| 145 | + | payload = append(payload, map[string]any{ | |||
| 146 | + | "name": name, | |||
| 147 | + | "url": r.URL, | |||
| 148 | + | "active": name == active, | |||
| 149 | + | "insecure_tls": r.InsecureTLS, | |||
| 150 | + | }) | |||
| 151 | + | } | |||
| 152 | + | return writeJSON(out, payload) | |||
| 153 | + | } | |||
| 154 | + | ||||
| 142 | // newRemotesAddCmd creates the remotes add command. | 155 | // newRemotesAddCmd creates the remotes add command. | ||
| 143 | func newRemotesAddCmd() *cobra.Command { | 156 | func newRemotesAddCmd() *cobra.Command { | ||
| 144 | var token string | 157 | var token string | ||