-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
158 lines (137 loc) · 3.56 KB
/
main.go
File metadata and controls
158 lines (137 loc) · 3.56 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/ActiveMemory/ctx-telephony/internal/agent"
"github.com/ActiveMemory/ctx-telephony/internal/cli"
"github.com/ActiveMemory/ctx-telephony/internal/config"
)
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
cfg, err := config.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
os.Exit(1)
}
switch os.Args[1] {
case "agent":
runAgent(cfg)
case "send":
// tel send <target> <command...>
// tel send --script <file> <target>
if len(os.Args) < 4 {
fmt.Fprintln(os.Stderr, "usage: tel send <target> <command...>")
fmt.Fprintln(os.Stderr, " tel send --script <file> <target>")
os.Exit(1)
}
var target, cmdStr, scriptFile string
timeout := 300
if os.Args[2] == "--script" {
if len(os.Args) < 5 {
fmt.Fprintln(os.Stderr, "usage: tel send --script <file> <target>")
os.Exit(1)
}
scriptFile = os.Args[3]
target = os.Args[4]
cmdStr = "script:" + scriptFile
} else {
target = os.Args[2]
cmdStr = strings.Join(os.Args[3:], " ")
}
// Check for --timeout flag anywhere in args
for i, arg := range os.Args {
if arg == "--timeout" && i+1 < len(os.Args) {
timeout, _ = strconv.Atoi(os.Args[i+1])
}
}
if err := cli.Send(cfg, target, cmdStr, scriptFile, timeout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
case "deploy":
// tel deploy [flags] <target> <repo-path> <dest>
executable := true
noOverwrite := true
args := os.Args[2:]
// Parse flags
var positional []string
for _, arg := range args {
switch arg {
case "--no-exec":
executable = false
case "--overwrite":
noOverwrite = false
default:
positional = append(positional, arg)
}
}
if len(positional) < 3 {
fmt.Fprintln(os.Stderr, "usage: tel deploy [--no-exec] [--overwrite] <target> <repo-path> <dest>")
os.Exit(1)
}
target := positional[0]
repoSrc := positional[1]
dest := positional[2]
if err := cli.DeployFile(cfg, target, repoSrc, dest, executable, noOverwrite); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
case "watch":
pollSecs := 30
if len(os.Args) > 2 {
pollSecs, _ = strconv.Atoi(os.Args[2])
}
cli.Watch(cfg, pollSecs)
case "status":
if err := cli.Status(cfg); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
default:
// Shorthand: tel <target> <command...>
// e.g., tel hades df -h
target := os.Args[1]
if len(os.Args) < 3 {
usage()
os.Exit(1)
}
cmdStr := strings.Join(os.Args[2:], " ")
if err := cli.Send(cfg, target, cmdStr, "", 300); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
}
func runAgent(cfg *config.Config) {
ctx, cancel := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
cancel()
}()
a := agent.New(cfg)
if err := a.Run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "agent error: %v\n", err)
os.Exit(1)
}
}
func usage() {
fmt.Fprintln(os.Stderr, `telephony — async command bus over git
usage:
tel agent run as daemon (asgard/hades)
tel send <target> <command...> queue a command
tel send --script <file> <target> queue a script
tel deploy [--no-exec] [--overwrite] <target> <repo-path> <dest>
tel <target> <command...> shorthand for send
tel watch [poll_secs] watch for results
tel status show heartbeats`)
}