diff --git a/listdir.go b/dirs.go similarity index 80% rename from listdir.go rename to dirs.go index 4dfa4a5..97d0b58 100644 --- a/listdir.go +++ b/dirs.go @@ -3,8 +3,26 @@ package fs import ( "context" "errors" + "os" + "os/user" ) +// HomeDir returns the home directory of the current user. +func HomeDir() File { + u, err := user.Current() + if err != nil { + return InvalidFile + } + return File(u.HomeDir) +} + +// CurrentWorkingDir returns the current working directory of the process. +// In case of an erorr, Exists() of the result File will return false. +func CurrentWorkingDir() File { + cwd, _ := os.Getwd() + return File(cwd) +} + // listDirMaxImpl implements the ListDirMax method functionality by calling listDir. // It returns the passed max number of files or an unlimited number if max is < 0. // FileSystem implementations can use this function to implement ListDirMax, diff --git a/listdir_test.go b/dirs_test.go similarity index 92% rename from listdir_test.go rename to dirs_test.go index 4ce7cff..a225c6d 100644 --- a/listdir_test.go +++ b/dirs_test.go @@ -4,8 +4,14 @@ import ( "context" "reflect" "testing" + + "github.com/stretchr/testify/require" ) +func TestHomeDir(t *testing.T) { + require.True(t, HomeDir().IsDir(), "home directory exists") +} + func Test_listDirMaxImpl(t *testing.T) { ctx := context.Background() errCtx, cancel := context.WithCancel(context.Background()) diff --git a/go.sum b/go.sum index 58ac937..c8c911b 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/go.work.sum b/go.work.sum index 4e9cf2a..61bb411 100644 --- a/go.work.sum +++ b/go.work.sum @@ -9,3 +9,4 @@ golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= diff --git a/specialpaths_test.go b/specialpaths_test.go deleted file mode 100644 index 10d7494..0000000 --- a/specialpaths_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package fs - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestHomeDir(t *testing.T) { - assert.True(t, HomeDir().IsDir(), "home directory exists") -} - -func TestTempDir(t *testing.T) { - assert.True(t, TempDir().IsDir(), "temp directory exists") -} - -func TestExecutable(t *testing.T) { - assert.True(t, Executable().Exists(), "executable file for current process exists") -} diff --git a/specialpaths.go b/temp.go similarity index 58% rename from specialpaths.go rename to temp.go index 227fc16..c55a4be 100644 --- a/specialpaths.go +++ b/temp.go @@ -4,29 +4,13 @@ import ( "crypto/rand" "fmt" "os" - "os/user" + "path" "strings" "time" "github.com/ungerik/go-fs/fsimpl" ) -// HomeDir returns the home directory of the current user. -func HomeDir() File { - u, err := user.Current() - if err != nil { - return InvalidFile - } - return File(u.HomeDir) -} - -// CurrentWorkingDir returns the current working directory of the process. -// In case of an erorr, Exists() of the result File will return false. -func CurrentWorkingDir() File { - cwd, _ := os.Getwd() - return File(cwd) -} - // TempDir returns the temp directory of the operating system func TempDir() File { return File(os.TempDir()) @@ -41,12 +25,13 @@ func TempFile(ext ...string) File { // MakeTempDir makes and returns a new randomly named sub directory in TempDir(). // Example: -// tempDir, err := fs.MakeTempDir() -// if err != nil { -// return err -// } -// defer tempDir.RemoveRecursive() -// doThingsWith(tempDir) +// +// tempDir, err := fs.MakeTempDir() +// if err != nil { +// return err +// } +// defer tempDir.RemoveRecursive() +// doThingsWith(tempDir) func MakeTempDir() (File, error) { name, err := tempDirName() if err != nil { @@ -63,9 +48,10 @@ func MakeTempDir() (File, error) { // MustMakeTempDir makes and returns a new randomly named sub directory in TempDir(). // It panics on errors. // Example: -// tempDir := fs.MustMakeTempDir() -// defer tempDir.RemoveRecursive() -// doThingsWith(tempDir) +// +// tempDir := fs.MustMakeTempDir() +// defer tempDir.RemoveRecursive() +// doThingsWith(tempDir) func MustMakeTempDir() File { dir, err := MakeTempDir() if err != nil { @@ -83,12 +69,14 @@ func tempDirName() (string, error) { return fmt.Sprintf("%s_%X", time.Now().Format("20060102-150405"), randomBytes), nil } -// Executable returns a File for the executable that started the current process. -// It wraps os.Executable, see https://golang.org/pkg/os/#Executable -func Executable() File { - exe, err := os.Executable() +// TempFileCopy copies the provided source file +// to the temp directory of the operating system +// using a random filename with the extension of the source file. +func TempFileCopy(source FileReader) (File, error) { + data, err := source.ReadAll() if err != nil { - return InvalidFile + return InvalidFile, err } - return File(exe) + f := TempFile(path.Ext(source.Name())) + return f, f.WriteAll(data) } diff --git a/temp_test.go b/temp_test.go new file mode 100644 index 0000000..feefcfc --- /dev/null +++ b/temp_test.go @@ -0,0 +1,11 @@ +package fs + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestTempDir(t *testing.T) { + require.True(t, TempDir().IsDir(), "temp directory exists") +} diff --git a/utils.go b/utils.go index 475d6cc..65c1b03 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package fs import ( "context" "io" + "os" ) // FilesToURLs returns the URLs of a slice of Files. @@ -111,3 +112,13 @@ func writeAllContext(ctx context.Context, w io.Writer, data []byte, chunkSize in } return nil } + +// CurrentProcessExecutable returns a File for the executable that started the current process. +// It wraps os.Executable, see https://golang.org/pkg/os/#CurrentProcessExecutable +func CurrentProcessExecutable() File { + exe, err := os.Executable() + if err != nil { + return InvalidFile + } + return File(exe) +} diff --git a/utils_test.go b/utils_test.go index 34b3273..bbc7463 100644 --- a/utils_test.go +++ b/utils_test.go @@ -6,6 +6,8 @@ import ( "errors" "testing" "time" + + "github.com/stretchr/testify/require" ) type failContext struct { @@ -68,3 +70,7 @@ func Test_writeAllContext(t *testing.T) { }) } } + +func TestCurrentProcessExecutable(t *testing.T) { + require.True(t, CurrentProcessExecutable().Exists(), "executable file for current process exists") +}