util: Add scheme and path support to our fs util struct

This lets us have a custom URI when wrapping an Afero.Fs interface.
This commit is contained in:
James Shubin
2024-02-21 17:12:17 -05:00
parent 15fa6b82a5
commit 73ae197d20
2 changed files with 17 additions and 8 deletions

View File

@@ -157,7 +157,7 @@ func TestCopyDiskToFs1(t *testing.T) {
mmFs := afero.NewMemMapFs() mmFs := afero.NewMemMapFs()
afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil
fs := &AferoFs{afs} fs := &AferoFs{Afero: afs}
if err := CopyDiskToFs(fs, dir+f+"/", "/", false); err != nil { if err := CopyDiskToFs(fs, dir+f+"/", "/", false); err != nil {
t.Errorf("copying to fs failed: %+v", err) t.Errorf("copying to fs failed: %+v", err)
@@ -218,7 +218,7 @@ func TestCopyDiskToFs2(t *testing.T) {
mmFs := afero.NewMemMapFs() mmFs := afero.NewMemMapFs()
afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil
fs := &AferoFs{afs} fs := &AferoFs{Afero: afs}
src := dir + f + "/" src := dir + f + "/"
dst := "/dest/" dst := "/dest/"
@@ -282,7 +282,7 @@ func TestCopyDiskContentsToFs1(t *testing.T) {
mmFs := afero.NewMemMapFs() mmFs := afero.NewMemMapFs()
afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil
fs := &AferoFs{afs} fs := &AferoFs{Afero: afs}
if err := CopyDiskContentsToFs(fs, dir+f+"/", "/", false); err != nil { if err := CopyDiskContentsToFs(fs, dir+f+"/", "/", false); err != nil {
t.Errorf("copying to fs failed: %+v", err) t.Errorf("copying to fs failed: %+v", err)

View File

@@ -25,13 +25,22 @@ import (
// AferoFs is a simple wrapper to a file system to be used for standalone // AferoFs is a simple wrapper to a file system to be used for standalone
// deploys. This is basically a pass-through so that we fulfill the same // deploys. This is basically a pass-through so that we fulfill the same
// interface that the deploy mechanism uses. To use this, wrap it with the // interface that the deploy mechanism uses. If you give it Scheme and Path
// implied field name, which will prevent `go vet` warnings, eg: // fields it will use those to build the URI. NOTE: This struct is here, since I
// `fs := &util.AferoFs{Afero: afs}`. NOTE: This struct is here, since I don't // don't know where else to put it for now.
// know where else to put it for now.
type AferoFs struct { type AferoFs struct {
*afero.Afero *afero.Afero
Scheme string
Path string
} }
// URI returns the unique URI of this filesystem. It returns the root path. // URI returns the unique URI of this filesystem. It returns the root path.
func (obj *AferoFs) URI() string { return fmt.Sprintf("%s://"+"/", obj.Name()) } func (obj *AferoFs) URI() string {
if obj.Scheme != "" {
// if obj.Path is not empty and doesn't start with a slash, then
// the first chunk will dissappear when being parsed with stdlib
return obj.Scheme + "://" + obj.Path
}
return fmt.Sprintf("%s://"+"/", obj.Name()) // old
}