feat(config): config at XDG dir
This commit is contained in:
parent
0c0de52a6e
commit
6f39d532ce
4 changed files with 77 additions and 5 deletions
67
cmd/config.go
Normal file
67
cmd/config.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
gap "github.com/muesli/go-app-paths"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DefaultDB string `toml:"default_db"`
|
||||
}
|
||||
|
||||
var (
|
||||
config Config
|
||||
configErr error
|
||||
)
|
||||
|
||||
func init() {
|
||||
config, configErr = loadConfig()
|
||||
}
|
||||
|
||||
func defaultConfig() Config {
|
||||
return Config{
|
||||
DefaultDB: "default",
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() (Config, error) {
|
||||
cfg := defaultConfig()
|
||||
|
||||
path, err := configPath()
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return cfg, nil
|
||||
}
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
||||
return cfg, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
|
||||
if cfg.DefaultDB == "" {
|
||||
cfg.DefaultDB = defaultConfig().DefaultDB
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func configPath() (string, error) {
|
||||
if override := os.Getenv("PDA_CONFIG"); override != "" {
|
||||
return override, nil
|
||||
}
|
||||
scope := gap.NewVendorScope(gap.User, "pda", "config")
|
||||
dir, err := scope.ConfigPath("")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(dir, "config.toml"), nil
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ type KeySpec struct {
|
|||
}
|
||||
|
||||
// ParseKey parses "KEY[@DB]" into a normalized KeySpec.
|
||||
// When defaults is true, a missing DB defaults to "default".
|
||||
// When defaults is true, a missing DB defaults to the configured default.
|
||||
func ParseKey(raw string, defaults bool) (KeySpec, error) {
|
||||
parts := strings.Split(raw, "@")
|
||||
if len(parts) > 2 {
|
||||
|
|
@ -34,7 +34,7 @@ func ParseKey(raw string, defaults bool) (KeySpec, error) {
|
|||
key := strings.ToLower(rawKey)
|
||||
db := strings.ToLower(rawDB)
|
||||
if db == "" && defaults {
|
||||
db = "default"
|
||||
db = config.DefaultDB
|
||||
}
|
||||
|
||||
return KeySpec{
|
||||
|
|
@ -57,7 +57,7 @@ func (k KeySpec) Full() string {
|
|||
// Display returns the normalized key reference
|
||||
// but omits the default database if none was set manually
|
||||
func (k KeySpec) Display() string {
|
||||
if k.DB == "" || k.DB == "default" {
|
||||
if k.DB == "" || k.DB == config.DefaultDB {
|
||||
return k.Key
|
||||
}
|
||||
return fmt.Sprintf("%s@%s", k.Key, k.DB)
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ THE SOFTWARE.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -42,6 +43,10 @@ var rootCmd = &cobra.Command{
|
|||
`}
|
||||
|
||||
func Execute() {
|
||||
if configErr != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to load config:", configErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ func (s *Store) parseDB(v string, defaults bool) (string, error) {
|
|||
}
|
||||
if db == "" {
|
||||
if defaults {
|
||||
return "default", nil
|
||||
return config.DefaultDB, nil
|
||||
}
|
||||
return "", fmt.Errorf("cannot parse db: bad db format, use DB or @DB")
|
||||
}
|
||||
|
|
@ -182,7 +182,7 @@ func (s *Store) parseDB(v string, defaults bool) (string, error) {
|
|||
|
||||
func (s *Store) open(name string) (*badger.DB, error) {
|
||||
if name == "" {
|
||||
name = "default"
|
||||
name = config.DefaultDB
|
||||
}
|
||||
path, err := s.path(name)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue