-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
184 lines (163 loc) · 4.86 KB
/
Copy pathmain.go
File metadata and controls
184 lines (163 loc) · 4.86 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
showHelp()
return
}
switch args[0] {
case "help", "--help", "-h":
showHelp()
return
case "all":
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "Usage: devops all <command>")
os.Exit(1)
}
os.Exit(runAllCommand(args[1], args[2:]))
case "reinstall":
os.Exit(runReinstall())
}
modules, err := discoverModules()
if err != nil {
fatalf("error discovering modules: %v", err)
}
// Is the first argument a known module name?
if m := findModule(modules, args[0]); m != nil {
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: devops %s <command>\n", args[0])
os.Exit(1)
}
switch args[1] {
case "exec", "shell":
if err := runExec(m, args[2:]); err != nil {
fatalf("%v", err)
}
default:
os.Exit(runSingleModuleCommand(m, args[1], args[2:]))
}
return
}
// Otherwise treat first argument as a command and run across all modules.
os.Exit(runAllCommand(args[0], args[1:]))
}
// runAllCommand runs command across all modules that define it, respecting
// priority order and launching same-priority groups in parallel.
func runAllCommand(command string, extraArgs []string) int {
modules, err := discoverModules()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}
found := findModulesForCommand(modules, command)
if len(found) == 0 {
fmt.Fprintf(os.Stderr, "No mapping found for command: %s, passing to docker compose...\n", command)
cmd := exec.Command("docker", append([]string{"compose", command}, extraArgs...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := runWithSignalForwarding(cmd); err != nil {
return 1
}
return 0
}
fmt.Printf("Running %q across %d module(s)...\n\n", command, len(found))
for _, group := range groupByPriority(found, command) {
if len(group) == 1 {
m := group[0]
fmt.Printf("=== [%s] ===\n", m.Name)
if err := runModuleSequential(m, command, extraArgs); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
fmt.Println()
} else {
if !runGroupParallel(group, command, extraArgs) {
return 1
}
fmt.Println()
}
}
fmt.Printf("Completed %q across all modules.\n", command)
return 0
}
// runSingleModuleCommand runs a command scoped to one module.
func runSingleModuleCommand(m *Module, command string, extraArgs []string) int {
if _, ok := m.Config[command]; !ok {
fmt.Fprintf(os.Stderr, "Command %q not defined in module %s\n", command, m.Name)
return 1
}
fmt.Printf("Running %q for module %s...\n\n", command, m.Name)
if err := runModuleSequential(m, command, extraArgs); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
// runGroupParallel launches a same-priority group of modules in the parallel TUI.
// It calls the binary itself as a subprocess per module so each module's full
// execution logic (multiple containers, multiple scripts) runs inside a panel.
func runGroupParallel(modules []*Module, command string, extraArgs []string) bool {
self := selfPath()
var panels []*panel
for _, m := range modules {
parts := append([]string{self, m.Name, command}, extraArgs...)
panels = append(panels, newPanel(m.Name, shellJoin(parts)))
}
return runTUI(panels)
}
// shellJoin builds a shell-safe command string by single-quoting each argument.
func shellJoin(args []string) string {
quoted := make([]string, len(args))
for i, a := range args {
quoted[i] = "'" + strings.ReplaceAll(a, "'", "'\\''") + "'"
}
return strings.Join(quoted, " ")
}
func selfPath() string {
exe, err := os.Executable()
if err != nil {
return os.Args[0]
}
resolved, err := filepath.EvalSymlinks(exe)
if err != nil {
return exe
}
return resolved
}
func showHelp() {
modules, _ := discoverModules()
fmt.Println("Usage:")
fmt.Println(" devops <command> Run command across all modules")
fmt.Println(" devops <module> <command> Run command for a specific module")
fmt.Println(" devops all <command> Run command across all modules (explicit)")
fmt.Println(" devops <module> exec [cmd...] Open interactive shell in module's container")
fmt.Println(" devops <module> shell Alias for exec")
fmt.Println(" devops help Show this help")
fmt.Println(" devops reinstall Download and install the latest release")
fmt.Println()
if len(modules) == 0 {
fmt.Println("No modules found (no .devops/commands.yaml files discovered).")
return
}
fmt.Println("Available modules and commands:")
fmt.Println()
for _, m := range modules {
fmt.Printf(" [%s]\n", m.Name)
for _, cmd := range allCommands([]*Module{m}) {
fmt.Printf(" - %s\n", cmd)
}
fmt.Println()
}
}
func fatalf(format string, args ...any) {
fmt.Fprintf(os.Stderr, "error: "+format+"\n", args...)
os.Exit(1)
}