aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/go-safetemp/safetemp.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/go-safetemp/safetemp.go')
-rw-r--r--vendor/github.com/hashicorp/go-safetemp/safetemp.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-safetemp/safetemp.go b/vendor/github.com/hashicorp/go-safetemp/safetemp.go
new file mode 100644
index 0000000..c4ae72b
--- /dev/null
+++ b/vendor/github.com/hashicorp/go-safetemp/safetemp.go
@@ -0,0 +1,40 @@
1package safetemp
2
3import (
4 "io"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8)
9
10// Dir creates a new temporary directory that isn't yet created. This
11// can be used with calls that expect a non-existent directory.
12//
13// The directory is created as a child of a temporary directory created
14// within the directory dir starting with prefix. The temporary directory
15// returned is always named "temp". The parent directory has the specified
16// prefix.
17//
18// The returned io.Closer should be used to clean up the returned directory.
19// This will properly remove the returned directory and any other temporary
20// files created.
21//
22// If an error is returned, the Closer does not need to be called (and will
23// be nil).
24func Dir(dir, prefix string) (string, io.Closer, error) {
25 // Create the temporary directory
26 td, err := ioutil.TempDir(dir, prefix)
27 if err != nil {
28 return "", nil, err
29 }
30
31 return filepath.Join(td, "temp"), pathCloser(td), nil
32}
33
34// pathCloser implements io.Closer to remove the given path on Close.
35type pathCloser string
36
37// Close deletes this path.
38func (p pathCloser) Close() error {
39 return os.RemoveAll(string(p))
40}