/
cmd.go
75 lines (59 loc) · 1.21 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pitchforkui
import (
pf "trident.li/pitchfork/lib"
)
/* /api/<cmd>/<arg>/<arg...> */
func h_api(cui PfUI) {
var err error
/* Force bearer auth */
if cui.GetToken() == "" {
cui.SetBearerAuth(true)
}
/* Run the command */
err = cui.Cmd(cui.GetPath())
if err != nil {
cui.OutLn("An error occured: %s", err.Error())
}
}
/* Simple CLI interface */
func h_cli(cui PfUI) {
var err error
out := ""
cmd := ""
if cui.IsPOST() {
cmd, err = cui.FormValue("cmd")
}
if cmd == "" {
cmd = "help"
}
if err == nil {
/* Parse the string (obeying quoting) */
args := pf.SplitArgs(cmd)
/* Run the command */
out, err = cui.CmdOut("", args)
/* Handle anything that causes a logout */
if !cui.IsLoggedIn() {
h_logout(cui)
return
}
}
if err != nil {
out += "An error occured: "
out += err.Error() + "\n"
}
/* Output the page */
type popt struct {
Cmd string `label:"Command" hint:"The command to execute"`
Button string `label:"Execute" pftype:"submit"`
}
type Page struct {
*PfPage
Output string
Opt popt
}
cui.SetPageMenu(nil)
cui.AddCrumb("", "CLI", "Tickly (CLI)")
opt := popt{cmd, ""}
p := Page{cui.Page_def(), out, opt}
cui.Page_show("misc/cli.tmpl", p)
}