-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.go
43 lines (35 loc) · 808 Bytes
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"crypto/rand"
"encoding/hex"
"io"
"os"
"github.com/pkg/errors"
)
func randomString() string {
b := make([]byte, 12)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return hex.EncodeToString(b)
}
func tmpfile(path, contents string, perms uint) (*os.File, error) {
if _, err := os.Stat(path); err == nil {
err := os.Remove(path)
if err != nil {
return nil, errors.Wrap(err, "could not delete old file")
}
}
file, err := os.OpenFile(
os.ExpandEnv(path),
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
os.FileMode(perms))
if err != nil {
return nil, errors.Wrap(err, "could not create credentials file")
}
if _, err := io.WriteString(file, contents); err != nil {
return nil, errors.Wrap(err, "could not write temp file contents")
}
return file, nil
}