-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
104 lines (94 loc) · 2.11 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
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
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
)
var defaultArgs = []string{"bash", "-l"}
func main() {
exitCode := 0
defer func() { os.Exit(exitCode) }()
err := parseConfigs()
if err != nil {
printIfErr(err)
exitCode = ExitCodeOnError
return
}
checkDockerSocket()
args := flag.Args()
if len(args) == 0 {
exitCode, err = shell(defaultArgs)
printIfErr(err)
} else {
switch args[0] {
case "shell":
exitCode, err = shell(defaultArgs)
printIfErr(err)
case "config":
showConfig(activeProfile)
case "update":
err = checkUpdate(activeProfile, checkAll(args), true)
if err != nil {
exitCode = ExitCodeOnError
printIfErr(err)
}
case "list":
err = list(context.Background())
if err != nil {
exitCode = ExitCodeOnError
printIfErr(err)
}
case "stop":
err = stop(context.Background(), activeProfile, checkAll(args), false)
if err != nil {
exitCode = ExitCodeOnError
printIfErr(err)
}
case "terminate":
err = stop(context.Background(), activeProfile, checkAll(args), true)
if err != nil {
exitCode = ExitCodeOnError
printIfErr(err)
}
case "--":
fallthrough
case "run":
exitCode, err = shell(args[1:])
printIfErr(err)
default:
exitCode, err = shell(args)
printIfErr(err)
}
}
}
func printIfErr(err error) {
if err == nil {
return
}
pc, filename, line, _ := runtime.Caller(1)
_, err2 := fmt.Fprintf(os.Stderr, "Error in %s[%s:%d] %v\n", runtime.FuncForPC(pc).Name(), filename, line, err)
if err2 != nil {
fmt.Printf("Error encountered printing to stderr: %s\nOriginal Error: %s", err2, err)
}
}
// On docker Desktop 4.18+ there's a high security option to run without admin permissions.
func checkDockerSocket() {
_, ok := os.LookupEnv("DOCKER_HOST")
if !ok {
_, err := os.Stat("/var/run/docker.sock")
if err != nil {
homedir, err := os.UserHomeDir()
printIfErr(err)
if err == nil {
hostPath := filepath.Join(homedir, ".docker/run/docker.sock")
_, err = os.Stat(hostPath)
if err == nil {
os.Setenv("DOCKER_HOST", "unix://"+hostPath)
}
}
}
}
}