/
drc.go
129 lines (124 loc) · 3.76 KB
/
drc.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
package main
import (
"fmt"
"github.com/pbergman/docker-registry-cli/api"
"github.com/pbergman/docker-registry-cli/config"
"github.com/pbergman/docker-registry-cli/helpers"
"github.com/pbergman/docker-registry-cli/http"
"github.com/pbergman/docker-registry-cli/logger"
"io"
"os"
"os/exec"
"time"
)
func init() {
api.ApiCheck()
}
func main() {
switch config.Config.Command {
case config.DELETE:
api.Delete(
*config.Config.Input["delete.repository"].(*string),
*config.Config.Input["delete.tag"].(*string),
*config.Config.Input["delete.dry"].(*bool),
)
if true != *config.Config.Input["delete.dry"].(*bool) {
fmt.Printf("Image %s:%s removed\n", *config.Config.Input["delete.repository"].(*string), *config.Config.Input["delete.tag"].(*string))
}
case config.TOKEN:
token, err := config.Config.TokenManager.GetToken(&http.AuthChallenge{
Service: *config.Config.Input["token.service"].(*string),
Realm: *config.Config.Input["token.realm"].(*string),
Scope: *config.Config.Input["token.scope"].(*string),
})
logger.Logger.CheckError(err)
fmt.Println("")
fmt.Println("Expires: " + token.ExpireTime().Format("2006-01-02 15:04:05 UTC"))
fmt.Println(token.Token)
fmt.Println("")
case config.LIST:
table := helpers.NewTable("REPOSITORY", "TAG", "DATE", "AUTHOR", "SIZE(MB)")
list := api.GetList()
list.Sort()
for _, info := range *list {
for _, tag := range info.Tags {
manifest := api.GetManifest(info.Name, tag, true)
author := ""
for _, history := range manifest.History {
if value, exist := history.Unpack()["author"]; exist {
author = value.(string)
break
}
}
time := time.Time{}
time.UnmarshalText([]byte(manifest.History[len(manifest.History)-1].Unpack()["created"].(string)))
table.AddRow(
info.Name,
tag,
time.Format("2006-01-02 15:04:05.000000"),
author,
api.GetSize(info.Name, tag)/(1024*1024),
)
}
}
table.Print()
case config.SIZE:
size := api.GetSize(
*config.Config.Input["size.repository"].(*string),
*config.Config.Input["size.tag"].(*string),
)
fmt.Printf("\n%dMB\n", size/(1024*1024))
case config.HISTORY:
manifest := api.GetManifest(
*config.Config.Input["history.repository"].(*string),
*config.Config.Input["history.tag"].(*string),
true,
)
if config.Config.Verbose { // Print json set to less
path, err := exec.LookPath("less")
logger.Logger.CheckError(err)
cmd := exec.Command(path, "-egR")
stdin, err := cmd.StdinPipe()
logger.Logger.CheckError(err)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
for i := len(manifest.History) - 1; i >= 0; i-- {
io.WriteString(stdin, string(manifest.History[i].Print().Bytes()))
io.WriteString(stdin, "\n")
}
stdin.Close()
err = cmd.Run()
logger.Logger.CheckError(err)
} else { // Print only date and command
for i := len(manifest.History) - 1; i >= 0; i-- {
data := manifest.History[i].Unpack()
cmd := data["container_config"].(map[string]interface{})["Cmd"]
if cmd != nil {
line := ""
for _, part := range cmd.([]interface{}) {
line += part.(string) + " "
}
time := time.Time{}
time.UnmarshalText([]byte(data["created"].(string)))
fmt.Printf("[%s] %s\n", time.Format("2006-01-02 15:04:05.000000"), line)
}
}
}
case config.REPOSITORIES:
repositories := api.GetRepositories()
table := helpers.NewTable("REPOSITORIES")
for _, name := range repositories.Images {
table.AddRow(name)
}
table.Print()
case config.TAGS:
tags := api.GetTags(*config.Config.Input["tag.repository"].(*string))
if tags != nil {
table := helpers.NewTable("TAGS(" + *config.Config.Input["tag.repository"].(*string) + ")")
for _, name := range tags.Tags {
table.AddRow(name)
}
table.Print()
}
}
}