-
Notifications
You must be signed in to change notification settings - Fork 18
/
dir.go
50 lines (42 loc) · 1017 Bytes
/
dir.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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// Package dir locates the Warcraft III installation and user directories.
package dir
import (
"os"
)
// Local directory that overrides any global directory
const Local = "./war3"
// Environment variables
const (
EnvUser = "WC3_USER_DIR"
EnvInstall = "WC3_INSTALL_DIR"
)
// UserDir returns the Warcraft III user directory
func UserDir() string {
if dir, ok := os.LookupEnv(EnvUser); ok {
return dir
}
if _, err := os.Stat(Local); err == nil {
return Local
}
return osUserDir()
}
// InstallDir checks common Warcraft III installation directories
// and returns the first one that exists
func InstallDir() string {
if dir, ok := os.LookupEnv(EnvInstall); ok {
return dir
}
if _, err := os.Stat(Local); err == nil {
return Local
}
var paths = osInstallDirs()
for _, p := range paths {
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}