-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-relay.go
112 lines (89 loc) · 2.51 KB
/
docker-relay.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
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
)
func main() {
// Get the name of the command we are running.
arg0 := filepath.Base(os.Args[0])
// Get the arguments we want to pass to the docker command.
args, err := dockerArgs(arg0, isTTY(), containerID)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Find the docker binary.
binary, err := exec.LookPath(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, "The "+args[0]+" binary was not found.")
os.Exit(1)
}
// Call to docker and pass on the current environment.
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
}
func dockerArgs(arg0 string, tty bool, containerFinder func(string) (string, bool)) ([]string, error) {
vc, err := viperSub(arg0)
if err != nil {
return []string{}, err
}
// Try loading the container id from the configuration or use the command name.
cid := vc.GetString("container")
if cid == "" {
cid = arg0
}
// Find the docker-compose container with the container id.
container, ok := containerFinder(cid)
if !ok {
return []string{}, fmt.Errorf("The container %s is not running", cid)
}
// Parse the viper configuration to a config struct.
conf, err := getConf(vc, container == "")
if err != nil {
return []string{}, fmt.Errorf("Error reading configuration: %e", err)
}
// Evaluate environment variables in the config.
processEnvVar(conf)
if conf.Exec != nil {
args := append(conf.Exec, processedArgs(conf)...)
logDebug(args, conf)
return args, nil
}
// Get a slice of command arguments, if it is just a string it will use that.
cmd := conf.Cmd
// Start assembling the command to run.
args := []string{"docker"}
// If there is no container to execute then we run the image.
if container == "" {
if conf.Image == "" {
return []string{}, fmt.Errorf("Not using docker-compose but no image is configured")
}
args = append(args, "run")
container = conf.Image
} else {
args = append(args, "exec")
}
// Always run as interactive.
args = append(args, "-i")
// If the program is run as a tty, pass the -t flag to docker.
if tty {
args = append(args, "-t")
}
// Add the docker arguments based on the configuration.
args = append(args, conf.options()...)
others := processedArgs(conf)
// Add the container, the command and all the arguments passed to us.
args = append(args, container)
if cmd != nil {
args = append(args, cmd...)
}
args = append(args, others...)
logDebug(args, conf)
return args, nil
}