Compare commits
1 commit
main
...
configurat
Author | SHA1 | Date | |
---|---|---|---|
1ac689cd04 |
3 changed files with 49 additions and 1 deletions
33
cmd/joyful-config/main.go
Normal file
33
cmd/joyful-config/main.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.annabunches.net/annabunches/joyful/internal/configparser"
|
||||
"git.annabunches.net/annabunches/joyful/internal/logger"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func getConfigDir(dir string) string {
|
||||
configDir := strings.ReplaceAll(dir, "~", "${HOME}")
|
||||
return os.ExpandEnv(configDir)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var configDir string
|
||||
flag.StringVarP(&configDir, "config", "c", "~/.config/joyful", "Directory to read configuration from.")
|
||||
flag.Parse()
|
||||
configDir = getConfigDir(configDir)
|
||||
|
||||
config, err := configparser.ParseConfig(configDir)
|
||||
switch err.(type) {
|
||||
case *configparser.EmptyConfigError:
|
||||
config = &configparser.Config{}
|
||||
default:
|
||||
logger.FatalIfError(err, "Fatal error reading config")
|
||||
}
|
||||
|
||||
fmt.Printf("Config: %v\n", config)
|
||||
}
|
|
@ -50,7 +50,7 @@ func getConfigFilePaths(directory string) ([]string, error) {
|
|||
if err != nil {
|
||||
return nil, errors.New("failed to create config directory at " + directory)
|
||||
} else {
|
||||
return nil, errors.New("no config files found at " + directory)
|
||||
return nil, &EmptyConfigError{directory}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,5 +63,9 @@ func getConfigFilePaths(directory string) ([]string, error) {
|
|||
paths = append(paths, filepath.Join(directory, file.Name()))
|
||||
}
|
||||
|
||||
if len(paths) == 0 {
|
||||
return nil, &EmptyConfigError{directory}
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
|
11
internal/configparser/errors.go
Normal file
11
internal/configparser/errors.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package configparser
|
||||
|
||||
import "fmt"
|
||||
|
||||
type EmptyConfigError struct {
|
||||
directory string
|
||||
}
|
||||
|
||||
func (e *EmptyConfigError) Error() string {
|
||||
return fmt.Sprintf("no config files found at %s", e.directory)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue