mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
Lean on built-in URL parsing to enable path prefix
Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
parent
b22fd987db
commit
6d730d24dd
11
cmd/root.go
11
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{
|
c, err := anchore.NewClient(anchore.Configuration{
|
||||||
Hostname: hostname,
|
BasePath: appConfig.Anchore.Host,
|
||||||
Username: appConfig.Anchore.Username,
|
Username: appConfig.Anchore.Username,
|
||||||
Password: appConfig.Anchore.Password,
|
Password: appConfig.Anchore.Password,
|
||||||
Scheme: scheme,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create anchore client: %+v", err)
|
return fmt.Errorf("failed to create anchore client: %+v", err)
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package anchore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/anchore/client-go/pkg/external"
|
"github.com/anchore/client-go/pkg/external"
|
||||||
"github.com/anchore/syft/internal"
|
"github.com/anchore/syft/internal"
|
||||||
@ -10,11 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
Hostname string
|
BasePath string
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
UserAgent string
|
UserAgent string
|
||||||
Scheme string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
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)
|
cfg.UserAgent = fmt.Sprintf("%s / %s %s", internal.ApplicationName, versionInfo.Version, versionInfo.Platform)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Scheme == "" {
|
basePath := ensureURLHasScheme(cfg.BasePath) // we can rely on the built-in URL parsing for the scheme, host,
|
||||||
cfg.Scheme = "https"
|
// port, and path prefix, as long as a scheme is present
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
client: external.NewAPIClient(&external.Configuration{
|
client: external.NewAPIClient(&external.Configuration{
|
||||||
Host: cfg.Hostname,
|
BasePath: basePath,
|
||||||
UserAgent: cfg.UserAgent,
|
UserAgent: cfg.UserAgent,
|
||||||
Scheme: cfg.Scheme,
|
|
||||||
}),
|
}),
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
|||||||
@ -51,7 +51,7 @@ func importProgress(source string) (*progress.Stage, *progress.Manual) {
|
|||||||
|
|
||||||
// nolint:funlen
|
// nolint:funlen
|
||||||
func (c *Client) Import(ctx context.Context, cfg ImportConfig) error {
|
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)
|
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user