This repository was archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibcmd.go
More file actions
59 lines (50 loc) · 1.36 KB
/
libcmd.go
File metadata and controls
59 lines (50 loc) · 1.36 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
package libcmd
import (
"reflect"
"github.com/replicatedcom/libcmd/command"
log "github.com/Sirupsen/logrus"
"github.com/fsouza/go-dockerclient"
)
var (
globalDockerClient *docker.Client
config command.CmdConfig
cmdConfigDefaultOpts = map[string]string{
"CommandsDir": "/root/commands",
"DockerEndpoint": "unix:///var/run/docker.sock",
"ContainerRepository": "freighterio/cmd",
"ContainerTag": "latest",
}
)
func InitCmdContainer(opts map[string]string) {
config = command.CmdConfig{}
for key, dflt := range cmdConfigDefaultOpts {
field := reflect.ValueOf(&config).Elem().FieldByName(key)
if value, ok := opts[key]; ok {
field.SetString(value)
} else {
field.SetString(dflt)
}
}
client, err := docker.NewClient(config.DockerEndpoint)
if err != nil {
log.Fatal(err)
}
globalDockerClient = client
if err := command.PullImage(globalDockerClient, config.ContainerRepository, config.ContainerTag); err != nil {
log.Fatal(err)
}
}
func RunCommand(op string, args ...string) ([]string, error) {
goCmd, err := command.NewGoCmd(op, config, globalDockerClient)
if err == nil {
return goCmd.Run(args...)
}
if err != command.ErrCommandNotFound {
return nil, err
}
containerCmd, err := command.NewContainerCmd(op, config, globalDockerClient)
if err == nil {
return containerCmd.Run(args...)
}
return nil, err
}