-
Notifications
You must be signed in to change notification settings - Fork 13
/
fileutil.go
54 lines (49 loc) · 1.04 KB
/
fileutil.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
44
45
46
47
48
49
50
51
52
53
54
package metha
import (
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"github.com/adrg/xdg"
gzip "github.com/klauspost/pgzip"
)
// MustGlob is like filepath.Glob, but panics on bad pattern.
func MustGlob(pattern string) []string {
m, err := filepath.Glob(pattern)
if err != nil {
panic(err)
}
return m
}
// MoveCompressFile will atomically move and compress a source file to a
// destination file.
func MoveCompressFile(src, dst string) (err error) {
tmp := fmt.Sprintf("%s-tmp-%d", dst, rand.Intn(999999999))
f, err := os.Create(tmp)
if err != nil {
return err
}
defer f.Close()
gw := gzip.NewWriter(f)
defer gw.Close()
ff, err := os.Open(src)
if err != nil {
return err
}
defer ff.Close()
if _, err := io.Copy(gw, ff); err != nil {
return err
}
if err := os.Rename(tmp, dst); err != nil {
return err
}
return os.Remove(src)
}
// GetBaseDir returns the base directory for the cache.
func GetBaseDir() string {
if dir := os.Getenv("METHA_DIR"); dir != "" {
return dir
}
return filepath.Join(xdg.CacheHome, "metha")
}