Skip to content
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

Fail on no config passed #49

Merged
merged 5 commits into from
Dec 31, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions config.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
telemetry:
logging:
level: debug # debug, info, warn, error, fatal
encoding: console

#api:
# http:
# ...
1 change: 1 addition & 0 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewCLI() *cobra.Command {
}

cli.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
_ = cli.MarkPersistentFlagRequired("config")

return cli
}
2 changes: 1 addition & 1 deletion pkg/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/provider_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 6 additions & 0 deletions pkg/config/testdata/provider.broken.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"telemetry": {
"logging": {
"level": "debug",
"encoding": "console"

Loading