Merge branch 'pr-56'

* pr-56:
  Add option to skip ping on creating registry client
This commit is contained in:
Jess Frazelle 2018-06-06 12:49:29 -04:00
commit 48a99e9644
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
4 changed files with 44 additions and 38 deletions

View file

@ -61,6 +61,7 @@ GLOBAL OPTIONS:
--username value, -u value username for the registry
--password value, -p value password for the registry
--registry value, -r value URL to the private registry (ex. r.j3ss.co) (default: "https://registry-1.docker.io") [$REG_REGISTRY]
--skip-ping skip pinging the registry while establishing connection
--help, -h show help
--version, -v print the version
```

21
main.go
View file

@ -53,6 +53,10 @@ func main() {
Value: repoutils.DefaultDockerRegistry,
EnvVar: "REG_REGISTRY",
},
cli.BoolFlag{
Name: "skip-ping",
Usage: "skip pinging the registry while establishing connection",
},
}
app.Commands = []cli.Command{
@ -89,17 +93,12 @@ func main() {
return fmt.Errorf("Attempt to use insecure protocol! Use non-ssl option to force")
}
// create the registry client
if c.GlobalBool("insecure") {
r, err = registry.NewInsecure(auth, c.GlobalBool("debug"))
if err != nil {
return err
}
return
}
r, err = registry.New(auth, c.GlobalBool("debug"))
// Create the registry client.
r, err = registry.New(auth, registry.Opt{
Insecure: c.GlobalBool("insecure"),
Debug: c.GlobalBool("debug"),
SkipPing: c.GlobalBool("skip-ping"),
})
return err
}

View file

@ -37,26 +37,29 @@ func Log(format string, args ...interface{}) {
log.Printf(format, args...)
}
// Opt holds the options for a new registry.
type Opt struct {
Insecure bool
Debug bool
SkipPing bool
}
// New creates a new Registry struct with the given URL and credentials.
func New(auth types.AuthConfig, debug bool) (*Registry, error) {
func New(auth types.AuthConfig, opt Opt) (*Registry, error) {
transport := http.DefaultTransport
return newFromTransport(auth, transport, debug)
}
// NewInsecure creates a new Registry struct with the given URL and credentials,
// using a http.Transport that will not verify an SSL certificate.
func NewInsecure(auth types.AuthConfig, debug bool) (*Registry, error) {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return newFromTransport(auth, transport, debug)
return newFromTransport(auth, transport, opt)
}
func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, debug bool) (*Registry, error) {
func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Opt) (*Registry, error) {
url := strings.TrimSuffix(auth.ServerAddress, "/")
if !reProtocol.MatchString(url) {
@ -80,7 +83,7 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, debug
// set the logging
logf := Quiet
if debug {
if opt.Debug {
logf = Log
}
@ -95,8 +98,10 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, debug
Logf: logf,
}
if err := registry.Ping(); err != nil {
return nil, err
if !opt.SkipPing {
if err := registry.Ping(); err != nil {
return nil, err
}
}
return registry, nil

View file

@ -94,6 +94,10 @@ func main() {
Name: "clair",
Usage: "url to clair instance",
},
cli.BoolFlag{
Name: "skip-ping",
Usage: "skip pinging the registry while establishing connection",
},
}
app.Action = func(c *cli.Context) error {
auth, err := repoutils.GetAuthConfig(c.GlobalString("username"), c.GlobalString("password"), c.GlobalString("registry"))
@ -101,17 +105,14 @@ func main() {
logrus.Fatal(err)
}
// create the registry client
if c.GlobalBool("insecure") {
r, err = registry.NewInsecure(auth, c.GlobalBool("debug"))
if err != nil {
logrus.Fatal(err)
}
} else {
r, err = registry.New(auth, c.GlobalBool("debug"))
if err != nil {
logrus.Fatal(err)
}
// Create the registry client.
r, err = registry.New(auth, registry.Opt{
Insecure: c.GlobalBool("insecure"),
Debug: c.GlobalBool("debug"),
SkipPing: c.GlobalBool("skip-ping"),
})
if err != nil {
logrus.Fatal(err)
}
// create a clair instance if needed