Skip to content

Commit 46e1631

Browse files
committed
Refactor mockRunnerService methods and improve model update logic in GitLab Runner TUI. Updated method signatures to use underscore for unused parameters, enhancing code clarity. Refactored Update method in model to streamline window size and key press handling, improving maintainability. Adjusted various view updates for better input handling and user experience.
1 parent 72efd3e commit 46e1631

File tree

12 files changed

+301
-228
lines changed

12 files changed

+301
-228
lines changed

cmd/gitlab-runner-tui/main.go

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -54,81 +54,95 @@ func (m model) Init() tea.Cmd {
5454
}
5555

5656
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
57-
var cmds []tea.Cmd
58-
5957
switch msg := msg.(type) {
6058
case tea.WindowSizeMsg:
61-
m.width = msg.Width
62-
m.height = msg.Height
59+
return m.handleWindowSize(msg)
60+
case tea.KeyMsg:
61+
return m.handleKeyPress(msg)
62+
}
6363

64-
m.runnersView.Update(msg)
65-
m.logsView.Update(msg)
66-
m.configView.Update(msg)
67-
m.systemView.Update(msg)
68-
m.historyView.Update(msg)
64+
return m.updateActiveView(msg)
65+
}
6966

70-
return m, nil
67+
func (m model) handleWindowSize(msg tea.WindowSizeMsg) (tea.Model, tea.Cmd) {
68+
m.width = msg.Width
69+
m.height = msg.Height
7170

72-
case tea.KeyMsg:
73-
switch msg.String() {
74-
case "ctrl+c", "q":
75-
if m.activeTab == 1 {
76-
m.activeTab = 0
77-
return m, nil
78-
}
79-
m.quitting = true
80-
return m, tea.Quit
71+
m.runnersView.Update(msg)
72+
m.logsView.Update(msg)
73+
m.configView.Update(msg)
74+
m.systemView.Update(msg)
75+
m.historyView.Update(msg)
8176

82-
case "tab":
83-
m.activeTab = (m.activeTab + 1) % len(m.tabs)
84-
return m.switchTab()
77+
return m, nil
78+
}
79+
80+
func (m model) handleKeyPress(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
81+
switch msg.String() {
82+
case "ctrl+c", "q":
83+
if m.activeTab == 1 {
84+
m.activeTab = 0
85+
return m, nil
86+
}
87+
m.quitting = true
88+
return m, tea.Quit
89+
90+
case "tab":
91+
m.activeTab = (m.activeTab + 1) % len(m.tabs)
92+
return m.switchTab()
93+
94+
case "shift+tab":
95+
m.activeTab = (m.activeTab - 1 + len(m.tabs)) % len(m.tabs)
96+
return m.switchTab()
8597

86-
case "shift+tab":
87-
m.activeTab = (m.activeTab - 1 + len(m.tabs)) % len(m.tabs)
98+
case "1", "2", "3", "4", "5":
99+
if idx := int(msg.String()[0] - '1'); idx < len(m.tabs) {
100+
m.activeTab = idx
88101
return m.switchTab()
102+
}
103+
return m, nil
89104

90-
case "1", "2", "3", "4", "5":
91-
if idx := int(msg.String()[0] - '1'); idx < len(m.tabs) {
92-
m.activeTab = idx
105+
case "enter":
106+
if m.activeTab == 0 {
107+
if runner := m.runnersView.GetSelectedRunner(); runner != nil {
108+
m.logsView.SetRunner(runner.Name)
109+
m.activeTab = 1
93110
return m.switchTab()
94111
}
95-
return m, nil
96-
97-
case "enter":
98-
if m.activeTab == 0 {
99-
if runner := m.runnersView.GetSelectedRunner(); runner != nil {
100-
m.logsView.SetRunner(runner.Name)
101-
m.activeTab = 1
102-
return m.switchTab()
103-
}
104-
}
105112
}
106113
}
107114

115+
// If key wasn't handled, pass it to the active view
116+
return m.updateActiveView(msg)
117+
}
118+
119+
func (m model) updateActiveView(msg tea.Msg) (tea.Model, tea.Cmd) {
120+
var cmd tea.Cmd
121+
108122
switch m.activeTab {
109123
case 0:
110-
updatedView, cmd := m.runnersView.Update(msg)
124+
var updatedView tea.Model
125+
updatedView, cmd = m.runnersView.Update(msg)
111126
m.runnersView = updatedView.(*ui.RunnersView)
112-
cmds = append(cmds, cmd)
113127
case 1:
114-
updatedView, cmd := m.logsView.Update(msg)
128+
var updatedView tea.Model
129+
updatedView, cmd = m.logsView.Update(msg)
115130
m.logsView = updatedView.(*ui.LogsView)
116-
cmds = append(cmds, cmd)
117131
case 2:
118-
updatedView, cmd := m.configView.Update(msg)
132+
var updatedView tea.Model
133+
updatedView, cmd = m.configView.Update(msg)
119134
m.configView = updatedView.(*ui.ConfigView)
120-
cmds = append(cmds, cmd)
121135
case 3:
122-
updatedView, cmd := m.systemView.Update(msg)
136+
var updatedView tea.Model
137+
updatedView, cmd = m.systemView.Update(msg)
123138
m.systemView = updatedView.(*ui.SystemView)
124-
cmds = append(cmds, cmd)
125139
case 4:
126-
updatedView, cmd := m.historyView.Update(msg)
140+
var updatedView tea.Model
141+
updatedView, cmd = m.historyView.Update(msg)
127142
m.historyView = updatedView.(*ui.HistoryView)
128-
cmds = append(cmds, cmd)
129143
}
130144

131-
return m, tea.Batch(cmds...)
145+
return m, cmd
132146
}
133147

134148
func (m model) switchTab() (model, tea.Cmd) {
@@ -214,7 +228,7 @@ func (m model) View() string {
214228
}
215229

216230
func (m model) renderTabBar() string {
217-
var tabs []string
231+
tabs := make([]string, 0, len(m.tabs))
218232

219233
for i, tab := range m.tabs {
220234
style := ui.TabStyle

cmd/gitlab-runner-tui/main_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ func (m *mockRunnerService) ListRunners() ([]runner.Runner, error) {
191191
return []runner.Runner{}, nil
192192
}
193193

194-
func (m *mockRunnerService) GetRunnerStatus(name string) (*runner.Runner, error) {
194+
func (m *mockRunnerService) GetRunnerStatus(_ string) (*runner.Runner, error) {
195195
return nil, nil
196196
}
197197

198-
func (m *mockRunnerService) GetRunnerLogs(name string, lines int) ([]string, error) {
198+
func (m *mockRunnerService) GetRunnerLogs(_ string, _ int) ([]string, error) {
199199
return []string{}, nil
200200
}
201201

202-
func (m *mockRunnerService) StreamRunnerLogs(name string) (io.ReadCloser, error) {
202+
func (m *mockRunnerService) StreamRunnerLogs(_ string) (io.ReadCloser, error) {
203203
return nil, nil
204204
}
205205

@@ -211,8 +211,8 @@ func (m *mockRunnerService) GetSystemStatus() (*runner.SystemStatus, error) {
211211
return &runner.SystemStatus{}, nil
212212
}
213213

214-
func (m *mockRunnerService) GetJobHistory(limit int) ([]runner.Job, error) {
214+
func (m *mockRunnerService) GetJobHistory(_ int) ([]runner.Job, error) {
215215
return []runner.Job{}, nil
216216
}
217217

218-
func (m *mockRunnerService) SetDebugMode(enabled bool) {}
218+
func (m *mockRunnerService) SetDebugMode(_ bool) {}

pkg/config/toml_parser.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ func (cm *TOMLConfigManager) UpdateLogLevel(level string) error {
125125
return nil
126126
}
127127

128-
func (cm *TOMLConfigManager) GetRunner(name string) (*runner.RunnerConfig, int) {
128+
func (cm *TOMLConfigManager) GetRunner(name string) (runner *runner.RunnerConfig, index int) {
129129
if cm.config == nil {
130130
return nil, -1
131131
}
132132

133-
for i, r := range cm.config.Runners {
134-
if r.Name == name {
133+
for i := range cm.config.Runners {
134+
if cm.config.Runners[i].Name == name {
135135
return &cm.config.Runners[i], i
136136
}
137137
}
@@ -207,7 +207,7 @@ func (cm *TOMLConfigManager) UpdateRunnerRequestConcurrency(name string, concurr
207207
return fmt.Errorf("request_concurrency must be non-negative")
208208
}
209209

210-
cm.config.Runners[idx].Request_concurrency = concurrency
210+
cm.config.Runners[idx].RequestConcurrency = concurrency
211211
return nil
212212
}
213213

@@ -221,7 +221,7 @@ func (cm *TOMLConfigManager) UpdateRunnerOutputLimit(name string, limit int) err
221221
return fmt.Errorf("output_limit must be non-negative")
222222
}
223223

224-
cm.config.Runners[idx].Output_limit = limit
224+
cm.config.Runners[idx].OutputLimit = limit
225225
return nil
226226
}
227227

@@ -234,7 +234,8 @@ func (cm *TOMLConfigManager) Validate() error {
234234
return fmt.Errorf("concurrent must be at least 1")
235235
}
236236

237-
for i, runner := range cm.config.Runners {
237+
for i := range cm.config.Runners {
238+
runner := &cm.config.Runners[i]
238239
if runner.Name == "" {
239240
return fmt.Errorf("runner %d has no name", i)
240241
}

pkg/config/toml_parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ log_level = "info"
2929
}
3030
defer os.Remove(tmpFile.Name())
3131

32-
if _, err := tmpFile.Write([]byte(testConfig)); err != nil {
32+
if _, err := tmpFile.WriteString(testConfig); err != nil {
3333
t.Fatal(err)
3434
}
3535
tmpFile.Close()
@@ -194,7 +194,7 @@ func TestTOMLConfigManager_Updates(t *testing.T) {
194194
return cm.UpdateRunnerRequestConcurrency("test-runner", 5)
195195
},
196196
validate: func() bool {
197-
return cm.config.Runners[0].Request_concurrency == 5
197+
return cm.config.Runners[0].RequestConcurrency == 5
198198
},
199199
},
200200
{
@@ -203,7 +203,7 @@ func TestTOMLConfigManager_Updates(t *testing.T) {
203203
return cm.UpdateRunnerOutputLimit("test-runner", 4096)
204204
},
205205
validate: func() bool {
206-
return cm.config.Runners[0].Output_limit == 4096
206+
return cm.config.Runners[0].OutputLimit == 4096
207207
},
208208
},
209209
}

0 commit comments

Comments
 (0)