From fe8cc21cba0e8a9b16295a76b73de2fe0242ac1b Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 11 Oct 2023 18:37:24 +0400 Subject: [PATCH] HostDir: support URLs in path (#142) --- pkg/resource/v1/host_dir.go | 32 +++++++++++++++++--------------- pkg/resource/v1/host_dir_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/pkg/resource/v1/host_dir.go b/pkg/resource/v1/host_dir.go index 8ffdea9..5dc24c8 100644 --- a/pkg/resource/v1/host_dir.go +++ b/pkg/resource/v1/host_dir.go @@ -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], diff --git a/pkg/resource/v1/host_dir_test.go b/pkg/resource/v1/host_dir_test.go index 53a6e70..b6123ff 100644 --- a/pkg/resource/v1/host_dir_test.go +++ b/pkg/resource/v1/host_dir_test.go @@ -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) +}