clean up client opt

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-06-06 13:00:42 -04:00
parent 3198679b1e
commit 6dbec6401b
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
6 changed files with 63 additions and 16 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]
--timeout value timeout for HTTP requests (default: "1m")
--skip-ping skip pinging the registry while establishing connection
--help, -h show help
--version, -v print the version

View file

@ -1,6 +1,7 @@
package clair
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
@ -26,24 +27,39 @@ func Log(format string, args ...interface{}) {
log.Printf(format, args...)
}
// Opt holds the options for a new clair client.
type Opt struct {
Debug bool
Insecure bool
Timeout time.Duration
}
// New creates a new Clair struct with the given URL and credentials.
func New(url string, debug bool, timeout time.Duration) (*Clair, error) {
func New(url string, opt Opt) (*Clair, error) {
transport := http.DefaultTransport
if opt.Insecure {
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
errorTransport := &ErrorTransport{
Transport: transport,
}
// set the logging
logf := Quiet
if debug {
if opt.Debug {
logf = Log
}
registry := &Clair{
URL: url,
Client: &http.Client{
Timeout: timeout,
Timeout: opt.Timeout,
Transport: errorTransport,
},
Logf: logf,

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/genuinetools/reg/registry"
@ -98,11 +99,18 @@ func main() {
return fmt.Errorf("Attempt to use insecure protocol! Use non-ssl option to force")
}
// Parse the timeout.
timeout, err := time.ParseDuration(c.GlobalString("timeout"))
if err != nil {
return fmt.Errorf("parsing %s as duration failed: %v", c.GlobalString("timeout"), 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"),
Timeout: timeout,
})
return err
}

View file

@ -8,6 +8,7 @@ import (
"net/http"
"regexp"
"strings"
"time"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/manifest/schema2"
@ -42,6 +43,7 @@ type Opt struct {
Insecure bool
Debug bool
SkipPing bool
Timeout time.Duration
}
// New creates a new Registry struct with the given URL and credentials.
@ -91,6 +93,7 @@ func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Op
URL: url,
Domain: reProtocol.ReplaceAllString(url, ""),
Client: &http.Client{
Timeout: opt.Timeout,
Transport: errorTransport,
},
Username: auth.Username,

View file

@ -98,6 +98,11 @@ func main() {
Name: "skip-ping",
Usage: "skip pinging the registry while establishing connection",
},
cli.StringFlag{
Name: "timeout",
Value: "1m",
Usage: "timeout for HTTP requests",
},
}
app.Action = func(c *cli.Context) error {
auth, err := repoutils.GetAuthConfig(c.GlobalString("username"), c.GlobalString("password"), c.GlobalString("registry"))
@ -105,25 +110,29 @@ func main() {
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)
}
// parse the timeout
// Parse the timeout.
timeout, err := time.ParseDuration(c.GlobalString("timeout"))
if err != nil {
logrus.Fatalf("parsing %s as duration failed: %v", c.GlobalString("timeout"), 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"),
Timeout: timeout,
})
if err != nil {
logrus.Fatal(err)
}
// create a clair instance if needed
if c.GlobalString("clair") != "" {
cl, err = clair.New(c.GlobalString("clair"), c.GlobalBool("debug"), timeout)
cl, err = clair.New(c.String("clair"), clair.Opt{
Debug: c.GlobalBool("debug"),
Timeout: timeout,
})
if err != nil {
logrus.Warnf("creation of clair failed: %v", err)
}

View file

@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"time"
"github.com/genuinetools/reg/clair"
"github.com/genuinetools/reg/repoutils"
@ -42,8 +43,17 @@ var vulnsCommand = cli.Command{
return err
}
// Parse the timeout.
timeout, err := time.ParseDuration(c.GlobalString("timeout"))
if err != nil {
return fmt.Errorf("parsing %s as duration failed: %v", c.GlobalString("timeout"), err)
}
// Initialize clair client.
cr, err := clair.New(c.String("clair"), c.GlobalBool("debug"))
cr, err := clair.New(c.String("clair"), clair.Opt{
Debug: c.GlobalBool("debug"),
Timeout: timeout,
})
if err != nil {
return err
}