Skip to content

Commit 5c26cde

Browse files
committed
fix lint / test
1 parent be1b054 commit 5c26cde

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

pkg/jsonutils/jsonutils.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func formatTable(data any) (string, error) {
111111
return formatGenericMap(mapVal)
112112
}
113113

114-
// formatToolsList formats a list of tools as a table
114+
// formatToolsList formats a list of tools as a table.
115115
func formatToolsList(tools any) (string, error) {
116116
toolsSlice, ok := tools.([]any)
117117
if !ok {
@@ -129,9 +129,9 @@ func formatToolsList(tools any) (string, error) {
129129
fmt.Fprintln(w, "----\t-----------")
130130

131131
termWidth := getTermWidth()
132-
nameColWidth := 20 // Default name column width
132+
nameColWidth := 20 // Default name column width
133133
descColWidth := termWidth - nameColWidth - 5 // Leave some margin
134-
134+
135135
if descColWidth < 10 {
136136
descColWidth = 40 // Minimum width if terminal is too narrow
137137
}
@@ -147,20 +147,20 @@ func formatToolsList(tools any) (string, error) {
147147

148148
// Handle multiline description
149149
lines := wrapText(desc, descColWidth)
150-
150+
151151
if len(lines) == 0 {
152152
fmt.Fprintf(w, "%s\t\n", name)
153153
continue
154154
}
155-
155+
156156
// First line with name
157157
fmt.Fprintf(w, "%s\t%s\n", name, lines[0])
158-
158+
159159
// Remaining lines with empty name column
160160
for _, line := range lines[1:] {
161161
fmt.Fprintf(w, "\t%s\n", line)
162162
}
163-
163+
164164
// Add a blank line between entries
165165
if len(lines) > 1 {
166166
fmt.Fprintln(w, "\t")
@@ -171,7 +171,7 @@ func formatToolsList(tools any) (string, error) {
171171
return buf.String(), nil
172172
}
173173

174-
// getTermWidth returns the terminal width or a default value if detection fails
174+
// getTermWidth returns the terminal width or a default value if detection fails.
175175
func getTermWidth() int {
176176
width, _, err := term.GetSize(int(os.Stdout.Fd()))
177177
if err != nil || width <= 0 {
@@ -180,42 +180,42 @@ func getTermWidth() int {
180180
return width
181181
}
182182

183-
// wrapText wraps text to fit within a specified width
183+
// wrapText wraps text to fit within a specified width.
184184
func wrapText(text string, width int) []string {
185185
if text == "" {
186186
return []string{}
187187
}
188-
188+
189189
words := strings.Fields(text)
190190
if len(words) == 0 {
191191
return []string{}
192192
}
193-
193+
194194
var lines []string
195195
var currentLine string
196-
196+
197197
for _, word := range words {
198-
// Check if adding this word would exceed the width
199-
if len(currentLine)+len(word)+1 > width && len(currentLine) > 0 {
198+
switch {
199+
case len(currentLine) == 0:
200+
currentLine = word
201+
case len(currentLine)+len(word)+1 > width:
200202
// Add current line to lines and start a new line
201203
lines = append(lines, currentLine)
202204
currentLine = word
203-
} else if len(currentLine) == 0 {
204-
currentLine = word
205-
} else {
205+
default:
206206
currentLine += " " + word
207207
}
208208
}
209-
209+
210210
// Add the last line
211211
if len(currentLine) > 0 {
212212
lines = append(lines, currentLine)
213213
}
214-
214+
215215
return lines
216216
}
217217

218-
// formatResourcesList formats a list of resources as a table
218+
// formatResourcesList formats a list of resources as a table.
219219
func formatResourcesList(resources any) (string, error) {
220220
resourcesSlice, ok := resources.([]any)
221221
if !ok {
@@ -241,7 +241,7 @@ func formatResourcesList(resources any) (string, error) {
241241
name, _ := resource["name"].(string)
242242
resType, _ := resource["type"].(string)
243243
uri, _ := resource["uri"].(string)
244-
244+
245245
// Use the entire URI instead of truncating
246246
fmt.Fprintf(w, "%s\t%s\t%s\n", name, resType, uri)
247247
}
@@ -250,7 +250,7 @@ func formatResourcesList(resources any) (string, error) {
250250
return buf.String(), nil
251251
}
252252

253-
// formatPromptsList formats a list of prompts as a table
253+
// formatPromptsList formats a list of prompts as a table.
254254
func formatPromptsList(prompts any) (string, error) {
255255
promptsSlice, ok := prompts.([]any)
256256
if !ok {
@@ -268,9 +268,9 @@ func formatPromptsList(prompts any) (string, error) {
268268
fmt.Fprintln(w, "----\t-----------")
269269

270270
termWidth := getTermWidth()
271-
nameColWidth := 20 // Default name column width
271+
nameColWidth := 20 // Default name column width
272272
descColWidth := termWidth - nameColWidth - 5 // Leave some margin
273-
273+
274274
if descColWidth < 10 {
275275
descColWidth = 40 // Minimum width if terminal is too narrow
276276
}
@@ -286,20 +286,20 @@ func formatPromptsList(prompts any) (string, error) {
286286

287287
// Handle multiline description
288288
lines := wrapText(desc, descColWidth)
289-
289+
290290
if len(lines) == 0 {
291291
fmt.Fprintf(w, "%s\t\n", name)
292292
continue
293293
}
294-
294+
295295
// First line with name
296296
fmt.Fprintf(w, "%s\t%s\n", name, lines[0])
297-
297+
298298
// Remaining lines with empty name column
299299
for _, line := range lines[1:] {
300300
fmt.Fprintf(w, "\t%s\n", line)
301301
}
302-
302+
303303
// Add a blank line between entries
304304
if len(lines) > 1 {
305305
fmt.Fprintln(w, "\t")

pkg/jsonutils/jsonutils_test.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package jsonutils
22

33
import (
4+
"os"
45
"strings"
56
"testing"
6-
"os"
77
)
88

9-
// TestWrapText tests the text wrapping functionality
9+
// TestWrapText tests the text wrapping functionality.
1010
func TestWrapText(t *testing.T) {
1111
testCases := []struct {
1212
name string
13-
text string
1413
width int
14+
text string
1515
expected []string
1616
}{
1717
{
@@ -43,11 +43,11 @@ func TestWrapText(t *testing.T) {
4343
for _, tc := range testCases {
4444
t.Run(tc.name, func(t *testing.T) {
4545
result := wrapText(tc.text, tc.width)
46-
46+
4747
if len(result) != len(tc.expected) {
4848
t.Fatalf("Expected %d lines, got %d", len(tc.expected), len(result))
4949
}
50-
50+
5151
for i, line := range result {
5252
if line != tc.expected[i] {
5353
t.Errorf("Line %d: expected '%s', got '%s'", i, tc.expected[i], line)
@@ -57,25 +57,27 @@ func TestWrapText(t *testing.T) {
5757
}
5858
}
5959

60-
// TestGetTermWidth tests the terminal width detection
60+
// TestGetTermWidth tests the terminal width detection.
6161
func TestGetTermWidth(t *testing.T) {
6262
// Save original stdout and restore it after the test
6363
origStdout := os.Stdout
6464
defer func() { os.Stdout = origStdout }()
65-
65+
6666
// Non-terminal case should return default width
6767
r, w, _ := os.Pipe()
6868
os.Stdout = w
69-
69+
7070
width := getTermWidth()
71-
71+
7272
// Close pipe
73-
w.Close()
73+
if err := w.Close(); err != nil {
74+
t.Errorf("Error closing pipe: %v", err)
75+
}
7476
os.Stdout = origStdout
75-
77+
7678
// Read and discard pipe content
7779
_, _ = r.Read(make([]byte, 1024))
78-
80+
7981
// Should return default width for non-terminal
8082
if width != 80 {
8183
t.Errorf("Expected default width 80 for non-terminal, got %d", width)
@@ -190,14 +192,14 @@ func TestParseFormat(t *testing.T) {
190192
}
191193
}
192194

193-
// TestToolsListFormatting tests the table formatting for tools list
195+
// TestToolsListFormatting tests the table formatting for tools list.
194196
func TestToolsListFormatting(t *testing.T) {
195-
tools := []map[string]any{
196-
{
197+
tools := []any{
198+
map[string]any{
197199
"name": "tool1",
198200
"description": "This is a short description",
199201
},
200-
{
202+
map[string]any{
201203
"name": "tool2",
202204
"description": "This is a longer description that should wrap across multiple lines in the table output when displayed to the user",
203205
},

0 commit comments

Comments
 (0)