-
Notifications
You must be signed in to change notification settings - Fork 4
/
binary.go
60 lines (49 loc) · 1.66 KB
/
binary.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
package script
import (
"fmt"
"strings"
"github.com/k-kinzal/aliases/pkg/aliases/context"
"github.com/k-kinzal/aliases/pkg/aliases/yaml"
"github.com/k-kinzal/aliases/pkg/docker"
"github.com/k-kinzal/aliases/pkg/posix"
)
// DockerBinaryAdapter adapts binary information from spec to a form that can be used in aliases.
type DockerBinaryAdapter yaml.Option
// Image returns image of docker binary.
func (adpt *DockerBinaryAdapter) Image() string {
spec := (*yaml.Option)(adpt)
return spec.Docker.Image
}
// Tag returns tag of docker binary.
func (adpt *DockerBinaryAdapter) Tag() string {
spec := (*yaml.Option)(adpt)
return spec.Docker.Tag
}
// FileName returns filename of docker binary.
func (adpt *DockerBinaryAdapter) FileName() string {
filename := fmt.Sprintf("%s:%s", adpt.Image(), adpt.Tag())
filename = strings.Replace(filename, "/", "-", -1)
filename = strings.Replace(filename, ":", "-", -1)
filename = strings.Replace(filename, ".", "-", -1)
filename = strings.Replace(filename, "_", "-", -1)
return filename
}
// Command returns a command to download the docker binary.
func (adpt *DockerBinaryAdapter) Command(client *docker.Client) *posix.Cmd {
image := fmt.Sprintf("%s:%s", adpt.Image(), adpt.Tag())
cmd := client.Run(image, []string{
"sh",
"-c",
fmt.Sprintf("'cp -av $(which docker) /share/%s'", adpt.FileName()),
}, docker.RunOption{
Entrypoint: (func(str string) *string { return &str })(""),
Volume: []string{
fmt.Sprintf("%s:/share", context.BinaryPath()),
},
})
return cmd
}
// adaptDockerBinary returns DockerBinaryAdapter.
func adaptDockerBinary(spec yaml.Option) *DockerBinaryAdapter {
return (*DockerBinaryAdapter)(&spec)
}