2017-05-05 15:08:02 +02:00
package config
2017-05-05 14:10:35 +02:00
import (
"encoding/json"
"flag"
"fmt"
"io"
2017-05-31 04:21:57 +02:00
"io/ioutil"
"log"
2017-05-05 14:10:35 +02:00
"os"
2017-05-31 04:21:57 +02:00
yaml "gopkg.in/yaml.v2"
2017-05-08 23:40:54 +02:00
)
2017-05-31 10:49:01 +02:00
var (
// DefaultConfigPath : path to the default config file (please do not change it)
DefaultConfigPath = "config/default_config.yml"
// ConfigPath : path to the user specific config file (please do not change it)
ConfigPath = "config/config.yml"
)
2017-05-31 04:21:57 +02:00
// Conf : Modified configuration
var Conf * Config
var privateConf Config
2017-05-26 12:12:52 +02:00
// IsSukebei : Tells if we are on the sukebei website
2017-05-20 12:45:27 +02:00
func IsSukebei ( ) bool {
2017-05-31 04:21:57 +02:00
return Conf . Models . TorrentsTableName == "sukebei_torrents"
2017-05-29 06:51:43 +02:00
}
2017-05-14 00:55:17 +02:00
2017-05-05 14:10:35 +02:00
var allowedDatabaseTypes = map [ string ] bool {
"sqlite3" : true ,
"postgres" : true ,
"mysql" : true ,
"mssql" : true ,
}
2017-05-11 08:01:12 +02:00
var allowedDBLogModes = map [ string ] bool {
"default" : true , // errors only
"detailed" : true ,
"silent" : true ,
}
2017-05-31 04:21:57 +02:00
func init ( ) {
2017-05-31 10:49:01 +02:00
Parse ( )
}
// Parse : Parse config into a config variable
func Parse ( ) {
2017-05-31 04:21:57 +02:00
getDefaultConfig ( )
privateConf = * DefaultConfig
Conf = & privateConf
overrideDefaults ( )
}
func overrideDefaults ( ) {
2017-05-31 10:49:01 +02:00
data , err := ioutil . ReadFile ( ConfigPath )
2017-05-31 04:21:57 +02:00
if err != nil {
2017-05-31 10:49:01 +02:00
log . Printf ( "can't read file '%s'" , ConfigPath )
2017-05-31 04:21:57 +02:00
}
err = yaml . Unmarshal ( data , & Conf )
if err != nil {
log . Printf ( "error: %v" , err )
}
2017-05-05 14:10:35 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// BindFlags returns a function which is to be used after
// flag.Parse to check and copy the flags' values to the Config instance.
func ( config * Config ) BindFlags ( ) func ( ) error {
confFile := flag . String ( "conf" , "" , "path to the configuration file" )
2017-05-31 04:21:57 +02:00
dbType := flag . String ( "dbtype" , Conf . DBType , "database backend" )
host := flag . String ( "host" , Conf . Host , "binding address of the server" )
port := flag . Int ( "port" , Conf . Port , "port of the server" )
dbParams := flag . String ( "dbparams" , Conf . DBParams , "parameters to open the database (see Gorm's doc)" )
dbLogMode := flag . String ( "dblogmode" , Conf . DBLogMode , "database log verbosity (errors only by default)" )
2017-05-05 14:10:35 +02:00
return func ( ) error {
// You can override fields in the config file with flags.
config . Host = * host
config . Port = * port
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
config . DBParams = * dbParams
err := config . SetDBType ( * dbType )
2017-05-06 20:43:15 +02:00
if err != nil {
return err
}
2017-05-11 08:01:12 +02:00
err = config . SetDBLogMode ( * dbLogMode )
if err != nil {
return err
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
err = config . HandleConfFileFlag ( * confFile )
2017-05-05 14:10:35 +02:00
return err
}
}
2017-05-26 12:12:52 +02:00
// HandleConfFileFlag : Read the config from a file
2017-05-05 15:08:02 +02:00
func ( config * Config ) HandleConfFileFlag ( path string ) error {
2017-05-05 14:10:35 +02:00
if path != "" {
file , err := os . Open ( path )
if err != nil {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return fmt . Errorf ( "can't read file '%s'" , path )
2017-05-05 14:10:35 +02:00
}
2017-05-31 04:21:57 +02:00
var b [ ] byte
_ , err = file . Read ( b )
if err != nil {
return fmt . Errorf ( "failed to parse file '%s' (%s)" , path , err )
}
err = yaml . Unmarshal ( b , config )
2017-05-05 14:10:35 +02:00
if err != nil {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return fmt . Errorf ( "failed to parse file '%s' (%s)" , path , err )
2017-05-05 14:10:35 +02:00
}
}
return nil
}
2017-05-26 12:12:52 +02:00
// SetDBType : Set the DataBase type in config
func ( config * Config ) SetDBType ( dbType string ) error {
if ! allowedDatabaseTypes [ dbType ] {
return fmt . Errorf ( "unknown database backend '%s'" , dbType )
2017-05-05 14:10:35 +02:00
}
2017-05-26 12:12:52 +02:00
config . DBType = dbType
2017-05-05 14:10:35 +02:00
return nil
}
2017-05-26 12:12:52 +02:00
// SetDBLogMode : Set the log mode in config
func ( config * Config ) SetDBLogMode ( dbLogmode string ) error {
if ! allowedDBLogModes [ dbLogmode ] {
return fmt . Errorf ( "unknown database log mode '%s'" , dbLogmode )
2017-05-11 08:01:12 +02:00
}
2017-05-26 12:12:52 +02:00
config . DBLogMode = dbLogmode
2017-05-11 08:01:12 +02:00
return nil
}
2017-05-26 12:12:52 +02:00
// Read : Decode config from json to config
2017-05-05 14:10:35 +02:00
func ( config * Config ) Read ( input io . Reader ) error {
return json . NewDecoder ( input ) . Decode ( config )
}
2017-05-26 12:12:52 +02:00
// Write : Encode config from json to config
2017-05-05 14:10:35 +02:00
func ( config * Config ) Write ( output io . Writer ) error {
return json . NewEncoder ( output ) . Encode ( config )
}
2017-05-26 12:12:52 +02:00
// Pretty : Write config json in a file
2017-05-05 14:10:35 +02:00
func ( config * Config ) Pretty ( output io . Writer ) error {
2017-05-31 04:21:57 +02:00
data , err := yaml . Marshal ( config )
2017-05-05 14:10:35 +02:00
if err != nil {
return err
}
_ , err = output . Write ( data )
return err
2017-05-07 21:51:37 +02:00
}