Add the initial rough skeleton for a config tool.
This commit is contained in:
parent
8d2b15a7c8
commit
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 {
|
if err != nil {
|
||||||
return nil, errors.New("failed to create config directory at " + directory)
|
return nil, errors.New("failed to create config directory at " + directory)
|
||||||
} else {
|
} 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()))
|
paths = append(paths, filepath.Join(directory, file.Name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(paths) == 0 {
|
||||||
|
return nil, &EmptyConfigError{directory}
|
||||||
|
}
|
||||||
|
|
||||||
return paths, nil
|
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