feat(init): initial commit
This commit is contained in:
commit
0520418489
7 changed files with 354 additions and 0 deletions
56
cmd/get.go
Normal file
56
cmd/get.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// getCmd represents the get command
|
||||
var getCmd = &cobra.Command{
|
||||
Use: "get KEY[@DB]",
|
||||
Short: "Get a value for a key. Optionally specify a db.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: get,
|
||||
}
|
||||
|
||||
func get(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
var v []byte
|
||||
if err := store.Transaction(args[0], true, func(tx *badger.Txn, k []byte) error {
|
||||
item, err := tx.Get(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err = item.ValueCopy(nil)
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
store.Print("%s", v)
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getCmd)
|
||||
}
|
||||
64
cmd/root.go
Normal file
64
cmd/root.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "pda",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.pda.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
109
cmd/shared.go
Normal file
109
cmd/shared.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
gap "github.com/muesli/go-app-paths"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type Store struct{}
|
||||
|
||||
func (s *Store) parse(k string) ([]byte, string, error) {
|
||||
var key, db string
|
||||
ps := strings.Split(k, "@")
|
||||
switch len(ps) {
|
||||
case 1:
|
||||
key = strings.ToLower(ps[0])
|
||||
case 2:
|
||||
key = strings.ToLower(ps[0])
|
||||
db = strings.ToLower(ps[1])
|
||||
default:
|
||||
return nil, "", fmt.Errorf("bad key format, use KEY@DB")
|
||||
}
|
||||
return []byte(key), db, nil
|
||||
}
|
||||
|
||||
func (s *Store) open(name string) (*badger.DB, error) {
|
||||
if name == "" {
|
||||
name = "default"
|
||||
}
|
||||
path, err := s.path(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return badger.Open(badger.DefaultOptions(path).WithLoggingLevel(badger.ERROR))
|
||||
}
|
||||
|
||||
func (s *Store) path(args ...string) (string, error) {
|
||||
scope := gap.NewVendorScope(gap.User, "pda", "stores")
|
||||
dir, err := scope.DataPath("")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(append([]string{dir}, args...)...), nil
|
||||
}
|
||||
|
||||
func (s *Store) Print(pf string, vs ...[]byte) {
|
||||
nb := "(omitted binary data)"
|
||||
fvs := make([]any, 0)
|
||||
tty := term.IsTerminal(int(os.Stdin.Fd()))
|
||||
for _, v := range vs {
|
||||
if tty && !utf8.Valid(v) {
|
||||
fvs = append(fvs, nb)
|
||||
} else {
|
||||
fvs = append(fvs, string(v))
|
||||
}
|
||||
}
|
||||
fmt.Printf(pf, fvs...)
|
||||
if tty && !strings.HasSuffix(pf, "\n") {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) Transaction(key string, readonly bool, fn func(tx *badger.Txn, key []byte) error) error {
|
||||
k, dbName, err := s.parse(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := s.open(dbName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
tx := db.NewTransaction(!readonly)
|
||||
if err := fn(tx, k); err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue