Skip to content

Commit

Permalink
Merge pull request #1081 from k1LoW/file-scheme
Browse files Browse the repository at this point in the history
Support `file://` scheme.
  • Loading branch information
k1LoW authored Nov 27, 2024
2 parents ecf7547 + 1054c9b commit bc9cfa0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
8 changes: 3 additions & 5 deletions include.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
ipath = c.path
}
// ipath must not be variable expanded. Because it will be impossible to identify the step of the included runbook in case of run failure.
if !hasRemotePrefix(ipath) {
ipath, err = fp(ipath, o.root)
if err != nil {
return err
}
ipath, err = fp(ipath, o.root)
if err != nil {
return err
}

// Store before record
Expand Down
16 changes: 13 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ const (
schemeHttps = "https"
schemeGitHub = "github"
schemeGist = "gist"
schemeFile = "file"
)

const (
prefixHttps = schemeHttps + "://"
prefixGitHub = schemeGitHub + "://"
prefixGist = schemeGist + "://"
prefixFile = schemeFile + "://"
)

// fp returns the absolute path of root+p.
// If path is a remote file, fp returns p.
func fp(p, root string) (string, error) {
if hasRemotePrefix(p) {
return p, nil
}
p = strings.TrimPrefix(p, prefixFile)

if filepath.IsAbs(p) {
cd, err := cacheDir()
if err == nil {
Expand Down Expand Up @@ -170,6 +178,7 @@ func fetchPaths(pathp string) ([]string, error) {
paths = append(paths, p)
default:
// Local file or cache
pp = strings.TrimPrefix(pp, prefixFile)

// Local single file
if !strings.Contains(pattern, "*") {
Expand Down Expand Up @@ -218,6 +227,7 @@ func fetchPath(path string) (string, error) {
// readFile reads single file from local or cache.
// When retrieving a cache file, if the cache file does not exist, re-fetch it.
func readFile(p string) ([]byte, error) {
p = strings.TrimPrefix(p, prefixFile)
fi, err := os.Stat(p)
if err == nil {
cd, err := cacheDir()
Expand Down Expand Up @@ -510,8 +520,8 @@ func readFileViaGitHub(urlstr string) ([]byte, error) {

// splitPathList splits the path list by os.PathListSeparator while keeping schemes.
func splitPathList(pathp string) []string {
rep := strings.NewReplacer(prefixHttps, repKey(prefixHttps), prefixGitHub, repKey(prefixGitHub), prefixGist, repKey(prefixGist))
per := strings.NewReplacer(repKey(prefixHttps), prefixHttps, repKey(prefixGitHub), prefixGitHub, repKey(prefixGist), prefixGist)
rep := strings.NewReplacer(prefixHttps, repKey(prefixHttps), prefixGitHub, repKey(prefixGitHub), prefixGist, repKey(prefixGist), prefixFile, repKey(prefixFile))
per := strings.NewReplacer(repKey(prefixHttps), prefixHttps, repKey(prefixGitHub), prefixGitHub, repKey(prefixGist), prefixGist, repKey(prefixFile), prefixFile)
var listp []string
for _, p := range filepath.SplitList(rep.Replace(pathp)) {
listp = append(listp, per.Replace(p))
Expand All @@ -521,7 +531,7 @@ func splitPathList(pathp string) []string {

func splitKeyAndPath(kp string) (string, string) {
const sep = ":"
if !strings.Contains(kp, sep) || strings.HasPrefix(kp, prefixHttps) || strings.HasPrefix(kp, prefixGitHub) || strings.HasPrefix(kp, prefixGist) {
if !strings.Contains(kp, sep) || strings.HasPrefix(kp, prefixHttps) || strings.HasPrefix(kp, prefixGitHub) || strings.HasPrefix(kp, prefixGist) || strings.HasPrefix(kp, prefixFile) {
return "", kp
}
pair := strings.SplitN(kp, sep, 2)
Expand Down
9 changes: 9 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func TestFp(t *testing.T) {
filepath.Join(globalCacheDir, "path/to/book.yml"),
false,
},
{
"Join root and path with file://",
"file://path/to/book.yml",
false,
false,
filepath.Join(root, "path/to/book.yml"),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -138,6 +146,7 @@ func TestFetchPaths(t *testing.T) {
{"github://k1LoW/runn/testdata/book/book.yml", 1, false},
{"github://k1LoW/runn/testdata/book/runn_*", 4, false},
{"https://raw.githubusercontent.com/k1LoW/runn/main/testdata/book/book.yml", 1, false},
{"file://testdata/book/book.yml", 1, false},
}

if os.Getenv("CI") == "" {
Expand Down
2 changes: 1 addition & 1 deletion testdata/book/http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ labels:
runners:
req:
endpoint: ${TEST_HTTP_ENDPOINT:-https:example.com}
openapi3: ../openapi3.yml
openapi3: file://../openapi3.yml
steps:
getusers:
req:
Expand Down
2 changes: 1 addition & 1 deletion testdata/book/include_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ steps:
loop: 3
desc: include include_b.yml loop 3
include:
path: include_b.yml
path: file://include_b.yml
vars:
filename_main: '{{ vars.filename }}'
filename: '{{ steps.a.filenames[0] }}'
2 changes: 1 addition & 1 deletion testdata/http.yml.runbook.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ labels:
runners:
req:
endpoint: ${TEST_HTTP_ENDPOINT:-https:example.com}
openapi3: ../openapi3.yml
openapi3: file://../openapi3.yml
steps:
- req:
/users:
Expand Down

0 comments on commit bc9cfa0

Please sign in to comment.