Skip to content
Closed
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
3 changes: 2 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func bindCobraAndViper(rootCommand *cobra.Command) error {
if err := v.ReadInConfig(); err != nil {
// If the configuration file was not found, it's all good, we ignore
// the failure and proceed with the default settings
if f := (&viper.ConfigFileNotFoundError{}); errors.As(err, &f) {
var cfErr viper.ConfigFileNotFoundError
if !errors.As(err, &cfErr) {
return fmt.Errorf("unable to read configuration file: %w", err)
}
}
Expand Down
28 changes: 28 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"os"
"testing"

"github.com/spf13/cobra"
)

func TestBindCobraAndViper_NoConfigFile(t *testing.T) {
tempDir := t.TempDir()

prev, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
if err := os.Chdir(tempDir); err != nil {
t.Fatalf("chdir to temp dir: %v", err)
}
defer os.Chdir(prev)

cmd := &cobra.Command{Use: "test"}
cmd.Flags().Bool("foo", false, "")

if err := bindCobraAndViper(cmd); err != nil {
t.Fatalf("bindCobraAndViper returned error: %v", err)
}
}