From aa49d601ab3ae090369af36d6de3fd76e2c54916 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 9 Sep 2021 23:33:04 +0200 Subject: [PATCH] override config entries with cli args use getconfig instead of settings file --- apiGo/api/Settings.go | 5 +- apiGo/api/TVShows.go | 4 +- apiGo/api/Video.go | 4 +- apiGo/config/Config.go | 174 ++++++++++++++++++++++------ apiGo/database/Database.go | 17 +-- apiGo/database/settings/Settings.go | 20 ---- apiGo/main.go | 43 +------ 7 files changed, 156 insertions(+), 111 deletions(-) delete mode 100644 apiGo/database/settings/Settings.go diff --git a/apiGo/api/Settings.go b/apiGo/api/Settings.go index b3a6a63..c5dedde 100644 --- a/apiGo/api/Settings.go +++ b/apiGo/api/Settings.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "openmediacenter/apiGo/api/types" + "openmediacenter/apiGo/config" "openmediacenter/apiGo/database" "openmediacenter/apiGo/database/settings" "openmediacenter/apiGo/videoparser" @@ -88,8 +89,8 @@ func getSettingsFromDB() { MediacenterName: sett.MediacenterName, VideoPath: serverVideoPath, TVShowPath: serverTVShowPath, - TVShowEnabled: settings.TVShowsEnabled(), - FullDeleteEnabled: settings.VideosDeletable(), + TVShowEnabled: !config.GetConfig().Features.DisableTVSupport, + FullDeleteEnabled: config.GetConfig().Features.FullyDeletableVideos, } str, _ := json.Marshal(res) diff --git a/apiGo/api/TVShows.go b/apiGo/api/TVShows.go index 7969392..7004c61 100644 --- a/apiGo/api/TVShows.go +++ b/apiGo/api/TVShows.go @@ -2,13 +2,13 @@ package api import ( "fmt" + "openmediacenter/apiGo/config" "openmediacenter/apiGo/database" - "openmediacenter/apiGo/database/settings" ) func AddTvshowHandlers() { // do not add handlers if tvshows not enabled - if !settings.TVShowsEnabled() { + if config.GetConfig().Features.DisableTVSupport { return } diff --git a/apiGo/api/Video.go b/apiGo/api/Video.go index b1dbe53..4af0ba5 100644 --- a/apiGo/api/Video.go +++ b/apiGo/api/Video.go @@ -5,8 +5,8 @@ import ( "fmt" "net/url" "openmediacenter/apiGo/api/types" + "openmediacenter/apiGo/config" "openmediacenter/apiGo/database" - "openmediacenter/apiGo/database/settings" "os" "strconv" ) @@ -448,7 +448,7 @@ func addToVideoHandlers() { } // only allow deletion of video if cli flag is set, independent of passed api arg - if settings.VideosDeletable() && args.FullyDelete { + if config.GetConfig().Features.FullyDeletableVideos && args.FullyDelete { // get physical path of video to delete query = fmt.Sprintf("SELECT movie_url FROM videos WHERE movie_id=%d", args.MovieId) var vidpath string diff --git a/apiGo/config/Config.go b/apiGo/config/Config.go index f1f46cb..a8f292e 100644 --- a/apiGo/config/Config.go +++ b/apiGo/config/Config.go @@ -2,6 +2,7 @@ package config import ( "errors" + "flag" "fmt" "github.com/pelletier/go-toml/v2" "os" @@ -15,18 +16,40 @@ type DatabaseT struct { DBHost string } -type FileConfT struct { - Database DatabaseT +type FeaturesT struct { + DisableTVSupport bool + FullyDeletableVideos bool } -func defaultConig() *FileConfT { - return &FileConfT{Database: DatabaseT{ - DBName: "mediacenter", - DBPassword: "mediapassword", - DBUser: "mediacenteruser", - DBPort: 3306, - DBHost: "127.0.0.1", - }} +type GeneralT struct { + VerboseLogging bool + ReindexPrefix string +} + +type FileConfT struct { + Database DatabaseT + General GeneralT + Features FeaturesT +} + +func defaultConfig() *FileConfT { + return &FileConfT{ + Database: DatabaseT{ + DBName: "mediacenter", + DBPassword: "mediapassword", + DBUser: "mediacenteruser", + DBPort: 3306, + DBHost: "127.0.0.1", + }, + General: GeneralT{ + VerboseLogging: false, + ReindexPrefix: "/var/www/openmediacenter", + }, + Features: FeaturesT{ + DisableTVSupport: false, + FullyDeletableVideos: false, + }, + } } var liveConf FileConfT @@ -44,32 +67,11 @@ func Init() { // check if config exists on local dir dat, err = os.ReadFile(cfgname) if err != nil { - // config really doesn't exist! - fmt.Printf("config not existing -- generating new empty config at %s%s\n", cfgpath, cfgname) - - // generate new default config - obj, _ := toml.Marshal(defaultConig()) - liveConf = *defaultConig() - - err := os.WriteFile(cfgpath+cfgname, obj, 777) - if err != nil { - if errors.Is(err, os.ErrPermission) { - // permisson denied to create file try to create at current dir - err = os.WriteFile(cfgname, obj, 777) - if err != nil { - fmt.Println("failed to create default config file!") - } else { - fmt.Println("config file created at .") - } - } else { - fmt.Println(err.Error()) - } - } + generateNewConfig(cfgpath, cfgname) } else { // ok decode local config decodeConfig(&dat) } - } else { // other error fmt.Println(err.Error()) @@ -77,6 +79,32 @@ func Init() { } else { decodeConfig(&dat) } + + handleCommandLineArguments() +} + +func generateNewConfig(cfgpath string, cfgname string) { + // config really doesn't exist! + fmt.Printf("config not existing -- generating new empty config at %s%s\n", cfgpath, cfgname) + + // generate new default config + obj, _ := toml.Marshal(defaultConfig()) + liveConf = *defaultConfig() + + err := os.WriteFile(cfgpath+cfgname, obj, 777) + if err != nil { + if errors.Is(err, os.ErrPermission) { + // permisson denied to create file try to create at current dir + err = os.WriteFile(cfgname, obj, 777) + if err != nil { + fmt.Println("failed to create default config file!") + } else { + fmt.Println("config file created at .") + } + } else { + fmt.Println(err.Error()) + } + } } func decodeConfig(bin *[]byte) { @@ -85,7 +113,7 @@ func decodeConfig(bin *[]byte) { err := toml.Unmarshal(*bin, &config) if err != nil { fmt.Println(err) - + liveConf = *defaultConfig() } else { fmt.Println("Successfully loaded config file!") @@ -93,6 +121,86 @@ func decodeConfig(bin *[]byte) { } } +// handleCommandLineArguments let cli args override the config +func handleCommandLineArguments() { + // get a defaultconfig obj to set defaults + dconf := defaultConfig() + + const ( + DBHost = "DBHost" + DBPort = "DBPort" + DBUser = "DBUser" + DBPassword = "DBPassword" + DBName = "DBName" + Verbose = "v" + ReindexPrefix = "ReindexPrefix" + DisableTVSupport = "DisableTVSupport" + FullyDeletableVideos = "FullyDeletableVideos" + ) + + dbhostPtr := flag.String(DBHost, dconf.Database.DBHost, "database host name") + dbPortPtr := flag.Int(DBPort, int(dconf.Database.DBPort), "database port") + dbUserPtr := flag.String(DBUser, dconf.Database.DBUser, "database username") + dbPassPtr := flag.String(DBPassword, dconf.Database.DBPassword, "database username") + dbNamePtr := flag.String(DBName, dconf.Database.DBName, "database name") + + verbosePtr := flag.Bool(Verbose, dconf.General.VerboseLogging, "Verbose log output") + + pathPrefix := flag.String(ReindexPrefix, dconf.General.ReindexPrefix, "Prefix path for videos to reindex") + + disableTVShowSupport := flag.Bool(DisableTVSupport, dconf.Features.DisableTVSupport, "Disable the TVShow support and pages") + videosFullyDeletable := flag.Bool(FullyDeletableVideos, dconf.Features.FullyDeletableVideos, "Allow deletion from harddisk") + + flag.Parse() + + if isFlagPassed(DBHost) { + liveConf.Database.DBHost = *dbhostPtr + } + + if isFlagPassed(DBPort) { + liveConf.Database.DBPort = uint16(*dbPortPtr) + } + + if isFlagPassed(DBName) { + liveConf.Database.DBName = *dbNamePtr + } + + if isFlagPassed(DBUser) { + liveConf.Database.DBUser = *dbUserPtr + } + + if isFlagPassed(DBPassword) { + liveConf.Database.DBPassword = *dbPassPtr + } + + if isFlagPassed(Verbose) { + liveConf.General.VerboseLogging = *verbosePtr + } + + if isFlagPassed(ReindexPrefix) { + liveConf.General.ReindexPrefix = *pathPrefix + } + + if isFlagPassed(DisableTVSupport) { + liveConf.Features.DisableTVSupport = *disableTVShowSupport + } + + if isFlagPassed(FullyDeletableVideos) { + liveConf.Features.FullyDeletableVideos = *videosFullyDeletable + } +} + +// isFlagPassed check whether a flag was passed +func isFlagPassed(name string) bool { + found := false + flag.Visit(func(f *flag.Flag) { + if f.Name == name { + found = true + } + }) + return found +} + func GetConfig() *FileConfT { return &liveConf } diff --git a/apiGo/database/Database.go b/apiGo/database/Database.go index 5eb7f3f..e72723d 100644 --- a/apiGo/database/Database.go +++ b/apiGo/database/Database.go @@ -5,23 +5,14 @@ import ( "fmt" _ "github.com/go-sql-driver/mysql" "openmediacenter/apiGo/api/types" + "openmediacenter/apiGo/config" ) var db *sql.DB var DBName string -// store the command line parameter for Videoprefix -var SettingsVideoPrefix = "" - -type DatabaseConfig struct { - DBHost string - DBPort int - DBUser string - DBPassword string - DBName string -} - -func InitDB(dbconf *DatabaseConfig) { +func InitDB() { + dbconf := config.GetConfig().Database DBName = dbconf.DBName // Open up our database connection. @@ -128,6 +119,6 @@ func GetSettings() (result types.SettingsType, PathPrefix string, sizes types.Se result.TMDBGrabbing = TMDBGrabbing != 0 result.PasswordEnabled = result.Password != "-1" result.DarkMode = DarkMode != 0 - PathPrefix = SettingsVideoPrefix + PathPrefix = config.GetConfig().General.ReindexPrefix return } diff --git a/apiGo/database/settings/Settings.go b/apiGo/database/settings/Settings.go deleted file mode 100644 index d13d305..0000000 --- a/apiGo/database/settings/Settings.go +++ /dev/null @@ -1,20 +0,0 @@ -package settings - -var tvShowEnabled bool -var videosDeletable bool - -func TVShowsEnabled() bool { - return tvShowEnabled -} - -func SetTVShowEnabled(enabled bool) { - tvShowEnabled = enabled -} - -func VideosDeletable() bool { - return videosDeletable -} - -func SetVideosDeletable(deletable bool) { - videosDeletable = deletable -} diff --git a/apiGo/main.go b/apiGo/main.go index 62105f7..76507f1 100644 --- a/apiGo/main.go +++ b/apiGo/main.go @@ -1,14 +1,12 @@ package main import ( - "flag" "fmt" "log" "net/http" "openmediacenter/apiGo/api" "openmediacenter/apiGo/config" "openmediacenter/apiGo/database" - settings2 "openmediacenter/apiGo/database/settings" "openmediacenter/apiGo/static" "openmediacenter/apiGo/videoparser" ) @@ -17,18 +15,13 @@ func main() { fmt.Println("init OpenMediaCenter server") port := 8081 - db, verbose, pathPrefix := handleCommandLineArguments() - // todo some verbosity logger or sth - config.Init() - fmt.Printf("Use verbose output: %t\n", verbose) - fmt.Printf("Videopath prefix: %s\n", *pathPrefix) + // todo some verbosity logger or sth + fmt.Printf("Use verbose output: %t\n", config.GetConfig().General.VerboseLogging) + fmt.Printf("Videopath prefix: %s\n", config.GetConfig().General.ReindexPrefix) - // set pathprefix in database settings object - database.SettingsVideoPrefix = *pathPrefix - - database.InitDB(db) + database.InitDB() defer database.Close() api.AddVideoHandlers() @@ -47,31 +40,3 @@ func main() { fmt.Printf("OpenMediacenter server up and running on port %d\n", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } - -func handleCommandLineArguments() (*database.DatabaseConfig, bool, *string) { - dbhostPtr := flag.String("DBHost", "127.0.0.1", "database host name") - dbPortPtr := flag.Int("DBPort", 3306, "database port") - dbUserPtr := flag.String("DBUser", "mediacenteruser", "database username") - dbPassPtr := flag.String("DBPassword", "mediapassword", "database username") - dbNamePtr := flag.String("DBName", "mediacenter", "database name") - - verbosePtr := flag.Bool("v", true, "Verbose log output") - - pathPrefix := flag.String("ReindexPrefix", "/var/www/openmediacenter", "Prefix path for videos to reindex") - - disableTVShowSupport := flag.Bool("DisableTVSupport", false, "Disable the TVShow support and pages") - videosFullyDeletable := flag.Bool("FullyDeletableVideos", false, "Allow deletion from harddisk") - - flag.Parse() - - settings2.SetTVShowEnabled(!*disableTVShowSupport) - settings2.SetVideosDeletable(*videosFullyDeletable) - - return &database.DatabaseConfig{ - DBHost: *dbhostPtr, - DBPort: *dbPortPtr, - DBUser: *dbUserPtr, - DBPassword: *dbPassPtr, - DBName: *dbNamePtr, - }, *verbosePtr, pathPrefix -}