Skip to content

Commit a3839cb

Browse files
feat: refactor diagnose command to be more extensible
1 parent b30e016 commit a3839cb

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

internal/commands/diagnose/diagnose.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,37 @@ var (
2222
authcheckTemplateFS embed.FS
2323
)
2424

25+
var diagnostics = []struct {
26+
check func() (any, error)
27+
template string
28+
templateFS embed.FS
29+
}{
30+
{
31+
authCheck,
32+
authcheckTemplate,
33+
authcheckTemplateFS,
34+
},
35+
}
36+
2537
// diagnoseCmd represents the diagnose command
2638
var diagnoseCmd = &cobra.Command{
2739
Use: "diagnose",
2840
Short: "Run diagnostic checks.",
2941
Long: `This command runs diagnostic checks for various settings and configurations
3042
to attempt to identify issues that could cause the agent to function improperly.`,
31-
Run: func(cmd *cobra.Command, args []string) {
43+
RunE: func(cmd *cobra.Command, args []string) error {
3244
fmt.Print("Running diagnosis checks...\n\n")
33-
runNetworkCheck()
45+
for _, diagnostic := range diagnostics {
46+
data, err := diagnostic.check()
47+
if err != nil {
48+
return err
49+
}
50+
t := template.Must(template.New(diagnostic.template).ParseFS(diagnostic.templateFS, diagnostic.template))
51+
if err := t.ExecuteTemplate(os.Stdout, authcheckTemplate, data); err != nil {
52+
return err
53+
}
54+
}
55+
return nil
3456
},
3557
}
3658

@@ -48,12 +70,8 @@ func init() {
4870
// diagnoseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4971
}
5072

51-
func runNetworkCheck() error {
73+
func authCheck() (any, error) {
5274
collector_url := viper.GetString("observe_url")
5375
authTestResponse := makeAuthTestRequest(collector_url)
54-
t := template.Must(template.New(authcheckTemplate).ParseFS(authcheckTemplateFS, authcheckTemplate))
55-
if err := t.ExecuteTemplate(os.Stdout, authcheckTemplate, authTestResponse); err != nil {
56-
return err
57-
}
58-
return nil
76+
return authTestResponse, nil
5977
}

internal/commands/diagnose/networkchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func makeTestRequest(URL string, headers map[string]string) NetworkTestResult {
4747
if err != nil {
4848
return NetworkTestResult{
4949
Passed: false,
50-
Error: "failed to parse response body",
50+
Error: fmt.Sprintf("failed to parse response body: %s", err.Error()),
5151
ResponseCode: resp.StatusCode,
5252
URL: URL,
5353
}

0 commit comments

Comments
 (0)