Skip to content

Commit

Permalink
fixup! Add parsedRemotePersisterConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Feb 12, 2024
1 parent 364bff0 commit 9ea3968
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
32 changes: 16 additions & 16 deletions browser/file_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/grafana/xk6-browser/storage"
)

type parsedRemotePersisterConfig struct {
preSignedURLGetterURL string
headers map[string]string
basePath string
type presignedURLConfig struct {
getterURL string
headers map[string]string
basePath string
}

// newFilePersister will return either a persister that persists file to the local
Expand All @@ -29,15 +29,15 @@ func newFilePersister(envLookup env.LookupFunc) (filePersister, error) {
return nil, fmt.Errorf("parsing %s: %w", env.ScreenshotsOutput, err)
}

return storage.NewRemoteFilePersister(popts.preSignedURLGetterURL, popts.headers, popts.basePath), nil
return storage.NewRemoteFilePersister(popts.getterURL, popts.headers, popts.basePath), nil
}

// parseEnvVar will parse a value such as:
// url=https://127.0.0.1/,basePath=/screenshots,header.1=a,header.2=b
// and return them.
//
//nolint:funlen,cyclop
func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) {
func parseEnvVar(envVarValue string) (presignedURLConfig, error) {
ss := strings.Split(envVarValue, ",")

var (
Expand All @@ -50,7 +50,7 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) {
// on '=' to retrieve the key and value separately.
kv := strings.Split(s, "=")
if len(kv) <= 1 || len(kv) > 2 {
return parsedRemotePersisterConfig{}, fmt.Errorf("format of value must be k=v, received %q", s)
return presignedURLConfig{}, fmt.Errorf("format of value must be k=v, received %q", s)
}

k := kv[0]
Expand All @@ -66,40 +66,40 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) {

hh := strings.Split(k, ".")
if len(hh) <= 1 || len(hh) > 2 {
return parsedRemotePersisterConfig{}, fmt.Errorf("format of header must be header.k=v, received %q", s)
return presignedURLConfig{}, fmt.Errorf("format of header must be header.k=v, received %q", s)
}

k = hh[0]
hk = hh[1]

if hk == "" {
return parsedRemotePersisterConfig{}, fmt.Errorf("empty header key, received %q", s)
return presignedURLConfig{}, fmt.Errorf("empty header key, received %q", s)
}
}

switch k {
case "url":
_, err := url.ParseRequestURI(v)
if err != nil {
return parsedRemotePersisterConfig{}, fmt.Errorf("invalid url %q", s)
return presignedURLConfig{}, fmt.Errorf("invalid url %q", s)
}
preSignedURLGetterURL = v
case "basePath":
basePath = v
case "header":
headers[hk] = hv
default:
return parsedRemotePersisterConfig{}, fmt.Errorf("invalid option %q", k)
return presignedURLConfig{}, fmt.Errorf("invalid option %q", k)
}
}

if preSignedURLGetterURL == "" {
return parsedRemotePersisterConfig{}, fmt.Errorf("missing required url")
return presignedURLConfig{}, fmt.Errorf("missing required url")
}

return parsedRemotePersisterConfig{
preSignedURLGetterURL: preSignedURLGetterURL,
headers: headers,
basePath: basePath,
return presignedURLConfig{
getterURL: preSignedURLGetterURL,
headers: headers,
basePath: basePath,
}, nil
}
34 changes: 17 additions & 17 deletions browser/file_persister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func Test_parseEnvVar(t *testing.T) {
tests := []struct {
name string
envVarValue string
want parsedRemotePersisterConfig
want presignedURLConfig
wantErr string
}{
{
name: "url_headers_basePath",
envVarValue: "url=https://127.0.0.1/,basePath=/screenshots,header.1=a,header.2=b",
want: parsedRemotePersisterConfig{
preSignedURLGetterURL: "https://127.0.0.1/",
basePath: "/screenshots",
want: presignedURLConfig{
getterURL: "https://127.0.0.1/",
basePath: "/screenshots",
headers: map[string]string{
"1": "a",
"2": "b",
Expand All @@ -30,8 +30,8 @@ func Test_parseEnvVar(t *testing.T) {
{
name: "url_headers",
envVarValue: "url=https://127.0.0.1/,header.1=a,header.2=b",
want: parsedRemotePersisterConfig{
preSignedURLGetterURL: "https://127.0.0.1/",
want: presignedURLConfig{
getterURL: "https://127.0.0.1/",
headers: map[string]string{
"1": "a",
"2": "b",
Expand All @@ -41,27 +41,27 @@ func Test_parseEnvVar(t *testing.T) {
{
name: "url",
envVarValue: "url=https://127.0.0.1/",
want: parsedRemotePersisterConfig{
preSignedURLGetterURL: "https://127.0.0.1/",
headers: map[string]string{},
want: presignedURLConfig{
getterURL: "https://127.0.0.1/",
headers: map[string]string{},
},
},
{
name: "url_basePath",
envVarValue: "url=https://127.0.0.1/,basePath=/screenshots",
want: parsedRemotePersisterConfig{
preSignedURLGetterURL: "https://127.0.0.1/",
basePath: "/screenshots",
headers: map[string]string{},
want: presignedURLConfig{
getterURL: "https://127.0.0.1/",
basePath: "/screenshots",
headers: map[string]string{},
},
},
{
name: "empty_basePath",
envVarValue: "url=https://127.0.0.1/,basePath=",
want: parsedRemotePersisterConfig{
preSignedURLGetterURL: "https://127.0.0.1/",
basePath: "",
headers: map[string]string{},
want: presignedURLConfig{
getterURL: "https://127.0.0.1/",
basePath: "",
headers: map[string]string{},
},
},
{
Expand Down

0 comments on commit 9ea3968

Please sign in to comment.