Skip to content

Commit

Permalink
added TempFileCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Jul 2, 2024
1 parent 412b2c3 commit b8d04cd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 59 deletions.
18 changes: 18 additions & 0 deletions listdir.go → dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions listdir_test.go → dirs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
19 changes: 0 additions & 19 deletions specialpaths_test.go

This file was deleted.

52 changes: 20 additions & 32 deletions specialpaths.go → temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions temp_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fs
import (
"context"
"io"
"os"
)

// FilesToURLs returns the URLs of a slice of Files.
Expand Down Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"
)

type failContext struct {
Expand Down Expand Up @@ -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")
}

0 comments on commit b8d04cd

Please sign in to comment.