This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
157 lines (137 loc) · 3.54 KB
/
helper.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
154
155
156
157
package fugu
import (
"encoding/json"
"errors"
"fmt"
"github.com/github/hub/github"
"github.com/mattes/go-collect/data"
"github.com/mattes/go-collect/flags"
"gopkg.in/mattes/go-expand-tilde.v1"
"io/ioutil"
"net/http"
"os"
"os/exec"
"sort"
"strings"
"sync"
)
// DockerExec runs a docker command
func DockerExec(cmd string, printCmd bool) {
if printCmd {
fmt.Println(cmd)
}
c := exec.Command("sh", "-c", cmd)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
if err := c.Run(); err != nil {
os.Exit(2)
}
os.Exit(0)
}
func filterDockerFlags(p *data.Data, command string) (*data.Data, error) {
df, err := DockerFlags[command].Keys()
if err != nil {
return nil, err
}
return data.Filter(p, df), nil
}
// buildDockerStr builds the docker command with all options and args
func buildDockerStr(command string, p *data.Data, args ...string) string {
str := []string{}
for _, n := range p.Keys() {
for _, o := range p.GetAll(n) {
if n == "volume" {
o = expandTilde(o)
}
nice := strings.TrimSpace(flags.Nice(n, o))
if nice != "" {
str = append(str, nice)
}
}
}
sort.Sort(sort.StringSlice(str))
str = append([]string{"docker", command}, str...)
str = append(str, args...)
return strings.Join(str, " ")
}
func expandTilde(path string) string {
retval, err := tilde.Expand(path)
if err != nil {
fmt.Println(err)
}
return retval
}
func currentGitBranch() (shortname string, err error) {
if os.Getenv("GOTEST") != "" {
return "current-branch", nil
}
localRepo, err := github.LocalRepo()
if err != nil {
return "", err
}
branch, err := localRepo.CurrentBranch()
if err != nil {
return "", err
}
if branch.ShortName() == "" {
return "", errors.New("empty branch name")
}
return branch.ShortName(), nil
}
type RegistryDockerImage struct {
Name string
Tags []string
}
type RegistryDockerImages []RegistryDockerImage
func (a RegistryDockerImages) Len() int { return len(a) }
func (a RegistryDockerImages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RegistryDockerImages) Less(i, j int) bool { return a[i].Name < a[j].Name }
func ListImages(registry, user, password string) ([]RegistryDockerImage, error) {
// # see https://docs.docker.com/reference/api/registry_api/
// TODO maybe use github.com/docker/docker/registry instead
var data struct {
Results []struct {
Name string
Description string
}
}
// TODO allow http:// too?
if err := registryJsonRequest("GET", "https://"+registry+"/v1/search", user, password, &data); err != nil {
return nil, errors.New("reading from registry failed")
}
result := make(RegistryDockerImages, 0)
var wg sync.WaitGroup
for _, i := range data.Results {
wg.Add(1)
go func(name string) {
defer wg.Done()
r := RegistryDockerImage{}
r.Name = name
var tagData map[string]string
if err := registryJsonRequest("GET", "https://"+registry+"/v1/repositories/"+name+"/tags", user, password, &tagData); err != nil {
return // TODO handle this error?
}
for tag, _ := range tagData {
r.Tags = append(r.Tags, tag)
}
sort.Sort(sort.StringSlice(r.Tags))
result = append(result, r)
}(i.Name)
}
wg.Wait()
sort.Sort(result)
return result, nil
}
func registryJsonRequest(method, url, username, password string, data interface{}) error {
client := &http.Client{}
req, _ := http.NewRequest(method, url, nil)
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return json.Unmarshal(body, &data)
}