feat(config): warn_on_delete, warn_on_overwrite, display_art toggles in config

This commit is contained in:
Lewis Wynne 2025-12-18 13:30:38 +00:00
parent 62c770dbf3
commit 6efa9863fe
5 changed files with 43 additions and 19 deletions

View file

@ -10,11 +10,23 @@ import (
)
type Config struct {
DefaultDB string `toml:"default_db"`
DefaultDB string `toml:"default_db"`
DisplayArt bool `toml:"display_art"`
WarnOnDelete bool `toml:"warn_on_delete"`
WarnOnOverwrite bool `toml:"warn_on_overwrite"`
}
var (
config Config
config Config
asciiArt string = `
(c) 2025 Lewis Wynne
`
configErr error
)
@ -24,7 +36,10 @@ func init() {
func defaultConfig() Config {
return Config{
DefaultDB: "default",
DefaultDB: "default",
DisplayArt: false,
WarnOnOverwrite: true,
WarnOnDelete: true,
}
}
@ -43,14 +58,27 @@ func loadConfig() (Config, error) {
return cfg, err
}
if _, err := toml.DecodeFile(path, &cfg); err != nil {
md, err := toml.DecodeFile(path, &cfg)
if err != nil {
return cfg, fmt.Errorf("parse %s: %w", path, err)
}
if cfg.DefaultDB == "" {
if !md.IsDefined("default_db") || cfg.DefaultDB == "" {
cfg.DefaultDB = defaultConfig().DefaultDB
}
if !md.IsDefined("display_art") {
cfg.DisplayArt = defaultConfig().DisplayArt
}
if !md.IsDefined("warn_on_delete") {
cfg.WarnOnDelete = defaultConfig().WarnOnDelete
}
if !md.IsDefined("warn_on_overwrite") {
cfg.WarnOnOverwrite = defaultConfig().WarnOnOverwrite
}
return cfg, nil
}

View file

@ -70,7 +70,7 @@ func del(cmd *cobra.Command, args []string) error {
return fmt.Errorf("cannot remove: No such key")
}
if !force {
if !force && config.WarnOnDelete {
var confirm string
quotedTargets := make([]string, 0, len(targetKeys))
for _, t := range targetKeys {

View file

@ -70,7 +70,7 @@ func mv(cmd *cobra.Command, args []string) error {
readonly: false,
sync: false,
transact: func(tx *badger.Txn, k []byte) error {
if !force {
if !force && config.WarnOnOverwrite {
if _, err := tx.Get(k); err == nil {
return fmt.Errorf("cannot move '%s': '%s' already exists > run with --force to overwrite", fromSpec.Key, toSpec.Key)
} else if err != badger.ErrKeyNotFound {

View file

@ -32,15 +32,8 @@ import (
var rootCmd = &cobra.Command{
Use: "pda",
Short: "A key-value store.",
Long: `
(c) 2025 Lewis Wynne
`}
Long: asciiArt,
}
func Execute() {
if configErr != nil {

View file

@ -28,15 +28,18 @@ import (
)
var (
version = "2025.51"
version = "pda! 2025.51 release"
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the version of your CLI tool",
Short: "Display pda! version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("pda! %s\n", version)
if config.DisplayArt {
fmt.Print(asciiArt + "\n ")
}
fmt.Printf("%s\n", version)
},
}