diff --git a/Makefile b/Makefile index 63f59f94..308a7b52 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ vuln: install-checkers ## Check for vulnerabilities @$(CHECKER_BIN)/gosec -quiet -exclude=G104 ./... run: ## Run Glide - @go run -ldflags $(LDFLAGS_COMMON) main.go + @go run -ldflags $(LDFLAGS_COMMON) main.go -c ./config.dev.yaml build: ## Build Glide @go build -ldflags $(LDFLAGS_COMMON) -o ./dist/glide diff --git a/config.dev.yaml b/config.dev.yaml new file mode 100644 index 00000000..4dcc554c --- /dev/null +++ b/config.dev.yaml @@ -0,0 +1,8 @@ +telemetry: + logging: + level: debug # debug, info, warn, error, fatal + encoding: console + +#api: +# http: +# ... diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index 6e1b72a3..885ec2e3 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -34,6 +34,7 @@ func NewCLI() *cobra.Command { } cli.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file") + _ = cli.MarkPersistentFlagRequired("config") return cli } diff --git a/pkg/config/provider.go b/pkg/config/provider.go index 6f1cfd0b..03d4f7f9 100644 --- a/pkg/config/provider.go +++ b/pkg/config/provider.go @@ -25,7 +25,7 @@ func NewProvider() *Provider { func (p *Provider) Load(configPath string) (*Provider, error) { content, err := os.ReadFile(filepath.Clean(configPath)) if err != nil { - return p, fmt.Errorf("unable to read the file %v: %w", configPath, err) + return p, fmt.Errorf("unable to read config file %v: %w", configPath, err) } // process raw config diff --git a/pkg/config/provider_test.go b/pkg/config/provider_test.go new file mode 100644 index 00000000..b6b024d7 --- /dev/null +++ b/pkg/config/provider_test.go @@ -0,0 +1,21 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConfigProvider_NonExistingConfigFile(t *testing.T) { + _, err := NewProvider().Load("./testdata/doesntexist.yaml") + + require.Error(t, err) + require.ErrorContains(t, err, "no such file or directory") +} + +func TestConfigProvider_NonYAMLConfigFile(t *testing.T) { + _, err := NewProvider().Load("./testdata/provider.broken.yaml") + + require.Error(t, err) + require.ErrorContains(t, err, "unable to parse config file") +} diff --git a/pkg/config/testdata/provider.broken.yaml b/pkg/config/testdata/provider.broken.yaml new file mode 100644 index 00000000..e3a0043d --- /dev/null +++ b/pkg/config/testdata/provider.broken.yaml @@ -0,0 +1,6 @@ +{ + "telemetry": { + "logging": { + "level": "debug", + "encoding": "console" +