-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
53 lines (47 loc) · 1.09 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
package astra
import (
"html/template"
"log"
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
)
type Config struct {
Location string
DBName string
Port string
InProduction bool
TemplateCache map[string]*template.Template
}
func DefaultConfig(dbPath, dbName string) *Config {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
cfg := &Config{
DBName: dbName,
Port: port,
}
if dbPath == "" {
homeDir, err := homedir.Dir()
if err != nil {
log.Printf("unable to find the home directory: %v", err)
os.Exit(1)
}
cfgLoc := filepath.Join(homeDir, ".astra")
if err = os.MkdirAll(cfgLoc, 0750); err != nil {
log.Printf("unable to create the config folder at the location %s: %v", cfgLoc, err)
os.Exit(1)
}
cfg.Location = cfgLoc
} else {
if err := os.MkdirAll(dbPath, 0750); err != nil {
log.Printf("unable to create the config folder at the location %s: %v", dbPath, err)
os.Exit(1)
}
cfg.Location = dbPath
}
log.Printf("Created and set the config location %s", cfg.Location)
cfg.InProduction = false
return cfg
}