detached 3cbff78

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 {
5959 }
6060
6161 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)
7563 }
7664
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)
8266
8367 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)
9569 }
9670
9771 if err := cliWriteLine(out, cliRenderTitle("Saved Remotes")); err != nil {
@@ -139,6 +113,45 @@ func runRemotesList(out io.Writer, jsonOut bool) error {
139113 return err
140114 }
141115
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+
142155 // newRemotesAddCmd creates the remotes add command.
143156 func newRemotesAddCmd() *cobra.Command {
144157 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 err113 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 string157 var token string

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close