This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
copy, not symlink, local files #844
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package localgetter | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
type LocalGetter struct { | ||
Logger log.Logger | ||
FS afero.Afero | ||
} | ||
|
||
func (g *LocalGetter) GetFiles(ctx context.Context, upstream, savePath string) (string, error) { | ||
debug := level.Debug(g.Logger) | ||
debug.Log("event", "localgetter.GetFiles", "upstream", upstream, "savePath", savePath) | ||
|
||
// Remove the directory because go-getter wants to create it | ||
err := g.FS.RemoveAll(savePath) | ||
if err != nil { | ||
return "", errors.Wrap(err, "remove dir") | ||
} | ||
|
||
err = g.copyDir(ctx, upstream, savePath) | ||
if err != nil { | ||
return "", errors.Wrap(err, "copy files") | ||
} | ||
return savePath, nil | ||
} | ||
|
||
func (g *LocalGetter) copyDir(ctx context.Context, upstream, savePath string) error { | ||
isDir, err := g.FS.IsDir(upstream) | ||
if err != nil { | ||
return errors.Wrapf(err, "check if %s is dir", upstream) | ||
} | ||
if !isDir { | ||
// copy a single file | ||
return g.copyFile(ctx, upstream, savePath, os.FileMode(777)) | ||
} | ||
|
||
files, err := g.FS.ReadDir(upstream) | ||
if err != nil { | ||
return errors.Wrapf(err, "read files in dir %s", upstream) | ||
} | ||
|
||
for _, file := range files { | ||
loopFile := filepath.Join(upstream, file.Name()) | ||
loopDest := filepath.Join(savePath, file.Name()) | ||
if file.IsDir() { | ||
err = g.FS.MkdirAll(loopDest, file.Mode()) | ||
if err != nil { | ||
return errors.Wrapf(err, "create dest dir %s", loopDest) | ||
} | ||
|
||
err = g.copyDir(ctx, loopFile, loopDest) | ||
if err != nil { | ||
return errors.Wrapf(err, "copy dir %s", file.Name()) | ||
} | ||
} else { | ||
err = g.copyFile(ctx, loopFile, loopDest, file.Mode()) | ||
if err != nil { | ||
return errors.Wrapf(err, "copy file %s", file.Name()) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (g *LocalGetter) copyFile(ctx context.Context, upstream, savePath string, mode os.FileMode) error { | ||
saveDir := filepath.Dir(savePath) | ||
exists, err := g.FS.Exists(saveDir) | ||
if err != nil { | ||
return errors.Wrapf(err, "determine if path %s exists", saveDir) | ||
} | ||
if !exists { | ||
err = g.FS.MkdirAll(saveDir, os.ModePerm) | ||
if err != nil { | ||
return errors.Wrapf(err, "create dest dir %s", saveDir) | ||
} | ||
} | ||
|
||
contents, err := g.FS.ReadFile(upstream) | ||
if err != nil { | ||
return errors.Wrapf(err, "read %s file contents", upstream) | ||
} | ||
|
||
err = g.FS.WriteFile(savePath, contents, mode) | ||
return errors.Wrapf(err, "write %s file contents", savePath) | ||
} | ||
|
||
func IsLocalFile(FS *afero.Afero, upstream string) bool { | ||
exists, err := FS.Exists(upstream) | ||
if err != nil { | ||
return false | ||
} | ||
return exists | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package localgetter | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLocalGetter_copyDir(t *testing.T) { | ||
type file struct { | ||
contents []byte | ||
path string | ||
} | ||
tests := []struct { | ||
name string | ||
upstream string | ||
savePath string | ||
inFiles []file | ||
outFiles []file | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "single file", | ||
upstream: "/upstream/file", | ||
savePath: "/save/file", | ||
inFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/file", | ||
}, | ||
}, | ||
outFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/file", | ||
}, | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/save/file", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "single file in dir", | ||
upstream: "/upstream/dir", | ||
savePath: "/save/dir", | ||
inFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/dir/file", | ||
}, | ||
}, | ||
outFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/dir/file", | ||
}, | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/save/dir/file", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "file plus subdirs", | ||
upstream: "/upstream/", | ||
savePath: "/save/", | ||
inFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/dir/file", | ||
}, | ||
{ | ||
contents: []byte("abc xyz"), | ||
path: "/upstream/dir2/file", | ||
}, | ||
{ | ||
contents: []byte("123456789"), | ||
path: "/upstream/file", | ||
}, | ||
}, | ||
outFiles: []file{ | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/upstream/dir/file", | ||
}, | ||
{ | ||
contents: []byte("abc xyz"), | ||
path: "/upstream/dir2/file", | ||
}, | ||
{ | ||
contents: []byte("123456789"), | ||
path: "/upstream/file", | ||
}, | ||
{ | ||
contents: []byte("hello world"), | ||
path: "/save/dir/file", | ||
}, | ||
{ | ||
contents: []byte("abc xyz"), | ||
path: "/save/dir2/file", | ||
}, | ||
{ | ||
contents: []byte("123456789"), | ||
path: "/save/file", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := require.New(t) | ||
|
||
mmFs := afero.Afero{Fs: afero.NewMemMapFs()} | ||
|
||
g := &LocalGetter{ | ||
Logger: log.NewNopLogger(), | ||
FS: mmFs, | ||
} | ||
|
||
for _, file := range tt.inFiles { | ||
req.NoError(mmFs.MkdirAll(filepath.Dir(file.path), os.ModePerm)) | ||
req.NoError(mmFs.WriteFile(file.path, file.contents, os.ModePerm)) | ||
} | ||
|
||
err := g.copyDir(context.Background(), tt.upstream, tt.savePath) | ||
if tt.wantErr { | ||
req.Error(err) | ||
} else { | ||
req.NoError(err) | ||
} | ||
|
||
for _, file := range tt.outFiles { | ||
contents, err := mmFs.ReadFile(file.path) | ||
req.NoError(err) | ||
req.Equal(file.contents, contents, "expected equal contents: expected %q, got %q", string(file.contents), string(contents)) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this comment and block still relevant?