64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
// Command mesh-delivery-cli is the operator's command for the delivery view (novox/hq ADR 0429 §3). It asks the
|
|
// view's local socket for the rows and prints them; it holds no credential. `answer <ask key> <option> [words]` asks the view
|
|
// to write the operator's answer to an ask that needs no proof of who answered (the view refuses the merge's Approve).
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"mesh-delivery-view/internal/view"
|
|
)
|
|
|
|
const defaultSocket = "/var/lib/mesh-delivery-view-cli/cli.sock"
|
|
|
|
func main() {
|
|
socket := os.Getenv("MESH_DELIVERY_SOCKET")
|
|
if socket == "" {
|
|
socket = defaultSocket
|
|
}
|
|
os.Exit(run(os.Args[1:], socket, os.Stdout, os.Stderr))
|
|
}
|
|
|
|
func run(args []string, socket string, out, errOut io.Writer) int {
|
|
fs := flag.NewFlagSet("mesh-delivery-cli", flag.ContinueOnError)
|
|
fs.SetOutput(errOut)
|
|
asJSON := fs.Bool("json", false, "print the rows as JSON")
|
|
if err := fs.Parse(args); err != nil {
|
|
return 2
|
|
}
|
|
if fs.NArg() > 0 {
|
|
if fs.Arg(0) != "answer" || fs.NArg() < 3 {
|
|
fmt.Fprintln(errOut, "mesh-delivery-cli shows the rows, or answers: mesh-delivery-cli answer <ask key> <option> [words]")
|
|
return 2
|
|
}
|
|
key, option := fs.Arg(1), fs.Arg(2)
|
|
if err := view.AnswerAsk(socket, key, option, strings.Join(fs.Args()[3:], " ")); err != nil {
|
|
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
|
|
return 1
|
|
}
|
|
fmt.Fprintf(out, "answered %s with %s; the teller fills the ask from it\n", key, option)
|
|
return 0
|
|
}
|
|
s, err := view.Ask(socket)
|
|
if err != nil {
|
|
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
|
|
return 1
|
|
}
|
|
if *asJSON {
|
|
enc := json.NewEncoder(out)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(s); err != nil {
|
|
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
view.Table(out, s)
|
|
return 0
|
|
}
|