-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (67 loc) · 1.34 KB
/
main.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
76
77
78
79
// i3go project main.go
package main
import (
"fmt"
"github.com/proxypoke/i3ipc"
"os"
"strings"
)
var (
command = ""
)
const (
Help = "help"
Version = "version"
Tree = "tree"
)
const Usage = `Usage: %s <command> [args]
commands:
%s: shows this message
%s: shows the version of i3.
%s: shows the tree structure
`
func parse_cmd() {
command = os.Args[0]
var paths = strings.SplitAfter(command, "/")
command = paths[len(paths)-1]
}
func show_usage() {
fmt.Fprintf(os.Stderr, Usage, command, Help, Version, Tree)
}
func show_version(ipcsocket *i3ipc.IPCSocket) {
if version, err := ipcsocket.GetVersion(); err == nil {
fmt.Println("i3wm " + version.Human_Readable)
} else {
fmt.Fprintf(os.Stderr, "error: ", err)
os.Exit(3)
}
}
func show_tree(ipcsocket *i3ipc.IPCSocket) {
if node, err := ipcsocket.GetTree(); err == nil {
fmt.Println(node)
} else {
fmt.Fprintf(os.Stderr, "error: ", err)
os.Exit(3)
}
}
func main() {
parse_cmd()
if len(os.Args) == 1 {
fmt.Fprintf(os.Stderr, "%s: Invalid usage:\n\n", command)
show_usage()
os.Exit(1)
}
if ipcsocket, err := i3ipc.GetIPCSocket(); err == nil {
switch fn := os.Args[1]; fn {
case Version:
show_version(ipcsocket)
case Tree:
show_tree(ipcsocket)
case Help:
show_usage()
}
} else {
fmt.Fprintf(os.Stderr, "socket error: ", err)
os.Exit(2)
}
}