HostDir: support URLs in path (#142)

This commit is contained in:
Nikolay Edigaryev 2023-10-11 18:37:24 +04:00 committed by GitHub
parent 13b4e192f0
commit fe8cc21cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 16 deletions

View File

@ -15,11 +15,24 @@ type HostDir struct {
}
func NewHostDirFromString(s string) (HostDir, error) {
parts := strings.Split(s, ":")
var readOnly bool
if len(parts) > 3 {
return HostDir{}, fmt.Errorf("%w: hostDir specification can only contain 3 parts at max",
ErrInvalidHostDir)
// Detect read-only (":ro") modifier
// and remove it from the string
if strings.HasSuffix(s, ":ro") {
s = strings.TrimSuffix(s, ":ro")
readOnly = true
}
// Limit the maximum number of splits to 2
// to support "http{,s}://..." paths[1]
// ^
// [1]: https://github.com/cirruslabs/tart/pull/620
parts := strings.SplitN(s, ":", 2)
if len(parts) < 2 {
return HostDir{}, fmt.Errorf("%w: hostDir specification needs to contain a name and a path "+
"separated by a colon (\":\")", ErrInvalidHostDir)
}
if parts[0] == "" {
@ -29,17 +42,6 @@ func NewHostDirFromString(s string) (HostDir, error) {
return HostDir{}, fmt.Errorf("%w: path cannot be empty", ErrInvalidHostDir)
}
var readOnly bool
if len(parts) == 3 {
if parts[2] == "ro" {
readOnly = true
} else {
return HostDir{}, fmt.Errorf("%w: hostDir's third part can only be \"ro\", got %q",
ErrInvalidHostDir, parts[2])
}
}
return HostDir{
Name: parts[0],
Path: parts[1],

View File

@ -1,12 +1,13 @@
package v1_test
import (
"fmt"
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"github.com/stretchr/testify/require"
"testing"
)
func TestNewHostDirFromString(t *testing.T) {
func TestHostDir(t *testing.T) {
hostDir, err := v1.NewHostDirFromString("large-project:/Users/ci/src/www:ro")
require.NoError(t, err)
require.EqualValues(t, v1.HostDir{
@ -23,3 +24,26 @@ func TestHostDirString(t *testing.T) {
ReadOnly: true,
}.String())
}
// TestHostDirWithArchiveURL ensures that Orchard supports "http{,s}://" paths[1].
//
// [1]: https://github.com/cirruslabs/tart/pull/620
func TestHostDirWithArchiveURL(t *testing.T) {
const hostDirRaw = "ghar:https://example.com/archive.tar.gz"
hostDir, err := v1.NewHostDirFromString(hostDirRaw)
require.NoError(t, err)
require.EqualValues(t, v1.HostDir{
Name: "ghar",
Path: "https://example.com/archive.tar.gz",
ReadOnly: false,
}, hostDir)
hostDirReadOnly, err := v1.NewHostDirFromString(fmt.Sprintf("%s:ro", hostDirRaw))
require.NoError(t, err)
require.EqualValues(t, v1.HostDir{
Name: "ghar",
Path: "https://example.com/archive.tar.gz",
ReadOnly: true,
}, hostDirReadOnly)
}