99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
|
|
//
|
|
// 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 main
|
|
|
|
import (
|
|
"bufio"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"g.arns.lt/zordsdavini/zordfsdb"
|
|
)
|
|
|
|
var (
|
|
rootDir string
|
|
executeCommand string
|
|
lastPath string
|
|
lastKey string
|
|
)
|
|
|
|
func main() {
|
|
flag.StringVar(&rootDir, "d", "./", "database root directory")
|
|
flag.StringVar(&executeCommand, "e", "", "execute command")
|
|
|
|
flag.Parse()
|
|
|
|
db, err := zordfsdb.InitDB(rootDir)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if len(executeCommand) > 0 {
|
|
err = db.ExecuteCommands(executeCommand, &lastPath, &lastKey)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
} else {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Println("ZordFsDB Shell")
|
|
fmt.Println("---------------------")
|
|
fmt.Println("DB initialized at ", rootDir)
|
|
fmt.Println("")
|
|
fmt.Println("Copyright (c) 2024 zordsdavini@arns.lt under MIT license")
|
|
fmt.Println("")
|
|
fmt.Println("Type HELP to get list of commands or QUIT to get out")
|
|
fmt.Println("")
|
|
|
|
for {
|
|
fmt.Print("zordfsdb> ")
|
|
text, _ := reader.ReadString('\n')
|
|
// convert CRLF to LF
|
|
text = strings.Replace(text, "\n", "", -1)
|
|
|
|
if strings.Compare("QUIT", strings.ToUpper(text)) == 0 || strings.Compare("\\Q", strings.ToUpper(text)) == 0 {
|
|
fmt.Println("\nBye o/")
|
|
break
|
|
}
|
|
|
|
if strings.Compare("HELP", strings.ToUpper(text)) == 0 || strings.Compare("\\H", strings.ToUpper(text)) == 0 {
|
|
fmt.Println(`
|
|
ZordFsDB shell supported commands:
|
|
|
|
KEYS [path] - get list of existing properties/objects/lists
|
|
GET [path] - get value of property
|
|
INC [path] - increase abcex value in property
|
|
DEC [path] - decrease abcex value in property
|
|
NOW [path] - set value to datetime string in property
|
|
DEL [path] - remove property/object/list (no confirmation)
|
|
SAVE [path] [value] - set value to property
|
|
CREATE [path] - create list/object in path (parent path should exist)
|
|
ADD [path] - add abcex indexed object to list. Will return index
|
|
INFO [path] - get all info about property/object/list
|
|
TREE [path] - draw db part in path with values
|
|
HELP - will print this help
|
|
QUIT - exit the shell
|
|
|
|
Last used path can be accessed by _
|
|
Last returned index (after ADD command) can be accessed by $
|
|
|
|
Short commands can be as:
|
|
KEYS(\K), GET(\G), INC(\I), DEC(\D), NOW(\N), DEL(\R), SAVE(\S), CREATE(\C), ADD(\A), INFO(\?), TREE(\T), HELP(\H), QUIT(\Q)
|
|
`)
|
|
continue
|
|
}
|
|
|
|
err := db.ExecuteCommands(text, &lastPath, &lastKey)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
}
|
|
}
|