tree command

This commit is contained in:
Arnas Udovic 2024-08-29 17:57:17 +03:00
parent 098b809310
commit b87fbd4991
3 changed files with 49 additions and 4 deletions

2
go.mod
View file

@ -2,6 +2,6 @@ module g.arns.lt/zordsdavini/zordfsdb-client
go 1.23.0 go 1.23.0
require g.arns.lt/zordsdavini/zordfsdb v1.0.1 require g.arns.lt/zordsdavini/zordfsdb v1.0.2
require g.arns.lt/zordsdavini/abcex v1.0.1 // indirect require g.arns.lt/zordsdavini/abcex v1.0.1 // indirect

4
go.sum
View file

@ -1,7 +1,7 @@
g.arns.lt/zordsdavini/abcex v1.0.1 h1:ZN/xSDQb8CiR9+jXHYp+tUtGILMeRu9PvvqhypZDkyI= g.arns.lt/zordsdavini/abcex v1.0.1 h1:ZN/xSDQb8CiR9+jXHYp+tUtGILMeRu9PvvqhypZDkyI=
g.arns.lt/zordsdavini/abcex v1.0.1/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8= g.arns.lt/zordsdavini/abcex v1.0.1/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8=
g.arns.lt/zordsdavini/zordfsdb v1.0.1 h1:XM/5qR2PsGBFUwT9GrCbiPEUySoZHKP6yeRcRDCO3no= g.arns.lt/zordsdavini/zordfsdb v1.0.2 h1:J2rTJa5rbG2RiSv3uQ6G8Y8LdUiZ/rPMlsdo6ilwXv8=
g.arns.lt/zordsdavini/zordfsdb v1.0.1/go.mod h1:gq30YNjz3yIMdI4cA5e5mK90V4Yns5NAsbwi6s8Bf3Q= g.arns.lt/zordsdavini/zordfsdb v1.0.2/go.mod h1:gq30YNjz3yIMdI4cA5e5mK90V4Yns5NAsbwi6s8Bf3Q=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w= github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=

47
main.go
View file

@ -64,6 +64,7 @@ SAVE [path] [value] - set value to property
CREATE [path] - create list/object in path (parent path should exist) CREATE [path] - create list/object in path (parent path should exist)
ADD [path] - add abcex indexed object to list. Will return index ADD [path] - add abcex indexed object to list. Will return index
INFO [path] - get all info about property/object/list INFO [path] - get all info about property/object/list
TREE [path] - draw db part in path with values
HELP - will print this help HELP - will print this help
QUIT - exit the shell QUIT - exit the shell
@ -71,7 +72,7 @@ Last used path can be accessed by _
Last returned index (after ADD command) can be accessed by $ Last returned index (after ADD command) can be accessed by $
Short commands can be as: Short commands can be as:
KEYS(\K), GET(\G), INC(\I), DEC(\D), NOW(\N), DEL(\R), SAVE(\S), CREATE(\C), ADD(\A), INFO(\?), HELP(\H), QUIT(\Q) 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 continue
} }
@ -128,6 +129,32 @@ func ExecuteCommand(db zordfsdb.DB, commands string) error {
} }
break break
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("")
break
case "GET": case "GET":
case "\\G": case "\\G":
if len(parts) == 1 || len(parts[1]) == 0 { if len(parts) == 1 || len(parts[1]) == 0 {
@ -268,3 +295,21 @@ func ExecuteCommand(db zordfsdb.DB, commands string) error {
return nil return nil
} }
func printNodeTree(node zordfsdb.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)
}
}
}