Skip to content

feat: add YAML parse check to diagnose command #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions internal/commands/diagnose/configcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package diagnose

import (
"embed"
"fmt"
"os"

"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

type ConfigTestResult struct {
ConfigFile string
Passed bool
Error string
}

func validateYaml(yamlContent []byte) error {
m := make(map[string]any)
return yaml.Unmarshal(yamlContent, &m)
}

func checkConfig() (any, error) {
configFile := viper.ConfigFileUsed()
if configFile == "" {
return nil, fmt.Errorf("no config file defined")
}
contents, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
if err = validateYaml(contents); err != nil {
return ConfigTestResult{
configFile,
false,
err.Error(),
}, nil
}
return ConfigTestResult{
ConfigFile: configFile,
Passed: true,
}, nil
}

const configcheckTemplate = "configcheck.tmpl"

var (
//go:embed configcheck.tmpl
configcheckTemplateFS embed.FS
)

func configDiagnostic() Diagnostic {
return Diagnostic{
check: checkConfig,
checkName: "Config Check",
templateName: configcheckTemplate,
templateFS: configcheckTemplateFS,
}
}
6 changes: 6 additions & 0 deletions internal/commands/diagnose/configcheck.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Running check on config file {{ .ConfigFile }}
{{- if .Passed }}
Config file is valid YAML.
{{- else }}
⚠️ Config file parse failed with error {{ .Error }}
{{- end }}
59 changes: 59 additions & 0 deletions internal/commands/diagnose/configcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package diagnose

import (
"testing"

"github.com/stretchr/testify/assert"
)

const testConfig = `
# Observe data token
token: "some token"

# Target Observe collection url
observe_url: "localhost"

# Debug mode - Sets agent log level to debug
debug: false

# collect metrics and logs pertaining to the agent itself
self_monitoring:
enabled: true

# collect metrics and logs about the host system
host_monitoring:
enabled: true
# collect logs of all running processes from the host system
logs:
enabled: true
metrics:
# collect metrics about the host system
host:
enabled: true
# collect metrics about the processes running on the host system
process:
enabled: false
`

var (
validCases = []string{
testConfig,
"key:\n spaceIndented: \"value\"",
}
invalidCases = []string{
"key:\n\ttabIndented: \"value\"",
"key:\n twoSpaces: true\n threeSpaces: true",
"\tstartsWithTab: true",
}
)

func Test_validateYaml(t *testing.T) {
for _, tc := range validCases {
err := validateYaml([]byte(tc))
assert.NoError(t, err)
}
for _, tc := range invalidCases {
err := validateYaml([]byte(tc))
assert.Error(t, err)
}
}
1 change: 1 addition & 0 deletions internal/commands/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Diagnostic struct {

var diagnostics = []Diagnostic{
authDiagnostic(),
configDiagnostic(),
}

// diagnoseCmd represents the diagnose command
Expand Down
Loading