-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (67 loc) · 1.44 KB
/
main.go
File metadata and controls
80 lines (67 loc) · 1.44 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
package main
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
l "github.com/sprisa/x/log"
)
func main() {
if len(os.Args) < 2 {
printAppHelp()
return
}
cmd := os.Args[1]
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
err := handleCmd(ctx, cmd)
if err != nil {
l.Log.Error().Msg(err.Error())
defer os.Exit(1)
}
}
func handleCmd(ctx context.Context, cmd string) error {
switch cmd {
case "up", "down", "restart":
if len(os.Args) >= 3 {
arg := os.Args[2]
if arg == "help" {
printCmdHelp(cmd)
return nil
}
}
return runComposeCmd(ctx, cmd, os.Args[2:])
case "plan":
return runPlanCmd(ctx)
case "setup":
return runSetup(ctx)
case "help":
printAppHelp()
return nil
default:
return fmt.Errorf("command `%s` not supported", cmd)
}
}
func printAppHelp() {
fmt.Print(`Composure - Calm docker compose deployments
Usage: composure <command>
Commands:
up - Start services
down - Stop services
restart - Restart services
plan - Show deployment plan
setup - Setup shared volumes (NFS)
help - Show this help message
`)
}
func printCmdHelp(cmd string) {
shell := exec.Command("docker", "compose", cmd, "--help")
out, _ := shell.CombinedOutput()
firstNewline := max(bytes.IndexRune(out, '\n'), 0)
fmt.Printf(`Usage: composure %s [docker compose options]
%s
`, cmd, bytes.TrimSpace(out[firstNewline:]))
}