-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmanager.go
133 lines (111 loc) · 3.22 KB
/
manager.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
package manager
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stashapp/stash-box/pkg/email"
"github.com/stashapp/stash-box/pkg/logger"
"github.com/stashapp/stash-box/pkg/manager/config"
"github.com/stashapp/stash-box/pkg/manager/paths"
"github.com/stashapp/stash-box/pkg/utils"
)
type singleton struct {
EmailManager *email.Manager
}
var instance *singleton
var once sync.Once
var configFilePath *string
func GetInstance() *singleton {
Initialize()
return instance
}
func Initialize() *singleton {
once.Do(func() {
initFlags()
initConfig()
initLog()
instance = &singleton{
EmailManager: email.NewManager(),
}
})
return instance
}
// returns the path and config name
func parseConfigFilePath() (string, string) {
dir := filepath.Dir(*configFilePath)
name := filepath.Base(*configFilePath)
extension := filepath.Ext(*configFilePath)
name = strings.TrimSuffix(name, extension)
return dir, name
}
func initConfig() {
if *configFilePath != "" {
dir, name := parseConfigFilePath()
viper.SetConfigName(name)
viper.AddConfigPath(dir)
} else {
// The config file is called config. Leave off the file extension.
viper.SetConfigName(paths.GetConfigName())
viper.AddConfigPath(".") // Look for config in the working directory
}
err := viper.ReadInConfig() // Find and read the config file
newConfig := false
if err != nil { // Handle errors reading the config file
newConfig = true
defaultConfigFilePath := paths.GetDefaultConfigFilePath()
if *configFilePath != "" {
defaultConfigFilePath = *configFilePath
}
_ = utils.Touch(defaultConfigFilePath)
if err = viper.ReadInConfig(); err != nil {
panic(err)
}
}
if err = config.InitializeDefaults(); err != nil {
panic(err)
}
initEnvs()
// Reread the config;
if err = viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
logger.Infof("failed to bind flags: %s", err.Error())
}
if err = config.Initialize(); err != nil {
panic(err)
}
if newConfig {
fmt.Printf(`
A new config file has been generated at %s.
The database connection string has been defaulted to: %s
Please ensure this database is created and available, or change the connection string in the configuration file, then rerun stash-box.`,
viper.GetViper().ConfigFileUsed(), config.GetDatabasePath())
os.Exit(0)
}
missingEmail := config.GetMissingEmailSettings()
if len(missingEmail) > 0 {
fmt.Printf("RequireActivation is set to true, but the following required settings are missing: %s\n", strings.Join(missingEmail, ", "))
}
}
func initFlags() {
pflag.IP("host", net.IPv4(0, 0, 0, 0), "ip address for the host")
pflag.Int("port", 9998, "port to serve from")
configFilePath = pflag.String("config_file", "", "location of the config file")
pflag.Parse()
}
func initEnvs() {
viper.SetEnvPrefix("stash_box") // will be uppercased automatically
viper.AutomaticEnv()
_ = viper.BindEnv("host") // STASH_BOX_HOST
_ = viper.BindEnv("port") // STASH_BOX_PORT
_ = viper.BindEnv("database") // STASH_BOX_DATABASE
}
func initLog() {
logger.Init(config.GetLogFile(), config.GetUserLogFile(), config.GetLogOut(), config.GetLogLevel())
}