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>
main v0.0.2
jolheiser 2023-05-23 12:08:32 -05:00
parent bc07bf116e
commit a882e0802c
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 119 additions and 34 deletions

151
main.go
View File

@ -8,6 +8,7 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
@ -19,13 +20,17 @@ import (
var Version = "develop"
func main() {
defaultStore, err := xdg.ConfigFile("kv/store.json")
configHome := xdg.ConfigHome
if configHome == "" {
home, err := os.UserHomeDir()
if err != nil {
defaultStore = "store.json"
home = "."
}
configHome = home
}
defaultStore := filepath.Join(configHome, "kv")
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")
a := &app{
@ -66,6 +71,8 @@ type app struct {
func (a *app) get() *ffcli.Command {
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{
Name: "get",
FlagSet: fs,
@ -80,7 +87,7 @@ func (a *app) get() *ffcli.Command {
}
key := strings.ToLower(strings.Join(args, " "))
val, ok := data[key]
val, ok := data.Val(*storeFlag, key)
if !ok {
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 {
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{
Name: "set",
FlagSet: fs,
@ -105,9 +114,24 @@ func (a *app) set() *ffcli.Command {
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]
data[key] = strings.Join(args[1:], " ")
if err := a.save(data); err != nil {
store[key] = strings.Join(args[1:], " ")
if err := a.save(*storeFlag, store); err != nil {
return err
}
fmt.Printf("set %q\n", key)
@ -118,6 +142,8 @@ func (a *app) set() *ffcli.Command {
func (a *app) del() *ffcli.Command {
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{
Name: "del",
FlagSet: fs,
@ -131,12 +157,17 @@ func (a *app) del() *ffcli.Command {
return err
}
store, ok := data[*storeFlag]
if !ok {
return fmt.Errorf("no store found for %q", *storeFlag)
}
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)
}
delete(data, key)
if err := a.save(data); err != nil {
delete(store, key)
if err := a.save(*storeFlag, store); err != nil {
return err
}
@ -149,6 +180,8 @@ func (a *app) del() *ffcli.Command {
func (a *app) list() *ffcli.Command {
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{
Name: "list",
FlagSet: fs,
@ -160,7 +193,7 @@ func (a *app) list() *ffcli.Command {
var keys []string
prefix := strings.ToLower(strings.Join(args, " "))
for key := range data {
for key := range data.Map() {
if strings.HasPrefix(key, prefix) {
keys = append(keys, key)
}
@ -176,31 +209,83 @@ func (a *app) list() *ffcli.Command {
}
}
func (a *app) load() (map[string]string, error) {
var m map[string]string
fi, err := os.Open(*a.storeLocation)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
fi, err = os.Create(*a.storeLocation)
if err != nil {
return m, err
type store map[string]map[string]string
func (s store) Val(store, key string) (string, bool) {
if store != "" {
val, ok := s[store][key]
return val, ok
}
if _, err := fi.WriteString("{}"); err != nil {
return m, err
}
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)
val, ok := s.Map()[key]
return val, ok
}
func (a *app) save(m map[string]string) error {
fi, err := os.Create(*a.storeLocation)
func (s store) Map() map[string]string {
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
}
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 {
return err
}