-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
87 lines (70 loc) · 1.62 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
package config
import (
"fmt"
"os"
"path/filepath"
)
const Version = "1.1.1-dev"
const (
BucketNameRoot = "sufr"
BucketNameURL = "url"
BucketNameUser = "user"
BucketNameTag = "tag"
DBFileMode = 0755
)
var (
SUFRUserAgent = fmt.Sprintf("Linux:SUFR:v%s", Version)
BuildTime string
BuildGitHash string
DataDir string
DatabaseFile string
)
var (
DefaultDataDir = fmt.Sprintf(filepath.Join(os.Getenv("HOME"), ".config", "sufr", "data"))
DefaultUserAgent = fmt.Sprintf("Linux:SUFR:v%s", Version)
)
const (
DefaultBindAddr = "localhost:8090"
DefaultDatabaseName = "sufr.db"
DefaultResultsPerPage = 40
)
type BuildInfo struct {
Time string
GitHash string
}
func SetBuildInfo(cfg *Config) {
cfg.Build = BuildInfo{
Time: BuildTime,
GitHash: BuildGitHash,
}
}
func SetDefaults(cfg *Config) {
if cfg.BindAddr == "" {
cfg.BindAddr = DefaultBindAddr
}
if cfg.DataDir == "" {
cfg.DataDir = DefaultDataDir
}
if cfg.ResultsPerPage == 0 {
cfg.ResultsPerPage = DefaultResultsPerPage
}
if cfg.UserAgent == "" {
cfg.UserAgent = DefaultUserAgent
}
if cfg.DatabaseFilename == "" {
cfg.DatabaseFilename = DefaultDatabaseName
}
}
type Config struct {
BindAddr string `env:"SUFR_BIND_ADDR"`
DataDir string `env:"SUFR_DATA_DIR"`
ResultsPerPage int `env:"SUFR_RESULTS_PER_PAGE"`
UserAgent string `env:"SUFR_USER_AGENT"`
DatabaseFilename string `env:"SUFR_DATABASE_FILENAME"`
Debug bool `env:"SUFR_DEBUG"`
// build time information
Build BuildInfo
}
func (c Config) DatabaseFile() string {
return filepath.Join(c.DataDir, c.DatabaseFilename)
}