feat(config): warn_on_delete, warn_on_overwrite, display_art toggles in config
This commit is contained in:
parent
62c770dbf3
commit
6efa9863fe
5 changed files with 43 additions and 19 deletions
|
|
@ -11,10 +11,22 @@ import (
|
||||||
|
|
||||||
type Config struct {
|
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 (
|
var (
|
||||||
config Config
|
config Config
|
||||||
|
asciiArt string = ` ▄▄
|
||||||
|
██
|
||||||
|
██▄███▄ ▄███▄██ ▄█████▄
|
||||||
|
██▀ ▀██ ██▀ ▀██ ▀ ▄▄▄██
|
||||||
|
██ ██ ██ ██ ▄██▀▀▀██
|
||||||
|
███▄▄██▀ ▀██▄▄███ ██▄▄▄███
|
||||||
|
██ ▀▀▀ ▀▀▀ ▀▀ ▀▀▀▀ ▀▀
|
||||||
|
██ (c) 2025 Lewis Wynne
|
||||||
|
`
|
||||||
configErr error
|
configErr error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,6 +37,9 @@ func init() {
|
||||||
func defaultConfig() Config {
|
func defaultConfig() Config {
|
||||||
return Config{
|
return Config{
|
||||||
DefaultDB: "default",
|
DefaultDB: "default",
|
||||||
|
DisplayArt: false,
|
||||||
|
WarnOnOverwrite: true,
|
||||||
|
WarnOnDelete: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,14 +58,27 @@ func loadConfig() (Config, error) {
|
||||||
return cfg, err
|
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)
|
return cfg, fmt.Errorf("parse %s: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.DefaultDB == "" {
|
if !md.IsDefined("default_db") || cfg.DefaultDB == "" {
|
||||||
cfg.DefaultDB = defaultConfig().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
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ func del(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("cannot remove: No such key")
|
return fmt.Errorf("cannot remove: No such key")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !force {
|
if !force && config.WarnOnDelete {
|
||||||
var confirm string
|
var confirm string
|
||||||
quotedTargets := make([]string, 0, len(targetKeys))
|
quotedTargets := make([]string, 0, len(targetKeys))
|
||||||
for _, t := range targetKeys {
|
for _, t := range targetKeys {
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ func mv(cmd *cobra.Command, args []string) error {
|
||||||
readonly: false,
|
readonly: false,
|
||||||
sync: false,
|
sync: false,
|
||||||
transact: func(tx *badger.Txn, k []byte) error {
|
transact: func(tx *badger.Txn, k []byte) error {
|
||||||
if !force {
|
if !force && config.WarnOnOverwrite {
|
||||||
if _, err := tx.Get(k); err == nil {
|
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)
|
return fmt.Errorf("cannot move '%s': '%s' already exists > run with --force to overwrite", fromSpec.Key, toSpec.Key)
|
||||||
} else if err != badger.ErrKeyNotFound {
|
} else if err != badger.ErrKeyNotFound {
|
||||||
|
|
|
||||||
11
cmd/root.go
11
cmd/root.go
|
|
@ -32,15 +32,8 @@ import (
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "pda",
|
Use: "pda",
|
||||||
Short: "A key-value store.",
|
Short: "A key-value store.",
|
||||||
Long: ` ▄▄
|
Long: asciiArt,
|
||||||
██
|
}
|
||||||
██▄███▄ ▄███▄██ ▄█████▄
|
|
||||||
██▀ ▀██ ██▀ ▀██ ▀ ▄▄▄██
|
|
||||||
██ ██ ██ ██ ▄██▀▀▀██
|
|
||||||
███▄▄██▀ ▀██▄▄███ ██▄▄▄███
|
|
||||||
██ ▀▀▀ ▀▀▀ ▀▀ ▀▀▀▀ ▀▀
|
|
||||||
██ (c) 2025 Lewis Wynne
|
|
||||||
`}
|
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if configErr != nil {
|
if configErr != nil {
|
||||||
|
|
|
||||||
|
|
@ -28,15 +28,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "2025.51"
|
version = "pda! 2025.51 release"
|
||||||
)
|
)
|
||||||
|
|
||||||
// versionCmd represents the version command
|
// versionCmd represents the version command
|
||||||
var versionCmd = &cobra.Command{
|
var versionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Show the version of your CLI tool",
|
Short: "Display pda! version",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue