diff --git a/README.md b/README.md index 6ec65af..76a27eb 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ pda rm kitty -y

-`pda ls` to see what you've got stored. By default it lists the contents of all stores. Pass a store name to check only the given store. Checking a specific store is faster than checking everything, but the slowdown should be insignificant unless you have masses of different stores. `store.list_all_stores` can be set to false to list `store.default_store_name` by default. +`pda ls` to see what you've got stored. By default it lists the contents of all stores. Pass a store name to check only the given store. Checking a specific store is faster than checking everything, but the slowdown should be insignificant unless you have masses of different stores. `list.list_all_stores` can be set to false to list `store.default_store_name` by default. ```bash pda ls # Key Store Value TTL @@ -789,10 +789,13 @@ always_prompt_overwrite = false [store] default_store_name = "default" -list_all_stores = true always_prompt_delete = true always_prompt_overwrite = true +[list] +list_all_stores = true +default_list_format = "table" + [git] auto_fetch = false auto_commit = true diff --git a/cmd/config.go b/cmd/config.go index aecc107..db252c4 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -35,6 +35,7 @@ type Config struct { DisplayAsciiArt bool `toml:"display_ascii_art"` Key KeyConfig `toml:"key"` Store StoreConfig `toml:"store"` + List ListConfig `toml:"list"` Git GitConfig `toml:"git"` } @@ -46,11 +47,15 @@ type KeyConfig struct { type StoreConfig struct { DefaultStoreName string `toml:"default_store_name"` - ListAllStores bool `toml:"list_all_stores"` AlwaysPromptDelete bool `toml:"always_prompt_delete"` AlwaysPromptOverwrite bool `toml:"always_prompt_overwrite"` } +type ListConfig struct { + ListAllStores bool `toml:"list_all_stores"` + DefaultListFormat string `toml:"default_list_format"` +} + type GitConfig struct { AutoFetch bool `toml:"auto_fetch"` AutoCommit bool `toml:"auto_commit"` @@ -85,10 +90,13 @@ func defaultConfig() Config { }, Store: StoreConfig{ DefaultStoreName: "default", - ListAllStores: true, AlwaysPromptDelete: true, AlwaysPromptOverwrite: true, }, + List: ListConfig{ + ListAllStores: true, + DefaultListFormat: "table", + }, Git: GitConfig{ AutoFetch: false, AutoCommit: false, @@ -121,6 +129,13 @@ func loadConfig() (Config, error) { cfg.Store.DefaultStoreName = defaultConfig().Store.DefaultStoreName } + if cfg.List.DefaultListFormat == "" { + cfg.List.DefaultListFormat = defaultConfig().List.DefaultListFormat + } + if err := validListFormat(cfg.List.DefaultListFormat); err != nil { + return cfg, fmt.Errorf("parse %s: list.default_list_format: %w", path, err) + } + return cfg, nil } diff --git a/cmd/config_fields_test.go b/cmd/config_fields_test.go index 1f994d6..dac76e4 100644 --- a/cmd/config_fields_test.go +++ b/cmd/config_fields_test.go @@ -41,9 +41,10 @@ func TestConfigFieldsDottedKeys(t *testing.T) { "key.always_prompt_glob_delete": true, "key.always_prompt_overwrite": true, "store.default_store_name": true, - "store.list_all_stores": true, "store.always_prompt_delete": true, "store.always_prompt_overwrite": true, + "list.list_all_stores": true, + "list.default_list_format": true, "git.auto_fetch": true, "git.auto_commit": true, "git.auto_push": true, diff --git a/cmd/list.go b/cmd/list.go index a4d1768..8795d8b 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -46,9 +46,16 @@ type formatEnum string func (e *formatEnum) String() string { return string(*e) } func (e *formatEnum) Set(v string) error { + if err := validListFormat(v); err != nil { + return err + } + *e = formatEnum(v) + return nil +} + +func validListFormat(v string) error { switch v { case "table", "tsv", "csv", "html", "markdown", "ndjson", "json": - *e = formatEnum(v) return nil default: return fmt.Errorf("must be one of 'table', 'tsv', 'csv', 'html', 'markdown', 'ndjson', or 'json'") @@ -66,7 +73,7 @@ var ( listFull bool listAll bool listNoHeader bool - listFormat formatEnum = "table" + listFormat formatEnum dimStyle = text.Colors{text.Faint, text.Italic} ) @@ -100,6 +107,10 @@ within the same flag.`, } func list(cmd *cobra.Command, args []string) error { + if listFormat == "" { + listFormat = formatEnum(config.List.DefaultListFormat) + } + store := &Store{} storePatterns, err := cmd.Flags().GetStringSlice("store") @@ -110,7 +121,7 @@ func list(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot use --store with a store argument") } - allStores := len(args) == 0 && (config.Store.ListAllStores || listAll) + allStores := len(args) == 0 && (config.List.ListAllStores || listAll) var targetDB string if allStores { targetDB = "all" diff --git a/testdata/config-list.ct b/testdata/config-list.ct index 3df2e13..b67c388 100644 --- a/testdata/config-list.ct +++ b/testdata/config-list.ct @@ -4,9 +4,10 @@ key.always_prompt_delete = false key.always_prompt_glob_delete = true key.always_prompt_overwrite = false store.default_store_name = default -store.list_all_stores = true store.always_prompt_delete = true store.always_prompt_overwrite = true +list.list_all_stores = true +list.default_list_format = table git.auto_fetch = false git.auto_commit = false git.auto_push = false diff --git a/testdata/help-list.ct b/testdata/help-list.ct index e6aec50..30815c9 100644 --- a/testdata/help-list.ct +++ b/testdata/help-list.ct @@ -21,7 +21,7 @@ Flags: -a, --all list across all stores -b, --base64 view binary data as base64 -c, --count print only the count of matching entries - -o, --format format output format (table|tsv|csv|markdown|html|ndjson|json) (default table) + -o, --format format output format (table|tsv|csv|markdown|html|ndjson|json) -f, --full show full values without truncation -h, --help help for list -k, --key strings filter keys with glob pattern (repeatable) @@ -52,7 +52,7 @@ Flags: -a, --all list across all stores -b, --base64 view binary data as base64 -c, --count print only the count of matching entries - -o, --format format output format (table|tsv|csv|markdown|html|ndjson|json) (default table) + -o, --format format output format (table|tsv|csv|markdown|html|ndjson|json) -f, --full show full values without truncation -h, --help help for list -k, --key strings filter keys with glob pattern (repeatable)