-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-search.go
204 lines (172 loc) · 4.56 KB
/
docker-search.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
"flag"
"os"
"os/user"
"path"
"encoding/json"
// "net/http"
"strconv"
"strings"
)
func help() {
doc := `
docker-search: A better way to search the docker registry
docker-search does a search against the Docker registry, and then pulls
Dockerfiles for matching images, searching inside them for more details.
Examples:
docker-search -filter=ruby:1.9 ffmpeg # Search for images with ffpmeg with ruby 1.9
docker-search -filter=quantal ffmpeg # Search for images with quantal in the Dockerfile
docker-search -dockerfile ffmpeg # print out full dockerfiles
`
fmt.Println( doc )
fmt.Println( "Flags:\n" )
flag.PrintDefaults()
fmt.Println( "\n" )
}
const DEFAULT_CONFIG_FILE = ".docker-search.toml"
func getHomeDir() string {
usr, err := user.Current()
if err != nil {
fmt.Println( "Error: ", err )
}
return usr.HomeDir
}
func getConfigFilePath() string {
return path.Join( getHomeDir(), DEFAULT_CONFIG_FILE )
}
func generateDefaultConfiguration() bool {
configPath := getConfigFilePath()
rv := false
// Generate configuration
if file, err := os.Create( configPath ); nil == err {
def := `
Host = "https://index.docker.io"
Endpoint = "/v1/search"
UpdateCheck = true
`
file.Write( []byte(def) )
rv = true
}
return rv
}
type filters []string
type Value interface {
String() string
Set(string) error
}
func (fs *filters) String() string {
values := ""
for _, s := range *fs {
values += s + ":"
}
return values
}
func (fs *filters) Set(value string) error {
*fs = append(*fs, value)
return nil
}
var genCon *bool
var printDockerfile *bool
// var printInfo *bool
var format *string
var annotation *bool
var filts filters
var verbose *bool
func main() {
genCon = flag.Bool( "generate-config", false, "Generate a new default configuration file" )
printDockerfile = flag.Bool( "dockerfile", false, "Print out dockerfiles" )
// printInfo = flag.Bool( "info", false, "Print out detailed information on the maintainer(s)" )
format = flag.String( "format", "table", "Format the output: table or json" )
annotation = flag.Bool( "annotate", true, "Annotation with Dockerfile information (faster without but no second level search)" )
verbose = flag.Bool( "verbose", false, "Output verbose messages (false)" )
flag.Var( &filts, "filter", "List of filters; you can have more than one (ANDed together)" )
flag.Parse()
if *genCon {
if generateDefaultConfiguration() {
fmt.Println( "Generated configuration file." )
} else {
fmt.Println( "Unable to create configuration file." )
}
// help()
// flag.PrintDefaults()
} else {
if "" == flag.Arg(0) {
help()
} else {
rwc := new(RealWebClient)
c := new(Client)
c.Http = rwc
if c.LoadConfig( getConfigFilePath() ) {
c.Verbose = *verbose
count := 0
ch := make(chan string, 3 )
for _, q := range flag.Args() {
go query( c, q, ch ) // c.Query(
// flag.Arg(0) )
count++
}
if *annotation {
for count > 0 {
queryStr := <- ch
logit( *verbose, "Query response received for: " + queryStr )
count--
}
c.Annotate()
c.Filter( filts )
}
printResults( c )
} else {
fmt.Println( "No configuration file found, use --generate-config" )
}
}
}
}
func logit( verbose bool, args ...string ) {
if verbose {
for _, m := range args {
os.Stderr.Write( []byte(m) )
}
os.Stderr.Write( []byte("\n") )
}
}
func query( c* Client, query string, ch chan string ) {
logit( *verbose, "Query Docker registry for: " + query )
c.Query( query )
ch <- query
}
const tableFmt = "%-30s%-30s"
func formatTable( c* Client ) {
count := strconv.Itoa( len(c.Results) )
fmt.Println( "Found " + count + " results\n" )
for _,e := range c.Results {
fmt.Println( fmt.Sprintf( tableFmt, "Name: ", e.Name ) )
if "" != strings.TrimSpace( e.Description ) {
fmt.Println( fmt.Sprintf( tableFmt, "Description: ", e.Description ) )
}
if "" != strings.TrimSpace( e.Dockerfile ) {
fmt.Println( fmt.Sprintf( "Dockerfile\n\n%s\n\n", e.Dockerfile ) )
} else {
fmt.Println( "No dockerfile found\n" )
}
}
}
func formatJson( c* Client ) {
b, _ := json.Marshal( c.Results )
fmt.Println( string(b) )
}
func printResults( c* Client ) {
if *printDockerfile { // || *printInfo {
switch *format {
case "json": formatJson( c )
default: formatTable( c )
}
} else {
fmt.Println( fmt.Sprintf( tableFmt, "Name", "Description" ) )
fmt.Println( fmt.Sprintf( tableFmt, "----", "-----------" ) )
for _, r := range c.Results {
fmt.Println( fmt.Sprintf( tableFmt, r.Name, r.Description ) )
}
}
}