-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
134 lines (114 loc) · 4.13 KB
/
parser.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package env provides runtime, server level setup and configuration
package env
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/BurntSushi/toml"
)
// LoadConfig loads runtime parameters like port numbers and DB connections.
// We first check for -config switch that would point us towards a .CONF file.
// If not found, we then read parameters from command line and environment vars.
func LoadConfig() (f Flags, ok bool) {
// Check and process config file
f, ok = configFile()
// If not OK then get parameters from command line and environment variables.
if !ok {
f, ok = commandLineEnv()
}
// reserved
if len(f.Location) == 0 {
f.Location = "selfhost"
}
return
}
// configFile checks for the presence of zero or one command argument.
// If no arguments are provided then we look for and load documize.conf file.
// If one argument is provided then we load the specified config file.
// If more than one argument is provided then we exit as flags as have passed.
// checks to see if it is a TOML format config file.
func configFile() (f Flags, ok bool) {
ok = false
var configFile string
// First argument is always program being executed.
// No additional arguments means check for documize.conf file.
if len(os.Args) == 1 {
// No arguments, so we default to default config filename.
configFile = "documize.conf"
} else if len(os.Args) == 2 {
// Config filename passed in, so we use it.
configFile = os.Args[1]
} else {
// Too many arguments means flags passed in so we return.
return
}
// Does file exist?
if len(configFile) == 0 || !configFileExists(configFile) {
return
}
// Tell caller where the config came from.
f.ConfigSource = configFile
// We parse the TOML format config file.
var ct ConfigToml
if _, err := toml.DecodeFile(configFile, &ct); err != nil {
fmt.Println(err)
return
}
f.DBType = strings.ToLower(ct.Database.Type)
f.DBConn = ct.Database.Connection
f.Salt = ct.Database.Salt
f.HTTPPort = strconv.Itoa(ct.HTTP.Port)
f.ForceHTTPPort2SSL = strconv.Itoa(ct.HTTP.ForceSSLPort)
f.SSLCertFile = ct.HTTP.Cert
f.SSLKeyFile = ct.HTTP.Key
f.Location = strings.ToLower(ct.Install.Location)
ok = true
return
}
// commandLineEnv loads command line and OS environment variables required by the program to function.
func commandLineEnv() (f Flags, ok bool) {
ok = true
var dbConn, dbType, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL, location string
// register(&configFile, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
register(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
register(&certFile, "cert", false, "the cert.pem file used for https")
register(&keyFile, "key", false, "the key.pem file used for https")
register(&port, "port", false, "http/https port number")
register(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS")
register(&siteMode, "offline", false, "set to '1' for OFFLINE mode")
register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgresql|sqlserver")
register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
register(&location, "location", false, `reserved`)
if !parse("db") {
ok = false
}
f.DBType = strings.ToLower(dbType)
f.DBConn = dbConn
f.ForceHTTPPort2SSL = forcePort2SSL
f.HTTPPort = port
f.Salt = jwtKey
f.SiteMode = siteMode
f.SSLCertFile = certFile
f.SSLKeyFile = keyFile
f.Location = strings.ToLower(location)
f.ConfigSource = "flags/environment"
return f, ok
}
func configFileExists(fn string) bool {
info, err := os.Stat(fn)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}