-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.go
62 lines (57 loc) · 1.57 KB
/
util.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
package main
import (
"flag"
"log"
"os"
)
// cutString cuts strings that exceed the maxLen to (maxLen-2) and adds ".."
func cutString(s string, maxLen int) string {
if len(s) > maxLen {
return s[:maxLen-2] + ".."
}
return s
}
// overwriteIfEmpty only overwrites the string s with the string overwrite if s is empty
func overwriteIfEmpty(s *string, overwrite string) {
if *s == "" {
*s = overwrite
}
}
// isFlagPassed returns true if the flag was explicitly set as CLI parameter
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
// isDirAccessible checks if the directory exsists, if it really is a directory instead of a file and if it's readable.
// If logFatal is true, errors will be printed and the program will exit. If it's false the program will just return the result.
func isDirAccessible(dir string, logFatal bool) bool {
// Use Stat instead of Lstat because serving works with softlinks (e.g. "serve -d ./softlink")
fileInfo, err := os.Stat(*directory)
if err != nil {
if logFatal {
log.Fatalf("%v can't be served: %v\n", *directory, err)
}
return false
} else if !fileInfo.IsDir() {
if logFatal {
log.Fatalf("%v can't be served because it's a file and not a directory: %v\n", *directory, err)
}
return false
} else {
file, err := os.Open(*directory)
if err != nil {
if logFatal {
log.Fatalf("%v can't be served because it's not readable: %v\n", *directory, err)
}
return false
} else {
file.Close() // Ignore errors
}
}
return true
}