Compare commits

...

5 Commits
v0.0.1 ... main

Author SHA1 Message Date
jolheiser 5fc404783e
fix: mkdirall and nushell comments
ci/woodpecker/push/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-08-20 21:04:36 -05:00
jolheiser 39d0df69d2
chore: rename nushell completions
ci/woodpecker/push/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-08-20 20:28:56 -05:00
jolheiser 8afc4efc10
chore: update nushell completions
ci/woodpecker/push/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-05-23 12:14:06 -05:00
jolheiser a882e0802c
feat: store as dir
ci/woodpecker/push/goreleaser Pipeline was successful Details
ci/woodpecker/tag/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-05-23 12:08:32 -05:00
jolheiser bc07bf116e
feat: nushell completions
ci/woodpecker/push/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-05-23 09:42:15 -05:00
2 changed files with 151 additions and 34 deletions

View File

@ -0,0 +1,29 @@
def keys [] {
^kv list | lines | each { |line| $line | str trim }
}
# Get a value
export extern "kv get" [
key: string@keys # Key
--store(-s) # Use a specific store instead of aggregate
]
# Set a value
export extern "kv set" [
key: string # Key
value: string # Value
...value: string # Value cont.
--store(-s) # Store for this key/value
]
# Delete a value
export extern "kv del" [
key: string@keys # Key
--store(-s) # Store for this key/value
]
# List keys and values
export extern "kv list" [
prefix?: string # Key prefix filter
--store(-s) # Use a specific store instead of aggregate
]

