allowed queries from shell
This commit is contained in:
parent
324aeb7cac
commit
b6bba8fcd1
@ -51,15 +51,13 @@ pub fn parse(query: String) {
|
||||
}
|
||||
|
||||
pub fn get(tree, path, recursive) -> Result(tree.Tree, QueryError) {
|
||||
{
|
||||
let res = case recursive {
|
||||
False -> tree.get(tree, path)
|
||||
_ -> tree.rget(tree, path)
|
||||
}
|
||||
case res {
|
||||
Error(_) -> Error(QueryError("There is no such node."))
|
||||
Ok(tree) -> Ok(tree)
|
||||
}
|
||||
let res = case recursive {
|
||||
False -> tree.get(tree, path)
|
||||
_ -> tree.rget(tree, path)
|
||||
}
|
||||
case res {
|
||||
Error(_) -> Error(QueryError("There is no such node."))
|
||||
Ok(tree) -> Ok(tree)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ import gleam/io
|
||||
import gleam/list
|
||||
import gleam/option
|
||||
import gleam/otp/actor
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import gleam_community/ansi
|
||||
import glisten
|
||||
@ -55,20 +56,10 @@ pub fn init(config_path) {
|
||||
err |> string.inspect |> bytes_tree.from_string,
|
||||
)
|
||||
Ok(#(root, evt)) -> {
|
||||
let tree = forest.trees |> dict.get(root)
|
||||
case tree {
|
||||
Error(_) ->
|
||||
glisten.send(conn, "No such tree." |> bytes_tree.from_string)
|
||||
Ok(tree) -> {
|
||||
let res = tree.1 |> process.call_forever(evt)
|
||||
let _ = case res {
|
||||
Error(query_error.QueryError(reason)) ->
|
||||
glisten.send(conn, reason |> bytes_tree.from_string)
|
||||
Ok(res) -> {
|
||||
glisten.send(conn, res |> bytes_tree.from_string)
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = run_query(forest, root, evt)
|
||||
case result {
|
||||
Ok(ok) -> glisten.send(conn, ok |> bytes_tree.from_string)
|
||||
Error(err) -> glisten.send(conn, err |> bytes_tree.from_string)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,6 +72,20 @@ pub fn init(config_path) {
|
||||
forest
|
||||
}
|
||||
|
||||
fn run_query(forest: forest.Forest, root, evt) {
|
||||
let tree = forest.trees |> dict.get(root)
|
||||
case tree {
|
||||
Error(_) -> Error("No such tree.")
|
||||
Ok(tree) -> {
|
||||
let res = tree.1 |> process.call_forever(evt)
|
||||
let _ = case res {
|
||||
Error(query_error.QueryError(reason)) -> Error(reason)
|
||||
Ok(res) -> Ok(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_next(forest) {
|
||||
let command = erlang.get_line("> ")
|
||||
case command {
|
||||
@ -90,19 +95,37 @@ fn read_next(forest) {
|
||||
}
|
||||
|
||||
Ok(term) -> {
|
||||
let term = term |> string.drop_end(1)
|
||||
case term {
|
||||
"exit\n" -> {
|
||||
"exit" -> {
|
||||
graceful(forest)
|
||||
}
|
||||
"help\n" -> {
|
||||
"Available commands:\n" |> io.println
|
||||
"help\n" |> io.println
|
||||
"exit\n" |> io.println
|
||||
"help" -> {
|
||||
"Available commands:" |> io.println
|
||||
"help" |> io.println
|
||||
"exit" |> io.println
|
||||
"get <path>" |> io.println
|
||||
"rget <path>" |> io.println
|
||||
"set <path> <data>" |> io.println
|
||||
"trees" |> io.println
|
||||
|
||||
read_next(forest)
|
||||
}
|
||||
"trees" -> {
|
||||
forest.trees |> dict.keys |> list.each(io.println)
|
||||
read_next(forest)
|
||||
}
|
||||
|
||||
_ -> {
|
||||
"Unknown command" |> io.println
|
||||
case query.parse(term) {
|
||||
Error(_) -> {
|
||||
"Unknown command" |> io.println
|
||||
}
|
||||
Ok(#(root, evt)) -> {
|
||||
let res = run_query(forest, root, evt)
|
||||
res |> result.unwrap_both |> io.println
|
||||
}
|
||||
}
|
||||
|
||||
read_next(forest)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user