-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.go
153 lines (127 loc) · 3.09 KB
/
pack.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/docker/distribution/reference"
regclient "github.com/docker/distribution/registry/client"
"github.com/mitchellh/go-homedir"
"github.com/offers/dope/out"
)
type Pack struct {
Name string `json:"name"`
Repo string `json:"repo"`
DockerArgs string `json:"dockerArgs"`
Cmd string `json:"cmd"`
Tag string `json:"tag"`
ImageId string `json:"ImageId"`
}
func newDefaultPack(repo string, tag string, name string) (*Pack, error) {
p := &Pack{
Name: name,
Repo: repo,
Tag: tag,
DockerArgs: "-it",
}
return p, nil
}
func repoTags(repo string) ([]string, error) {
ref, err := reference.ParseNamed(repo)
if err != nil {
return []string{}, err
}
url := fmt.Sprintf("https://%s/", reference.Domain(ref))
name, err := reference.WithName(reference.Path(ref))
if err != nil {
return []string{}, err
}
r, err := regclient.NewRepository(nil, name, url, http.DefaultTransport)
if err != nil {
return []string{}, err
}
return r.Tags(nil).All(nil)
}
func highTag(repo string) (string, error) {
tags, err := repoTags(repo)
if err != nil {
return "", err
}
highTag := ""
for _, t := range tags {
match, _ := regexp.MatchString(`\d\.\d\.\d`, t)
if match && (highTag == "" || compareTags(t, highTag) == 1) {
highTag = t
}
}
return highTag, nil
}
// TODO handle versions starting with v, e.g. v1.0.0
// TODO use 'latest' if no semantic tags, and check image hash for update
// TODO add error return
func (p *Pack) checkForUpdate() (avail bool, tag string) {
highTag, err := highTag(p.Repo)
if err != nil {
return false, ""
}
if "" == highTag {
out.Notice("No semantic tags in repo")
return false, ""
}
if compareTags(highTag, p.Tag) == 1 {
return true, highTag
}
return false, ""
}
// Compare 2 semantic version tags (e.g. 1.0.0)
// Returns 1 if t1 is a higher version
// Returns -1 if t2 is a higher version
// Returns 0 if t1 == t2
func compareTags(t1 string, t2 string) int {
nums1 := strings.Split(t1, ".")
nums2 := strings.Split(t2, ".")
l := len(nums1)
if len(nums2) < l {
l = len(nums2)
}
for i := 0; i < l; i++ {
v1, _ := strconv.ParseInt(nums1[i], 0, 64)
v2, _ := strconv.ParseInt(nums2[i], 0, 64)
if v1 > v2 {
return 1
}
if v1 < v2 {
return -1
}
}
return 0
}
func (p *Pack) stubPath() string {
home, err := homedir.Dir()
if err != nil {
panic("Couldn't determine user home directory")
}
binDir := filepath.Join(home, ".dope", "bin")
os.MkdirAll(binDir, 0755)
return filepath.Join(binDir, p.Name)
}
func (p *Pack) removeStub() error {
return os.Remove(p.stubPath())
}
func (p *Pack) writeStub() error {
dockerBin, err := exec.LookPath("docker")
if err != nil {
return err
}
dockerCmd := fmt.Sprintf("%s run --rm %s %s:%s", dockerBin, p.DockerArgs, p.Repo, p.Tag)
if "" != p.Cmd {
dockerCmd += " " + p.Cmd
}
s := fmt.Sprintf("#!/bin/bash\ndope check -q %s\nexec %s $@", p.Name, dockerCmd)
return ioutil.WriteFile(p.stubPath(), []byte(s), 0755)
}