generated from d2iq-archive/golang-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support passing optional scheme in --to-registry (#252)
Co-authored-by: Jimmi Dyson <jimmidyson@gmail.com>
- Loading branch information
1 parent
35a0746
commit 6983970
Showing
8 changed files
with
155 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2021 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package flags | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"path" | ||
) | ||
|
||
const ( | ||
httpScheme = "http" | ||
) | ||
|
||
type RegistryURI struct { | ||
raw string | ||
scheme string | ||
address string | ||
} | ||
|
||
func (v RegistryURI) String() string { | ||
return v.raw | ||
} | ||
|
||
func (v *RegistryURI) Set(value string) (err error) { | ||
v.raw = value | ||
v.scheme, v.address, err = parsePossibleURI(value) | ||
|
||
return | ||
} | ||
|
||
func parsePossibleURI(raw string) (scheme, 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("unused://" + 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 | ||
} | ||
|
||
address = path.Join(u.Host, u.Path) | ||
return | ||
} | ||
|
||
func (v RegistryURI) Scheme() string { | ||
return v.scheme | ||
} | ||
|
||
func (v RegistryURI) Address() string { | ||
return v.address | ||
} | ||
|
||
func (*RegistryURI) Type() string { | ||
return "string" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2021 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_parsePossibleURI(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
in string | ||
expectedScheme string | ||
expectedAddress string | ||
}{ | ||
{ | ||
name: "no scheme", | ||
in: "0.0.0.0:5000", | ||
expectedAddress: "0.0.0.0:5000", | ||
}, | ||
{ | ||
name: "http scheme", | ||
in: "http://0.0.0.0:5000", | ||
expectedScheme: "http", | ||
expectedAddress: "0.0.0.0:5000", | ||
}, | ||
{ | ||
name: "https scheme", | ||
in: "https://0.0.0.0:5000", | ||
expectedScheme: "https", | ||
expectedAddress: "0.0.0.0:5000", | ||
}, | ||
{ | ||
name: "no scheme with path", | ||
in: "0.0.0.0:5000/dkp", | ||
expectedAddress: "0.0.0.0:5000/dkp", | ||
}, | ||
{ | ||
name: "http scheme with path", | ||
in: "https://0.0.0.0:5000/dkp", | ||
expectedScheme: "https", | ||
expectedAddress: "0.0.0.0:5000/dkp", | ||
}, | ||
{ | ||
name: "https scheme with path", | ||
in: "https://0.0.0.0:5000/dkp", | ||
expectedScheme: "https", | ||
expectedAddress: "0.0.0.0:5000/dkp", | ||
}, | ||
} | ||
for ti := range tests { | ||
tt := tests[ti] | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
scheme, address, _ := parsePossibleURI(tt.in) | ||
require.Equal(t, tt.expectedScheme, scheme) | ||
require.Equal(t, tt.expectedAddress, address) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2021 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package flags | ||
|
||
// SkipTLSVerify returns true if registrySkipTLSVerify is true | ||
// or registryURI URI is http. | ||
func SkipTLSVerify(registrySkipTLSVerify bool, registryURI RegistryURI) bool { | ||
return registrySkipTLSVerify || registryURI.Scheme() == httpScheme | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.