forked from hyperboloide/pdfgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
112 lines (102 loc) · 2.48 KB
/
config.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/viper"
)
var (
// Templates is a map containing all templates,
// the keys are used to match urls with directories.
Templates map[string]*Template
)
func init() {
viper.SetEnvPrefix("pdfgen")
viper.AutomaticEnv()
viper.SetDefault("port", "8888")
viper.SetDefault("addr", "0.0.0.0")
viper.SetDefault("templates", "")
}
// IsValidDir returns true if the path exists and is a directory.
func IsValidDir(path string) bool {
fi, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return fi.Mode().IsDir()
}
// SelectDir selects the first valid dir.
func SelectDir(choices []string) *string {
for _, path := range choices {
if IsValidDir(path) {
return &path
}
}
return nil
}
// BuildTemplates will iterate all dirs one by one to create the corresponding
// templates.
func BuildTemplates(rootDir string) error {
fi, err := ioutil.ReadDir(rootDir)
if err != nil {
return fmt.Errorf("failed to read templates dir '%s'", rootDir)
}
Templates = make(map[string]*Template)
for _, i := range fi {
if i.IsDir() && i.Name()[0] != '.' {
t, err := NewTemplate(rootDir, i.Name())
if err != nil {
return err
}
Templates[i.Name()] = t
}
}
return nil
}
// FindRoot choose the templates root.
func FindRoot() (string, error) {
rootDir := viper.GetString("templates")
choices := []string{
"/etc/pdfgen/templates",
os.Getenv("HOME") + "/.templates",
}
if rootDir != "" {
if !IsValidDir(rootDir) {
return "", errors.New("invalid template directory")
}
return rootDir, nil
}
if pth := SelectDir(choices); pth != nil {
return *pth, nil
}
return "", errors.New("template directory not found")
}
// ConfigRead validates the configuration.
func ConfigRead() error {
if _, err := exec.LookPath("wkhtmltopdf"); err != nil {
return errors.New("executable wkhtmltopdf could not be found in PATH")
} else if rootDir, err := FindRoot(); err != nil {
return err
} else if rootDir, err = filepath.Abs(rootDir); err != nil {
return fmt.Errorf("invalid templates dir '%s'", rootDir)
} else if err := BuildTemplates(rootDir); err != nil {
return err
} else {
nb := len(Templates)
switch nb {
case 0:
return errors.New("No template found")
case 1:
fmt.Printf("1 template found in '%s':\n", rootDir)
default:
fmt.Printf("%d templates found in '%s':\n", nb, rootDir)
}
for k := range Templates {
fmt.Printf(" - %s\n", k)
}
return nil
}
}