-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
126 lines (111 loc) · 3.45 KB
/
utils.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
)
// Assert will throw a panic if condition is false.
// The additional parameter is provided to panic() as argument.
func Assert(cond bool, i interface{}) {
if cond == false {
panic(i)
}
}
// AssertNoError is a cheap function to check for err==nil and
// panics if condition is not met
func AssertNoError(err error) {
Assert(err == nil, err)
}
// ExitOnError exits the program with rc=1 if err!=nil. The following
// Message and all additional arguments will be passed to fmt.Printf()
func ExitOnError(err error, errMsg string, v ...interface{}) {
if err != nil {
fmt.Fprintf(os.Stderr, errMsg+": ", v...)
fmt.Fprintln(os.Stderr, err)
// TODO add flag somewhere that will exit with panic() instead
os.Exit(1)
}
}
// ExitWithMessage prints out a message on StdErr and then exits the
// program with rc=1
func ExitWithMessage(errMsg string, v ...interface{}) {
fmt.Fprintf(os.Stderr, "Error: "+errMsg, v...)
os.Exit(1)
}
// InformOnError provides an error message on stdErr if an error
// occurs, but continues with the program
func InformOnError(err error, errMsg string, v ...interface{}) {
if err != nil {
fmt.Fprintf(os.Stderr, errMsg+":\n", v...)
fmt.Fprintln(os.Stderr, err)
}
}
// GetTempDir returns the location of a temporary directory in which
// files can be stored. The caller needs to ensure that the
// directory is deleted afterwards.
func GetTempDir() (dirName string) {
dirName, err := ioutil.TempDir("", "pfscf-")
AssertNoError(err)
return dirName
}
// GetExecutableDir returns the dir in which the binary of this program is located
func GetExecutableDir() (dirName string) {
var baseDir string
if IsTestEnvironment() {
// during test runs the executable will be run from some temporary
// directory, so instead return the local directory for that case.
baseDir = "."
} else {
baseDir = filepath.Dir(os.Args[0])
}
dirName, err := filepath.Abs(baseDir)
AssertNoError(err)
return dirName
}
// IsFile checks wether a file exists and is not a directory.
func IsFile(filename string) (exists bool, err error) {
info, err := os.Stat(filename)
if err != nil {
return false, err
}
if info.IsDir() {
return false, fmt.Errorf("Path %v is a directory", filename)
}
return true, nil
}
// IsDir checks wether a directory exists.
func IsDir(dirname string) (exists bool, err error) {
info, err := os.Stat(dirname)
if err != nil {
return false, err
}
if !info.IsDir() {
return false, fmt.Errorf("Path %v is not a directory", dirname)
}
return true, nil
}
// IsSet checks whether the provided value is different from its zero value and
// in case of a non-nil pointer it also checks whether the referenced value is
// not the zero value
func IsSet(val interface{}) (result bool) {
x := reflect.ValueOf(val)
if x.Kind() == reflect.Ptr {
return !(x.IsNil() || x.Elem().IsZero())
}
return !x.IsZero()
}
// HasWhitespace checks if a string includes at least one whitespace character
func HasWhitespace(s string) bool {
return strings.Index(s, " ") > -1
}
// QuoteStringIfRequired takes a string as input. If the string contains whitespace,
// then it is returned enclosed by quotation marks, else it is returned as-is
func QuoteStringIfRequired(s string) string {
if HasWhitespace(s) {
return fmt.Sprintf("\"%v\"", s)
}
return s
}