-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
104 lines (94 loc) · 2.44 KB
/
main.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
package main
import (
"fmt"
"github.com/dustin/go-humanize"
"os"
"strings"
"text/tabwriter"
)
var Quiet = false
func fatal(msg error) {
fmt.Fprintf(os.Stderr, "%s\n", msg)
os.Exit(1)
}
func log(format string, args ...interface{}) {
if Quiet == true {
return
}
format = fmt.Sprintf("# %s\n", format)
fmt.Fprintf(os.Stderr, format, args...)
}
func readUserCredentials() (string, string) {
log("Reading user/passwd from env var \"USER_CREDS\"")
v := os.Getenv("USER_CREDS")
if v == "" {
log("No password provided, disabling auth")
return "", ""
}
user := strings.SplitN(v, ":", 2)
if len(user) < 2 {
return user[0], ""
}
return user[0], user[1]
}
func initRegistry(config *Config, reposName string) (*Registry, string) {
registry, e := NewRegistry(config.IndexDomain, config.RegistryDomain)
if e != nil {
fatal(e)
}
registry.Logger = log
user, passwd := readUserCredentials()
log("Getting token from %s", config.IndexDomain)
token, e := registry.GetToken(user, passwd, reposName)
if e != nil {
fatal(e)
}
return registry, token
}
func CmdInfo(config *Config, args []string) {
registry, token := initRegistry(config, args[0])
tags, err := registry.ReposTags(token, args[0])
if err != nil {
fatal(err)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Println("- Repository:", args[0])
fmt.Println("- Tags:")
for k, v := range tags {
fmt.Fprintf(w, "\t%s\t%s\n", k, v)
}
w.Flush()
}
func CmdLayerInfo(config *Config, args []string) {
registry, token := initRegistry(config, args[0])
info, err := registry.LayerJson(token, args[1])
if err != nil {
fatal(err)
}
ancestry, err := registry.LayerAncestry(token, args[1])
if err != nil {
fatal(err)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintf(w, "- Id\t%s\n", info.Id)
fmt.Fprintf(w, "- Parent\t%s\n", info.Parent)
fmt.Fprintf(w, "- Size\t%s\n", humanize.Bytes(uint64(info.Size)))
fmt.Fprintf(w, "- Created\t%s\n", info.Created)
fmt.Fprintf(w, "- DockerVersion\t%s\n", info.DockerVersion)
fmt.Fprintf(w, "- Author\t%s\n", info.Author)
fmt.Fprintf(w, "- Ancestry:")
for _, id := range *ancestry {
fmt.Fprintf(w, "\t%s\n", id)
}
w.Flush()
}
func CmdCurlme(config *Config, args []string) {
registry, token := initRegistry(config, args[0])
fmt.Printf("curl -i --location-trusted -I -X GET -H \"Authorization: Token %s\" %s/v1/images/%s/layer\n",
token, registry.RegistryHost, args[1])
}
func main() {
OptParse()
}