2021-04-11 23:31:33 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
"errors"
|
2021-05-13 12:15:21 +00:00
|
|
|
"fmt"
|
2021-04-11 23:31:33 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2021-11-12 21:05:05 +00:00
|
|
|
"path/filepath"
|
2021-04-11 23:31:33 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
var (
|
|
|
|
initOnce sync.Once
|
|
|
|
instanceOnce sync.Once
|
|
|
|
)
|
2021-04-11 23:31:33 +00:00
|
|
|
|
|
|
|
type flagStruct struct {
|
|
|
|
configFilePath string
|
2021-05-16 07:21:11 +00:00
|
|
|
cpuProfilePath string
|
2021-10-28 23:19:23 +00:00
|
|
|
nobrowser bool
|
2021-04-11 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
func GetInstance() *Instance {
|
|
|
|
instanceOnce.Do(func() {
|
|
|
|
instance = &Instance{
|
|
|
|
main: viper.New(),
|
|
|
|
overrides: viper.New(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:31:33 +00:00
|
|
|
func Initialize() (*Instance, error) {
|
|
|
|
var err error
|
2021-11-07 23:14:11 +00:00
|
|
|
initOnce.Do(func() {
|
2021-04-11 23:31:33 +00:00
|
|
|
flags := initFlags()
|
2021-11-07 23:14:11 +00:00
|
|
|
overrides := makeOverrideConfig()
|
2021-05-16 07:21:11 +00:00
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
_ = GetInstance()
|
|
|
|
instance.overrides = overrides
|
|
|
|
instance.cpuProfilePath = flags.cpuProfilePath
|
|
|
|
|
|
|
|
if err = initConfig(instance, flags); err != nil {
|
2021-05-13 12:15:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance.isNewSystem {
|
|
|
|
if instance.Validate() == nil {
|
|
|
|
// system has been initialised by the environment
|
|
|
|
instance.isNewSystem = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !instance.isNewSystem {
|
2021-11-07 23:14:11 +00:00
|
|
|
err = instance.setExistingSystemDefaults()
|
|
|
|
if err == nil {
|
|
|
|
err = instance.SetInitialConfig()
|
|
|
|
}
|
2021-05-13 12:15:21 +00:00
|
|
|
}
|
2021-04-11 23:31:33 +00:00
|
|
|
})
|
|
|
|
return instance, err
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
func initConfig(instance *Instance, flags flagStruct) error {
|
|
|
|
v := instance.main
|
2021-05-16 07:21:11 +00:00
|
|
|
|
2021-04-11 23:31:33 +00:00
|
|
|
// The config file is called config. Leave off the file extension.
|
2021-11-07 23:14:11 +00:00
|
|
|
v.SetConfigName("config")
|
2021-04-11 23:31:33 +00:00
|
|
|
|
2021-11-12 21:05:05 +00:00
|
|
|
v.AddConfigPath(".") // Look for config in the working directory
|
|
|
|
v.AddConfigPath(filepath.FromSlash("$HOME/.stash")) // Look for the config in the home directory
|
2021-04-11 23:31:33 +00:00
|
|
|
|
2021-05-13 12:15:21 +00:00
|
|
|
configFile := ""
|
2021-04-11 23:31:33 +00:00
|
|
|
envConfigFile := os.Getenv("STASH_CONFIG_FILE")
|
2021-05-13 12:15:21 +00:00
|
|
|
|
|
|
|
if flags.configFilePath != "" {
|
|
|
|
configFile = flags.configFilePath
|
|
|
|
} else if envConfigFile != "" {
|
|
|
|
configFile = envConfigFile
|
2021-04-11 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:15:21 +00:00
|
|
|
if configFile != "" {
|
2021-11-07 23:14:11 +00:00
|
|
|
v.SetConfigFile(configFile)
|
2021-04-11 23:31:33 +00:00
|
|
|
|
2021-05-13 12:15:21 +00:00
|
|
|
// if file does not exist, assume it is a new system
|
|
|
|
if exists, _ := utils.FileExists(configFile); !exists {
|
|
|
|
instance.isNewSystem = true
|
|
|
|
|
|
|
|
// ensure we can write to the file
|
|
|
|
if err := utils.Touch(configFile); err != nil {
|
|
|
|
return fmt.Errorf(`could not write to provided config path "%s": %s`, configFile, err.Error())
|
|
|
|
} else {
|
|
|
|
// remove the file
|
|
|
|
os.Remove(configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
err := v.ReadInConfig() // Find and read the config file
|
2021-05-13 12:15:21 +00:00
|
|
|
// if not found, assume its a new system
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
var notFoundErr viper.ConfigFileNotFoundError
|
|
|
|
if errors.As(err, ¬FoundErr) {
|
2021-05-13 12:15:21 +00:00
|
|
|
instance.isNewSystem = true
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-11 23:31:33 +00:00
|
|
|
|
2021-05-13 12:15:21 +00:00
|
|
|
return nil
|
2021-04-11 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initFlags() flagStruct {
|
|
|
|
flags := flagStruct{}
|
|
|
|
|
|
|
|
pflag.IP("host", net.IPv4(0, 0, 0, 0), "ip address for the host")
|
|
|
|
pflag.Int("port", 9999, "port to serve from")
|
|
|
|
pflag.StringVarP(&flags.configFilePath, "config", "c", "", "config file to use")
|
2021-05-16 07:21:11 +00:00
|
|
|
pflag.StringVar(&flags.cpuProfilePath, "cpuprofile", "", "write cpu profile to file")
|
2021-10-28 23:19:23 +00:00
|
|
|
pflag.BoolVar(&flags.nobrowser, "nobrowser", false, "Don't open a browser window after launch")
|
2021-04-11 23:31:33 +00:00
|
|
|
|
|
|
|
pflag.Parse()
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
func initEnvs(viper *viper.Viper) {
|
|
|
|
viper.SetEnvPrefix("stash") // will be uppercased automatically
|
|
|
|
bindEnv(viper, "host") // STASH_HOST
|
|
|
|
bindEnv(viper, "port") // STASH_PORT
|
|
|
|
bindEnv(viper, "external_host") // STASH_EXTERNAL_HOST
|
|
|
|
bindEnv(viper, "generated") // STASH_GENERATED
|
|
|
|
bindEnv(viper, "metadata") // STASH_METADATA
|
|
|
|
bindEnv(viper, "cache") // STASH_CACHE
|
|
|
|
bindEnv(viper, "stash") // STASH_STASH
|
2021-09-20 23:34:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:14:11 +00:00
|
|
|
func bindEnv(viper *viper.Viper, key string) {
|
2021-09-20 23:34:25 +00:00
|
|
|
if err := viper.BindEnv(key); err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to set environment key (%v): %v", key, err))
|
2021-04-11 23:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 23:14:11 +00:00
|
|
|
|
|
|
|
func makeOverrideConfig() *viper.Viper {
|
|
|
|
viper := viper.New()
|
|
|
|
|
|
|
|
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
|
|
|
|
logger.Infof("failed to bind flags: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
initEnvs(viper)
|
|
|
|
|
|
|
|
return viper
|
|
|
|
}
|