/
OS.go
69 lines (61 loc) · 1.83 KB
/
OS.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
package helper
import (
"fmt"
"github.com/kardianos/osext"
"os"
"path/filepath"
"runtime"
"strings"
)
//IsRunningOnWindows returns true if the program is running on Windows
func IsRunningOnWindows() bool {
return runtime.GOOS == "windows"
}
//IsRunningOnLinux returns true if the program is running on Linux
func IsRunningOnLinux() bool {
return runtime.GOOS == "linux" || runtime.GOOS == "darwin"
}
//GenClassPath concatenates the elements with ; on Windows with : on Linux
func GenClassPath(elements ...string) string {
if IsRunningOnWindows() {
return strings.Join(elements, ";")
} else if IsRunningOnLinux() {
return strings.Join(elements, ":")
} else {
panic("Can not detect operatingsystem. Supported are: Windows, Linux, MacOs. Detected is: " + runtime.GOOS)
}
}
//DoesFileExist returns true if the given file exists
func DoesFileExist(file string) bool {
_, err := os.Stat(file)
return err == nil
}
var sakuliHome = ""
//GetSakuliHome returns the sakulihome folder.
//First lookup: env: SAKULI_HOME
//Second: one folder above the binary
func GetSakuliHome() string {
if sakuliHome == "" {
sakuliHome = os.Getenv("SAKULI_HOME")
if sakuliHome == "" {
var err error
var execFolder string
execFolder, err = osext.ExecutableFolder()
sakuliHome, err = filepath.Abs(filepath.Join(execFolder, ".."))
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "=================== SAKULI_HOME is empty using binary folder ===================\n"+sakuliHome+"\n================================================================================")
}
}
return sakuliHome
}
//GetSakuliRoot returns the sakuliroot folder.
//lookup: env: SAKULI_ROOT
func GetSakuliRoot() string {
var sakuliRoot = os.Getenv("SAKULI_ROOT")
if sakuliHome == "" {
panic("environment variable 'SAKULI_ROOT' is not defined!")
}
return sakuliRoot
}