add commands to be executed as script
This commit is contained in:
parent
ecfd279d7e
commit
77933ceacd
4 changed files with 316 additions and 0 deletions
29
README.md
29
README.md
|
@ -38,6 +38,7 @@ Commands can be split into value layer when you know the structure and node laye
|
||||||
|
|
||||||
* Del - delete value, object or list by given path
|
* Del - delete value, object or list by given path
|
||||||
* Keys - return possible keys for object or ids for list
|
* Keys - return possible keys for object or ids for list
|
||||||
|
* ExecuteCommands - execute commands separated by semicolon. See **Commands** section
|
||||||
|
|
||||||
## dictionary
|
## dictionary
|
||||||
|
|
||||||
|
@ -45,6 +46,34 @@ Commands can be split into value layer when you know the structure and node laye
|
||||||
* list - directory of objects named by abcex as key
|
* list - directory of objects named by abcex as key
|
||||||
* path - path to key or object or list
|
* path - path to key or object or list
|
||||||
|
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
DB can run one or many commands separated by semicolon. Same commands are used in template to add new list object.
|
||||||
|
|
||||||
|
* 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), HELP(\H), QUIT(\Q)
|
||||||
|
|
||||||
|
|
||||||
## DEV
|
## DEV
|
||||||
|
|
||||||
Run tests: `$ go test`
|
Run tests: `$ go test`
|
||||||
|
|
266
command.go
Normal file
266
command.go
Normal file
|
@ -0,0 +1,266 @@
|
||||||
|
// 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 zordfsdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
lastPath string
|
||||||
|
lastKey string
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExecuteCommands(db DB, commands string, lastPathIn string, lastKeyIn string) (error, string, string) {
|
||||||
|
lastPath = lastPathIn
|
||||||
|
lastKey = lastKeyIn
|
||||||
|
for _, command := range strings.Split(commands, ";") {
|
||||||
|
command = strings.Trim(command, " ")
|
||||||
|
parts := strings.Split(command, " ")
|
||||||
|
if len(parts) > 1 {
|
||||||
|
if lastPath != "" {
|
||||||
|
if parts[1] == "_" {
|
||||||
|
parts[1] = lastPath
|
||||||
|
} else {
|
||||||
|
parts[1] = strings.Replace(parts[1], "_.", lastPath+".", 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if lastKey != "" {
|
||||||
|
parts[1] = strings.Replace(parts[1], "$", lastKey, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
lastPath = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
err := ExecuteCommand(db, parts)
|
||||||
|
if err != nil {
|
||||||
|
return err, lastPath, lastKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, lastPath, lastKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExecuteCommand(db DB, parts []string) error {
|
||||||
|
switch strings.ToUpper(parts[0]) {
|
||||||
|
case "KEYS":
|
||||||
|
case "\\K":
|
||||||
|
kpath := "."
|
||||||
|
if !(len(parts) == 1 || len(parts[1]) == 0) {
|
||||||
|
kpath = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("{%s} KEYS: ", kpath)
|
||||||
|
fmt.Println(db.Keys(kpath))
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
case "INFO":
|
||||||
|
case "\\?":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: INFO [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
node, ok := db.GetNode(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No path found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} INFO\n-----------------\n", parts[1])
|
||||||
|
ntype := "property"
|
||||||
|
if node.List {
|
||||||
|
ntype = "list"
|
||||||
|
} else if node.Object {
|
||||||
|
ntype = "object"
|
||||||
|
}
|
||||||
|
fmt.Println("type:", ntype)
|
||||||
|
if ntype == "property" {
|
||||||
|
fmt.Println("value:", node.Value)
|
||||||
|
} else {
|
||||||
|
fmt.Println("keys:", db.Keys(parts[1]))
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "TREE":
|
||||||
|
case "\\T":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
||||||
|
fmt.Printf("TREE\n-----------------\n\\\n")
|
||||||
|
printNodeTree(&db, 1)
|
||||||
|
fmt.Println("")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
node, ok := db.GetNode(parts[1])
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("err format: TREE [path]\n")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} TREE\n-----------------\n", parts[1])
|
||||||
|
if !node.List && !node.Object {
|
||||||
|
fmt.Printf("%s: %s\n\n", node.Key, node.Value)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(node.Key)
|
||||||
|
printNodeTree(&node, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
case "GET":
|
||||||
|
case "\\G":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: GET [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
value, ok := db.Get(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} VALUE: ", parts[1])
|
||||||
|
fmt.Println(value)
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "INC":
|
||||||
|
case "\\I":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: INC [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := db.Inc(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} INC OK\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "DEC":
|
||||||
|
case "\\D":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: DEC [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := db.Dec(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} DEC OK\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "NOW":
|
||||||
|
case "\\N":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: NOW [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := db.Now(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} NOW OK\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "DEL":
|
||||||
|
case "\\R":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: DEL [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := db.Del(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value/object found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} DELETED\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "SAVE":
|
||||||
|
case "\\S":
|
||||||
|
if len(parts) < 3 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: SAVE [path] [value|long value]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
value := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||||
|
|
||||||
|
ok := db.Save(parts[1], value)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No value found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} SAVED\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
db.Refresh()
|
||||||
|
|
||||||
|
case "CREATE":
|
||||||
|
case "\\C":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: CREATE [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := db.CreateNode(parts[1])
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("No path found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} CREATED\n", parts[1])
|
||||||
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "ADD":
|
||||||
|
case "\\A":
|
||||||
|
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||||
|
return fmt.Errorf("err format: ADD [path]\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := db.AddObject(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("No path found.")
|
||||||
|
fmt.Println("")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id)
|
||||||
|
fmt.Println("")
|
||||||
|
lastKey = id
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("err[00] unknown command, try HELP")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func printNodeTree(node GetNodes, i int) {
|
||||||
|
for key, child := range node.GetNodes() {
|
||||||
|
j := i - 1
|
||||||
|
for j > 0 {
|
||||||
|
fmt.Printf("│ ")
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("├── %s", key)
|
||||||
|
if !child.List && !child.Object {
|
||||||
|
fmt.Printf(": %s\n", child.Value)
|
||||||
|
} else {
|
||||||
|
fmt.Println("")
|
||||||
|
printNodeTree(&child, i+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
zordfsdb.go
13
zordfsdb.go
|
@ -1,3 +1,11 @@
|
||||||
|
// 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 zordfsdb
|
package zordfsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -357,3 +365,8 @@ func (db *DB) GetRandomNode(vpath string) (Node, error) {
|
||||||
|
|
||||||
return node.Nodes[randomKey], nil
|
return node.Nodes[randomKey], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) ExecuteCommands(commands string, lastPath string, lastKey string) (error, string, string) {
|
||||||
|
return ExecuteCommands(*db, commands, lastPath, lastKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
|
// 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 zordfsdb
|
package zordfsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
Loading…
Add table
Reference in a new issue