diff --git a/cmd/root.go b/cmd/root.go index 65262c6de..2786a45ea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -158,19 +158,10 @@ func doImport(src source.Source, s source.Metadata, catalog *pkg.Catalog, d *dis } } - var scheme string - var hostname = appConfig.Anchore.Host - urlFields := strings.Split(hostname, "://") - if len(urlFields) > 1 { - scheme = urlFields[0] - hostname = urlFields[1] - } - c, err := anchore.NewClient(anchore.Configuration{ - Hostname: hostname, + BasePath: appConfig.Anchore.Host, Username: appConfig.Anchore.Username, Password: appConfig.Anchore.Password, - Scheme: scheme, }) if err != nil { return fmt.Errorf("failed to create anchore client: %+v", err) diff --git a/internal/anchore/client.go b/internal/anchore/client.go index 3283baf71..9033d4a26 100644 --- a/internal/anchore/client.go +++ b/internal/anchore/client.go @@ -3,6 +3,7 @@ package anchore import ( "context" "fmt" + "strings" "github.com/anchore/client-go/pkg/external" "github.com/anchore/syft/internal" @@ -10,11 +11,10 @@ import ( ) type Configuration struct { - Hostname string + BasePath string Username string Password string UserAgent string - Scheme string } type Client struct { @@ -29,16 +29,14 @@ func NewClient(cfg Configuration) (*Client, error) { cfg.UserAgent = fmt.Sprintf("%s / %s %s", internal.ApplicationName, versionInfo.Version, versionInfo.Platform) } - if cfg.Scheme == "" { - cfg.Scheme = "https" - } + basePath := ensureURLHasScheme(cfg.BasePath) // we can rely on the built-in URL parsing for the scheme, host, + // port, and path prefix, as long as a scheme is present return &Client{ config: cfg, client: external.NewAPIClient(&external.Configuration{ - Host: cfg.Hostname, + BasePath: basePath, UserAgent: cfg.UserAgent, - Scheme: cfg.Scheme, }), }, nil } @@ -56,3 +54,19 @@ func (c *Client) newRequestContext(parentContext context.Context) context.Contex }, ) } + +func hasScheme(url string) bool { + parts := strings.Split(url, "://") + + return len(parts) > 1 +} + +func ensureURLHasScheme(url string) string { + const defaultScheme = "http" + + if !hasScheme(url) { + return fmt.Sprintf("%s://%s", defaultScheme, url) + } + + return url +} diff --git a/internal/anchore/import.go b/internal/anchore/import.go index a5c32bd38..25bef2297 100644 --- a/internal/anchore/import.go +++ b/internal/anchore/import.go @@ -51,7 +51,7 @@ func importProgress(source string) (*progress.Stage, *progress.Manual) { // nolint:funlen func (c *Client) Import(ctx context.Context, cfg ImportConfig) error { - stage, prog := importProgress(c.config.Hostname) + stage, prog := importProgress(c.config.BasePath) ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30) defer cancel()