154
main.go
View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath"
"sort" "sort"
"strings" "strings"
@ -19,13 +20,17 @@ import (
var Version = "develop" var Version = "develop"
func main() { func main() {
defaultStore, err := xdg.ConfigFile("kv/store.json") configHome := xdg.ConfigHome
if configHome == "" {
home, err := os.UserHomeDir()
if err != nil { if err != nil {
defaultStore = "store.json" home = "."
} }
configHome = home
}
defaultStore := filepath.Join(configHome, "kv")
fs := flag.NewFlagSet("kv", flag.ContinueOnError) fs := flag.NewFlagSet("kv", flag.ContinueOnError)
storeFlag := fs.String("store", defaultStore, "Location of the store on disk") storeFlag := fs.String("store", defaultStore, "Location of the store directory")
fs.StringVar(storeFlag, "s", *storeFlag, "--store") fs.StringVar(storeFlag, "s", *storeFlag, "--store")
a := &app{ a := &app{
@ -66,6 +71,8 @@ type app struct {
func (a *app) get() *ffcli.Command { func (a *app) get() *ffcli.Command {
fs := flag.NewFlagSet("get", flag.ContinueOnError) fs := flag.NewFlagSet("get", flag.ContinueOnError)
storeFlag := fs.String("store", "", "Use a specific store instead of aggregate")
fs.StringVar(storeFlag, "s", *storeFlag, "--store")
return &ffcli.Command{ return &ffcli.Command{
Name: "get", Name: "get",
FlagSet: fs, FlagSet: fs,
@ -80,7 +87,7 @@ func (a *app) get() *ffcli.Command {
} }
key := strings.ToLower(strings.Join(args, " ")) key := strings.ToLower(strings.Join(args, " "))
val, ok := data[key] val, ok := data.Val(*storeFlag, key)
if !ok { if !ok {
return fmt.Errorf("no value found for %q", key) return fmt.Errorf("no value found for %q", key)
} }
@ -92,6 +99,8 @@ func (a *app) get() *ffcli.Command {
func (a *app) set() *ffcli.Command { func (a *app) set() *ffcli.Command {
fs := flag.NewFlagSet("set", flag.ContinueOnError) fs := flag.NewFlagSet("set", flag.ContinueOnError)
storeFlag := fs.String("store", "kv", "Store for this key/value")
fs.StringVar(storeFlag, "s", *storeFlag, "--store")
return &ffcli.Command{ return &ffcli.Command{
Name: "set", Name: "set",
FlagSet: fs, FlagSet: fs,
@ -105,9 +114,24 @@ func (a *app) set() *ffcli.Command {
return err return err
} }
store, ok := data[*storeFlag]
if !ok {
fi, err := os.Create(filepath.Join(*a.storeLocation, *storeFlag+".json"))
if err != nil {
return err
}
if _, err := fi.WriteString("{}"); err != nil {
return err
}
if err := fi.Close(); err != nil {
return err
}
store = make(map[string]string)
}
key := args[0] key := args[0]
data[key] = strings.Join(args[1:], " ") store[key] = strings.Join(args[1:], " ")
if err := a.save(data); err != nil { if err := a.save(*storeFlag, store); err != nil {
return err return err
} }
fmt.Printf("set %q\n", key) fmt.Printf("set %q\n", key)
@ -118,6 +142,8 @@ func (a *app) set() *ffcli.Command {
func (a *app) del() *ffcli.Command { func (a *app) del() *ffcli.Command {
fs := flag.NewFlagSet("del", flag.ContinueOnError) fs := flag.NewFlagSet("del", flag.ContinueOnError)
storeFlag := fs.String("store", "kv", "Store for this key/value")
fs.StringVar(storeFlag, "s", *storeFlag, "--store")
return &ffcli.Command{ return &ffcli.Command{
Name: "del", Name: "del",
FlagSet: fs, FlagSet: fs,
@ -131,12 +157,17 @@ func (a *app) del() *ffcli.Command {
return err return err
} }
store, ok := data[*storeFlag]
if !ok {
return fmt.Errorf("no store found for %q", *storeFlag)
}
key := strings.ToLower(strings.Join(args, " ")) key := strings.ToLower(strings.Join(args, " "))
if _, ok := data[key]; !ok { if _, ok := store[key]; !ok {
return fmt.Errorf("no value found for %q", key) return fmt.Errorf("no value found for %q", key)
} }
delete(data, key) delete(store, key)
if err := a.save(data); err != nil { if err := a.save(*storeFlag, store); err != nil {
return err return err
} }
@ -149,6 +180,8 @@ func (a *app) del() *ffcli.Command {
func (a *app) list() *ffcli.Command { func (a *app) list() *ffcli.Command {
fs := flag.NewFlagSet("list", flag.ContinueOnError) fs := flag.NewFlagSet("list", flag.ContinueOnError)
storeFlag := fs.String("store", "", "Use a specific store instead of aggregate")
fs.StringVar(storeFlag, "s", *storeFlag, "--store")
return &ffcli.Command{ return &ffcli.Command{
Name: "list", Name: "list",
FlagSet: fs, FlagSet: fs,
@ -160,7 +193,7 @@ func (a *app) list() *ffcli.Command {
var keys []string var keys []string
prefix := strings.ToLower(strings.Join(args, " ")) prefix := strings.ToLower(strings.Join(args, " "))
for key := range data { for key := range data.Map() {
if strings.HasPrefix(key, prefix) { if strings.HasPrefix(key, prefix) {
keys = append(keys, key) keys = append(keys, key)
} }
@ -176,31 +209,86 @@ func (a *app) list() *ffcli.Command {
} }
} }
func (a *app) load() (map[string]string, error) { type store map[string]map[string]string
var m map[string]string
fi, err := os.Open(*a.storeLocation) func (s store) Val(store, key string) (string, bool) {
if err != nil { if store != "" {
if errors.Is(err, fs.ErrNotExist) { val, ok := s[store][key]
fi, err = os.Create(*a.storeLocation) return val, ok
if err != nil {
return m, err
} }
if _, err := fi.WriteString("{}"); err != nil { val, ok := s.Map()[key]
return m, err return val, ok
}
if err := fi.Close(); err != nil {
return m, err
}
return a.load()
}
return m, err
}
defer fi.Close()
return m, json.NewDecoder(fi).Decode(&m)
} }
func (a *app) save(m map[string]string) error { func (s store) Map() map[string]string {
fi, err := os.Create(*a.storeLocation) m := s["kv"]
for sname, ss := range s {
if sname == "kv" {
continue
}
for k, v := range ss {
m[k] = v
}
}
return m
}
func load(paths ...string) store {
s := make(store)
for _, p := range paths {
func() {
var m map[string]string
fi, err := os.Open(p)
if err != nil {
fmt.Printf("could not open %q: %v\n", p, err)
return
}
defer fi.Close()
if err := json.NewDecoder(fi).Decode(&m); err != nil {
fmt.Printf("could not decode %q: %v\n", p, err)
return
}
storeName := strings.TrimSuffix(filepath.Base(p), ".json")
s[storeName] = m
}()
}
return s
}
func (a *app) load() (store, error) {
defaultConfig := filepath.Join(*a.storeLocation, "kv.json")
if _, err := os.Stat(defaultConfig); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
if err := os.MkdirAll(*a.storeLocation, os.ModePerm); err != nil {
return nil, err
}
fi, err := os.Create(defaultConfig)
if err != nil {
return nil, err
}
if _, err := fi.WriteString("{}"); err != nil {
return nil, err
}
if err := fi.Close(); err != nil {
return nil, err
}
}
dirs, err := os.ReadDir(*a.storeLocation)
if err != nil {
return nil, err
}
paths := make([]string, 0, len(dirs))
for _, dir := range dirs {
paths = append(paths, filepath.Join(*a.storeLocation, dir.Name()))
}
return load(paths...), nil
}
func (a *app) save(storeName string, m map[string]string) error {
fi, err := os.Create(filepath.Join(*a.storeLocation, storeName+".json"))
if err != nil { if err != nil {
return err return err
} }