-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
84 lines (76 loc) · 1.81 KB
/
helpers.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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
)
func InterfaceMap(i interface{}) (interface{}, error) {
t := reflect.TypeOf(i)
switch t.Kind() {
case reflect.Map:
v := reflect.ValueOf(i)
it := reflect.TypeOf((*interface{})(nil)).Elem()
m := reflect.MakeMap(reflect.MapOf(t.Key(), it))
for _, mk := range v.MapKeys() {
m.SetMapIndex(mk, v.MapIndex(mk))
}
return m.Interface(), nil
}
return nil, errors.New("Unsupported type")
}
func String(v string) *string {
return &v
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE")
}
func unmarshallResource(resource *errorPageData) (output []byte) {
output, _ = json.Marshal(resource)
return output
}
func fileExists(filename string) (fileExist bool) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func dirExists(filename string) (dirExist bool) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
func templateFileCheck(template string, code string, filetype string) (filepath string, err error) {
var filename, extension string
switch filetype {
case "css":
filename = "style"
extension = "css"
break
case "html":
filename = "index"
extension = "html"
break
default:
return "", err
}
codeBasedFile := fmt.Sprintf("%v/%v/%v-%v.%v", templatePath, template, filename, code, extension)
genericFile := fmt.Sprintf("/%v/%v/%v.%v", templatePath, template, filename, extension)
fallbackFile := fmt.Sprintf("/%v/default/%v.%v", templatePath, filename, extension)
switch {
case fileExists(codeBasedFile):
return codeBasedFile, err
case fileExists(genericFile):
return genericFile, err
case fileExists(fallbackFile):
return fallbackFile, err
}
return filepath, err
}