diff --git a/cmd/mindthegap/flags/custom_flags.go b/cmd/mindthegap/flags/custom_flags.go index af91c258..521d5ab5 100644 --- a/cmd/mindthegap/flags/custom_flags.go +++ b/cmd/mindthegap/flags/custom_flags.go @@ -4,15 +4,13 @@ package flags import ( - "strings" + "fmt" + "net/url" + "path" ) const ( - httpScheme = "http" - httpsScheme = "https" - - httpSchemePrefix = "http://" - httpsSchemePrefix = "https://" + httpScheme = "http" ) type RegistryURI struct { @@ -25,25 +23,28 @@ func (v RegistryURI) String() string { return v.raw } -func (v *RegistryURI) Set(value string) error { +func (v *RegistryURI) Set(value string) (err error) { v.raw = value - v.scheme, v.address = parsePossibleURI(value) + v.scheme, v.address, err = parsePossibleURI(value) - return nil + return } -func parsePossibleURI(value string) (string, string) { - scheme := "" - address := value - if strings.HasPrefix(value, httpSchemePrefix) { - scheme = httpScheme - address = strings.TrimPrefix(value, httpSchemePrefix) - } else if strings.HasPrefix(value, httpsSchemePrefix) { - scheme = httpsScheme - address = strings.TrimPrefix(value, httpsSchemePrefix) +func parsePossibleURI(raw string) (scheme string, address string, err error) { + u, err := url.ParseRequestURI(raw) + if err != nil || u.Host == "" { + // parse again with a scheme to make it a valid URI + u, err = url.ParseRequestURI("dummy://" + raw) + if err != nil { + return "", "", fmt.Errorf("could not parse raw url: %q, error: %w", raw, err) + } + } else { + // only set the scheme when set by the caller + scheme = u.Scheme } - return scheme, address + address = path.Join(u.Host, u.Path) + return } func (v RegistryURI) Scheme() string { diff --git a/cmd/mindthegap/flags/custom_flags_test.go b/cmd/mindthegap/flags/custom_flags_test.go index ad30a636..37d0b5a1 100644 --- a/cmd/mindthegap/flags/custom_flags_test.go +++ b/cmd/mindthegap/flags/custom_flags_test.go @@ -57,7 +57,7 @@ func Test_parsePossibleURI(t *testing.T) { tt := tests[ti] t.Run(tt.name, func(t *testing.T) { t.Parallel() - scheme, address := parsePossibleURI(tt.in) + scheme, address, _ := parsePossibleURI(tt.in) require.Equal(t, tt.expectedScheme, scheme) require.Equal(t, tt.expectedAddress, address) })