cli/main.bend

near-v-framework@0.1.0.0 · 57 lines · 0 definitions

1# V: data structures for your Bend algorithms.
2import Base
3import near-v-framework@0.1.0.0/cli/v.bend as M
4import near-v-framework@0.1.0.0/cli/project.bend as P
5import near-v-framework@0.1.0.0/cli/vimport.bend as I
6import near-v-framework@0.1.0.0/cli/vdry.bend as D
7
8# The `v` command. Its commands are namespaced by what they act on: `v system ...` for a system.
9def usage() -> IO(Unit):
10  IO.print("usage: v system check | plan | snapshot | import | dryrun | deploy | diagram [out.png]")
11
12def plan() -> IO(Unit):
13  do IO<Unit>:
14    p : String <- P.run_bend("plan.bend")
15    IO.print(p)
16
17def pick_io(c: Bool, a: IO(Unit), b: IO(Unit)) -> IO(Unit):
18  match c:
19    case True{}:
20      a
21    case False{}:
22      b
23
24def first_or(xs: List<String>, d: String) -> String:
25  match xs:
26    case []:
27      d
28    case x <> _:
29      x
30
31def run(+cmd: String, rest: List<String>) -> IO(Unit):
32  pick_io(String.eq(cmd, "check"), M.check(),
33  pick_io(String.eq(cmd, "plan"), plan(),
34  pick_io(String.eq(cmd, "snapshot"), M.snapshot_only(),
35  pick_io(String.eq(cmd, "import"), I.import_mac(),
36  pick_io(String.eq(cmd, "dryrun"), D.dryrun(),
37  pick_io(String.eq(cmd, "deploy"), M.deploy(),
38  pick_io(String.eq(cmd, "diagram"), M.diagram(first_or(rest, "diagram.png")), usage())))))))
39
40def system(args: List<String>) -> IO(Unit):
41  match args:
42    case []:
43      usage()
44    case cmd <> rest:
45      run(cmd, rest)
46
47def dispatch(args: List<String>) -> IO(Unit):
48  match args:
49    case []:
50      usage()
51    case +ns <> rest:
52      pick_io(String.eq(ns, "system"), system(rest), usage())
53
54def main() -> IO(Unit):
55  do IO<Unit>:
56    args : List<String> <- IO.args()
57    dispatch(args)