forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.go
129 lines (109 loc) · 2.52 KB
/
filesystem.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/kardianos/osext"
)
// WINDOWS is "windows"
const WINDOWS = "windows"
// HomeDir is the user's home directory
var HomeDir = homeDir()
// BinPath is the running executable
var BinPath = binPath()
// AppDir is the subdirectory the binary is running from
var AppDir = appDir()
// ConfigHome is XDG_CONFIG_HOME/heroku or equivalent
var ConfigHome = configHome()
// DataHome is XDG_CONFIG_HOME/heroku or equivalent
var DataHome = dataHome()
// CacheHome is XDG_CACHE_HOME/heroku or equivalent
var CacheHome = cacheHome()
// ErrLogPath is the location of the error log
var ErrLogPath = filepath.Join(CacheHome, "error.log")
func homeDir() string {
home := os.Getenv("HOME")
if home != "" {
return home
}
user, err := user.Current()
must(err)
return user.HomeDir
}
func binPath() string {
d, err := osext.Executable()
must(err)
return d
}
func appDir() string {
d, err := osext.ExecutableFolder()
must(err)
return filepath.Join(d, "..")
}
func configHome() string {
d := os.Getenv("XDG_CONFIG_HOME")
if d == "" {
d = filepath.Join(HomeDir, ".config")
}
d = filepath.Join(d, "heroku")
must(mkdirp(d))
return d
}
func dataHome() string {
d := os.Getenv("XDG_DATA_HOME")
if d == "" {
if runtime.GOOS == WINDOWS && localAppData() != "" {
d = localAppData()
} else {
d = filepath.Join(HomeDir, ".local", "share")
}
}
d = filepath.Join(d, "heroku")
must(mkdirp(d))
return d
}
func cacheHome() string {
d := os.Getenv("XDG_CACHE_HOME")
if d == "" {
if runtime.GOOS == WINDOWS && localAppData() != "" {
d = localAppData()
} else {
d = filepath.Join(HomeDir, ".cache")
}
}
d = filepath.Join(d, "heroku")
must(mkdirp(d))
return d
}
func localAppData() string {
return os.Getenv("LOCALAPPDATA")
}
// FileExists returns whether or not path exists
func FileExists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func tmpDir(base string) string {
root := filepath.Join(base, "tmp")
err := mkdirp(root)
must(err)
dir, err := ioutil.TempDir(root, "")
must(err)
return dir
}
func mkdirp(path string) error {
err := os.MkdirAll(path, 0755)
if os.IsPermission(err) && runtime.GOOS != "windows" {
fmt.Fprintf(os.Stderr, "Error creating %s which is needed for the Heroku CLI.\nRun `sudo mkdir -p %s && sudo chown $USER %s` to create this directory.\n", path, path, path)
os.Exit(1)
}
return err
}