Skip to content

Commit bfd7622

Browse files
committed
fix: resolve CI failures in build-test and lint workflows
- Fix hardcoded /Users/bowen path in main_test.go; use runtime.Caller to resolve cmd/flashduty directory portably across CI environments - Fix fmt import missing from main_test.go after prior refactor - Remove unnecessary fmt.Sprintf (staticcheck S1039) - Add _, _ = prefix to all unchecked fmt.Fprintf/Fprintln calls (errcheck) across 10 command files - Suppress footer lines (Total:, Showing N) in --json mode to produce valid JSON output
1 parent 7be2c73 commit bfd7622

11 files changed

Lines changed: 56 additions & 45 deletions

File tree

cmd/flashduty/main_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"path/filepath"
9+
"runtime"
810
"strings"
911
"testing"
1012
)
@@ -37,9 +39,9 @@ func TestErrorFormatToStderr(t *testing.T) {
3739

3840
// We use a different strategy: build the binary, run it with a bad
3941
// subcommand, and inspect stderr.
40-
binPath := t.TempDir() + "/flashduty-test"
42+
binPath := filepath.Join(t.TempDir(), "flashduty-test")
4143
build := exec.Command("go", "build", "-o", binPath, ".")
42-
build.Dir = "/Users/bowen/go/src/github.com/flashcatcloud/flashduty-cli/cmd/flashduty"
44+
build.Dir = testMainDir(t)
4345
if out, err := build.CombinedOutput(); err != nil {
4446
t.Fatalf("[#77] failed to build test binary: %v\n%s", err, out)
4547
}
@@ -81,15 +83,13 @@ func TestErrorFormatToStderr(t *testing.T) {
8183
// Test 78: SetVersionInfo before Execute -- version/commit/date set by main
8284
// are reflected in the `version` subcommand output.
8385
func TestSetVersionInfoBeforeExecute(t *testing.T) {
84-
binPath := t.TempDir() + "/flashduty-test"
86+
binPath := filepath.Join(t.TempDir(), "flashduty-test")
8587

8688
// Build with custom ldflags to inject known version info, just like the
8789
// real release build does.
88-
ldflags := fmt.Sprintf(
89-
"-X main.version=1.2.3-test -X main.commit=abc1234 -X main.date=2026-04-13",
90-
)
90+
ldflags := "-X main.version=1.2.3-test -X main.commit=abc1234 -X main.date=2026-04-13"
9191
build := exec.Command("go", "build", "-ldflags", ldflags, "-o", binPath, ".")
92-
build.Dir = "/Users/bowen/go/src/github.com/flashcatcloud/flashduty-cli/cmd/flashduty"
92+
build.Dir = testMainDir(t)
9393
if out, err := build.CombinedOutput(); err != nil {
9494
t.Fatalf("[#78] failed to build test binary: %v\n%s", err, out)
9595
}
@@ -114,3 +114,14 @@ func TestSetVersionInfoBeforeExecute(t *testing.T) {
114114
}
115115
}
116116
}
117+
118+
// testMainDir returns the directory of cmd/flashduty/main.go relative to this
119+
// test file, so it works both locally and in CI.
120+
func testMainDir(t *testing.T) string {
121+
t.Helper()
122+
_, filename, _, ok := runtime.Caller(0)
123+
if !ok {
124+
t.Fatal("could not determine test file path")
125+
}
126+
return filepath.Dir(filename)
127+
}

internal/cli/change.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func newChangeListCmd() *cobra.Command {
6767
return err
6868
}
6969
if !flagJSON {
70-
fmt.Fprintf(cmd.OutOrStdout(), "Showing %d results (page %d, total %d).\n", len(result.Changes), page, result.Total)
70+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Showing %d results (page %d, total %d).\n", len(result.Changes), page, result.Total)
7171
}
7272
return nil
7373
},

internal/cli/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func newChannelListCmd() *cobra.Command {
4949
return err
5050
}
5151
if !flagJSON {
52-
fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
52+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
5353
}
5454
return nil
5555
},

internal/cli/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func newConfigShowCmd() *cobra.Command {
3232
displayKey = "(not set)"
3333
}
3434

35-
fmt.Fprintf(cmd.OutOrStdout(), "app_key: %s %s\n", displayKey, config.ConfigSource("app_key"))
36-
fmt.Fprintf(cmd.OutOrStdout(), "base_url: %s %s\n", cfg.BaseURL, config.ConfigSource("base_url"))
35+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "app_key: %s %s\n", displayKey, config.ConfigSource("app_key"))
36+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "base_url: %s %s\n", cfg.BaseURL, config.ConfigSource("base_url"))
3737
return nil
3838
},
3939
}
@@ -66,7 +66,7 @@ func newConfigSetCmd() *cobra.Command {
6666
return err
6767
}
6868

69-
fmt.Fprintf(cmd.OutOrStdout(), "Set %s successfully.\n", key)
69+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Set %s successfully.\n", key)
7070
return nil
7171
},
7272
}

internal/cli/escalation_rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func resolveChannelID(cmd *cobra.Command, client flashdutyClient, name string) (
8383
case 1:
8484
return result.Channels[0].ChannelID, nil
8585
default:
86-
fmt.Fprintln(cmd.OutOrStdout(), "Multiple channels match:")
86+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Multiple channels match:")
8787
for _, ch := range result.Channels {
88-
fmt.Fprintf(cmd.OutOrStdout(), " %d %s\n", ch.ChannelID, ch.ChannelName)
88+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " %d %s\n", ch.ChannelID, ch.ChannelName)
8989
}
9090
return 0, fmt.Errorf("multiple channels match %q, use --channel <id> to specify", name)
9191
}

