mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Initial implementation of config file
This commit is contained in:
parent
1db4349eb0
commit
2baae37f5f
9 changed files with 672 additions and 19 deletions
28
internal/config.go
Normal file
28
internal/config.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package magellan
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bikeshack/magellan/internal/util"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func LoadConfig(path string) error {
|
||||
dir, filename, ext := util.SplitPathForViper(path)
|
||||
// fmt.Printf("dir: %s\nfilename: %s\nextension: %s\n", dir, filename, ext)
|
||||
viper.AddConfigPath(dir)
|
||||
viper.SetConfigName(filename)
|
||||
viper.SetConfigType(ext)
|
||||
// ...no search paths set intentionally, so config has to be set explicitly
|
||||
// ...also, the config file will not save anything
|
||||
// ...and finally, parameters passed to CLI have precedence over config values
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
return fmt.Errorf("config file not found: %w", err)
|
||||
} else {
|
||||
return fmt.Errorf("could not load config file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -78,4 +80,10 @@ func MakeOutputDirectory(path string) (string, error) {
|
|||
return final, fmt.Errorf("could not make directory: %v", err)
|
||||
}
|
||||
return final, nil
|
||||
}
|
||||
}
|
||||
|
||||
func SplitPathForViper(path string) (string, string, string) {
|
||||
filename := filepath.Base(path)
|
||||
ext := filepath.Ext(filename)
|
||||
return filepath.Dir(path), strings.TrimSuffix(filename, ext), strings.TrimPrefix(ext, ".")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue