Skip to content

Commit

Permalink
Add NewFilePersister
Browse files Browse the repository at this point in the history
This method will help determine whether to use the LocalFilePersister
or the RemoteFilePersister. It mainly depends on whether a valid
K6_BROWSER_SCREENSHOTS_OUTPUT env var has been setup.
  • Loading branch information
ankur22 committed Feb 1, 2024
1 parent a730696 commit 660e5e9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions storage/file_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"path/filepath"
"strings"
"time"

"github.com/grafana/xk6-browser/env"
)

// FilePersister is the type that all file persisters must implement. It's job is
Expand All @@ -21,6 +23,23 @@ type FilePersister interface {
Persist(ctx context.Context, path string, data io.Reader) (err error)
}

// NewFilePersister will return either a LocalFilePersister or a RemoteFilePersister
// depending on whether the K6_BROWSER_SCREENSHOTS_OUTPUT env var is setup with the
// correct configs.
func NewFilePersister(envLookup env.LookupFunc) (FilePersister, error) {
envVar, ok := envLookup(env.ScreenshotsOutput)
if !ok || envVar == "" {
return &LocalFilePersister{}, nil
}

u, b, h, err := parseEnvVar(envVar)
if err != nil {
return nil, fmt.Errorf("parsing %s: %w", env.ScreenshotsOutput, err)
}

return NewRemoteFilePersister(u, h, b), 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.
Expand Down

0 comments on commit 660e5e9

Please sign in to comment.