internal/cli/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func newFieldListCmd() *cobra.Command {
5252
return err
5353
}
5454
if !flagJSON {
55-
fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
55+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
5656
}
5757
return nil
5858
},

internal/cli/incident.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func newIncidentListCmd() *cobra.Command {
8686
return err
8787
}
8888
if !flagJSON {
89-
fmt.Fprintf(cmd.OutOrStdout(), "Showing %d results (page %d, total %d).\n", len(result.Incidents), page, result.Total)
89+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Showing %d results (page %d, total %d).\n", len(result.Incidents), page, result.Total)
9090
}
9191
return nil
9292
},
@@ -155,18 +155,18 @@ func printIncidentDetail(w io.Writer, inc flashduty.EnrichedIncident) {
155155
fields = append(fields, fmt.Sprintf("%s=%v", k, v))
156156
}
157157

158-
fmt.Fprintf(w, "ID: %s\n", inc.IncidentID)
159-
fmt.Fprintf(w, "Title: %s\n", inc.Title)
160-
fmt.Fprintf(w, "Severity: %s\n", inc.Severity)
161-
fmt.Fprintf(w, "Progress: %s\n", inc.Progress)
162-
fmt.Fprintf(w, "Channel: %s\n", inc.ChannelName)
163-
fmt.Fprintf(w, "Created: %s\n", output.FormatTime(inc.StartTime))
164-
fmt.Fprintf(w, "Creator: %s (%s)\n", inc.CreatorName, inc.CreatorEmail)
165-
fmt.Fprintf(w, "Responders: %s\n", orDash(strings.Join(responders, ", ")))
166-
fmt.Fprintf(w, "Description: %s\n", orDash(inc.Description))
167-
fmt.Fprintf(w, "Labels: %s\n", orDash(strings.Join(labels, ", ")))
168-
fmt.Fprintf(w, "Custom Fields: %s\n", orDash(strings.Join(fields, ", ")))
169-
fmt.Fprintf(w, "Alerts: %d total\n", inc.AlertsTotal)
158+
_, _ = fmt.Fprintf(w, "ID: %s\n", inc.IncidentID)
159+
_, _ = fmt.Fprintf(w, "Title: %s\n", inc.Title)
160+
_, _ = fmt.Fprintf(w, "Severity: %s\n", inc.Severity)
161+
_, _ = fmt.Fprintf(w, "Progress: %s\n", inc.Progress)
162+
_, _ = fmt.Fprintf(w, "Channel: %s\n", inc.ChannelName)
163+
_, _ = fmt.Fprintf(w, "Created: %s\n", output.FormatTime(inc.StartTime))
164+
_, _ = fmt.Fprintf(w, "Creator: %s (%s)\n", inc.CreatorName, inc.CreatorEmail)
165+
_, _ = fmt.Fprintf(w, "Responders: %s\n", orDash(strings.Join(responders, ", ")))
166+
_, _ = fmt.Fprintf(w, "Description: %s\n", orDash(inc.Description))
167+
_, _ = fmt.Fprintf(w, "Labels: %s\n", orDash(strings.Join(labels, ", ")))
168+
_, _ = fmt.Fprintf(w, "Custom Fields: %s\n", orDash(strings.Join(fields, ", ")))
169+
_, _ = fmt.Fprintf(w, "Alerts: %d total\n", inc.AlertsTotal)
170170
}
171171

172172
func orDash(s string) string {
@@ -353,7 +353,7 @@ func newIncidentTimelineCmd() *cobra.Command {
353353
}
354354

355355
if len(results) == 0 || len(results[0].Timeline) == 0 {
356-
fmt.Fprintln(cmd.OutOrStdout(), "No timeline events.")
356+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "No timeline events.")
357357
return nil
358358
}
359359

@@ -394,7 +394,7 @@ func newIncidentAlertsCmd() *cobra.Command {
394394
}
395395

396396
if len(results) == 0 || len(results[0].Alerts) == 0 {
397-
fmt.Fprintln(cmd.OutOrStdout(), "No alerts.")
397+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "No alerts.")
398398
return nil
399399
}
400400

@@ -411,7 +411,7 @@ func newIncidentAlertsCmd() *cobra.Command {
411411
return err
412412
}
413413
if !flagJSON {
414-
fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", results[0].Total)
414+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", results[0].Total)
415415
}
416416
return nil
417417
},
@@ -440,7 +440,7 @@ func newIncidentSimilarCmd() *cobra.Command {
440440
}
441441

442442
if len(result.Incidents) == 0 {
443-
fmt.Fprintln(cmd.OutOrStdout(), "No similar incidents found.")
443+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "No similar incidents found.")
444444
return nil
445445
}
446446

internal/cli/member.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func newMemberListCmd() *cobra.Command {
6767
if flagJSON {
6868
return newPrinter(cmd.OutOrStdout()).Print([]struct{}{}, nil)
6969
}
70-
fmt.Fprintln(cmd.OutOrStdout(), "No members found.")
70+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "No members found.")
7171
return nil
7272
}
7373

7474
if !flagJSON {
75-
fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
75+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
7676
}
7777
return nil
7878
},

internal/cli/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ func writeResult(w io.Writer, message string) {
135135
}
136136
if flagJSON {
137137
out, _ := json.MarshalIndent(map[string]string{"message": message}, "", " ")
138-
fmt.Fprintln(w, string(out))
138+
_, _ = fmt.Fprintln(w, string(out))
139139
} else {
140-
fmt.Fprintln(w, message)
140+
_, _ = fmt.Fprintln(w, message)
141141
}
142142
}
143143

internal/cli/team.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newTeamListCmd() *cobra.Command {
5858
return err
5959
}
6060
if !flagJSON {
61-
fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
61+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Total: %d\n", result.Total)
6262
}
6363
return nil
6464
},

0 commit comments

Comments
 (0)