From 2087cf2c0353284a591b32b93975ba164a3c230b Mon Sep 17 00:00:00 2001 From: restitux Date: Wed, 14 Sep 2022 18:36:36 -0600 Subject: [PATCH] Update config logic to use ohea/golang/config --- config/config.go | 87 ------------------------------------------------ go.mod | 9 ++--- go.sum | 22 ++++++++---- main.go | 46 ++++++++++++++----------- 4 files changed, 44 insertions(+), 120 deletions(-) delete mode 100644 config/config.go diff --git a/config/config.go b/config/config.go deleted file mode 100644 index 6739d17..0000000 --- a/config/config.go +++ /dev/null @@ -1,87 +0,0 @@ -package config - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - - "github.com/op/go-logging" - "github.com/pelletier/go-toml/v2" -) - -var log = logging.MustGetLogger("ikinuki-server") - -type Config struct { - Address string - Port int - ScanDirectory string -} - - -func GetConfigDir() string { - // Find config directory - - // Linux (XDG base directory specification compliant) - xdg_config_home := os.Getenv("XDG_CONFIG_HOME") - - if xdg_config_home != "" { - return filepath.Join(xdg_config_home, "ikinuki") - } else { - home := os.Getenv("HOME") - return filepath.Join(home, ".config", "ikinuki") - } -} - -func GetConfig(config_dir string) (*Config, error) { - config := Config{ - Address: "127.0.0.1", - Port: 32520, - ScanDirectory: "FILL IN", - } - - err := os.MkdirAll(config_dir, 0755) - if err != nil { - return &config, fmt.Errorf("Could not create config directory: %v", err) - } - - config_path := filepath.Join(config_dir, "server.toml") - - log.Debugf("Config File Dir: %v\n", config_path) - - // open file, creating it if empty - config_file, err := os.OpenFile(config_path, os.O_RDWR|os.O_CREATE, 0755) - defer config_file.Close() - - if err != nil { - return &config, fmt.Errorf("Could not open config file: %v", err) - } - - fi, err := config_file.Stat() - if err != nil { - return &config, fmt.Errorf("Could not get config file size: %v", err) - } - - if fi.Size() == 0 { - // if file is empty, write default config - data, err := toml.Marshal(config) - if err != nil { - return &config, fmt.Errorf("Could not write default config to file: %v", err) - } - config_file.Write(data) - // return nil as the user must edit the config file - return nil, nil - } else { - // try to parse config file - data, err := ioutil.ReadAll(config_file) - if err != nil { - return &config, fmt.Errorf("Could not read data from config file: %v", err) - } - err = toml.Unmarshal(data, &config) - if err != nil { - return &config, fmt.Errorf("Could not parse config file contents: %v", err) - } - return &config, nil - } - -} diff --git a/go.mod b/go.mod index 1e05b19..995f455 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,9 @@ module github.com/restitux/ikinuki-server go 1.18 require ( - github.com/mattn/go-sqlite3 v1.14.12 + github.com/mattn/go-sqlite3 v1.14.15 github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 - github.com/pelletier/go-toml/v2 v2.0.0-beta.8 + gitlab.com/ohea/golang/config v0.0.0-20220915002456-b07c13508f07 ) -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect -) +require github.com/pelletier/go-toml/v2 v2.0.5 // indirect diff --git a/go.sum b/go.sum index 09a33b9..a8b1ba4 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,26 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= -github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= -github.com/pelletier/go-toml/v2 v2.0.0-beta.8 h1:dy81yyLYJDwMTifq24Oi/IslOslRrDSb3jwDggjz3Z0= -github.com/pelletier/go-toml/v2 v2.0.0-beta.8/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= +github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gitlab.com/ohea/golang/config v0.0.0-20220914234236-03c9ea3081fa h1:oRAlBWnguYVME4HD5rw2iVFzyfbp2WiBlYc3vEx81yU= +gitlab.com/ohea/golang/config v0.0.0-20220914234236-03c9ea3081fa/go.mod h1:nAge7Tiz8q6mbHDGMEG8gIPqCjfCLnHAM8H7EjxoHoI= +gitlab.com/ohea/golang/config v0.0.0-20220915000847-ec118318b5b8 h1:Dfq2kxPHMqF3rciatF2fIuAKngINe4PFg0K1BgqT0KI= +gitlab.com/ohea/golang/config v0.0.0-20220915000847-ec118318b5b8/go.mod h1:nAge7Tiz8q6mbHDGMEG8gIPqCjfCLnHAM8H7EjxoHoI= +gitlab.com/ohea/golang/config v0.0.0-20220915002456-b07c13508f07 h1:4rc2sNX5t4PYVs9Ev7+crvID031ZHKdfXAxbT9X/zC0= +gitlab.com/ohea/golang/config v0.0.0-20220915002456-b07c13508f07/go.mod h1:nAge7Tiz8q6mbHDGMEG8gIPqCjfCLnHAM8H7EjxoHoI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index ee1fe2a..33fde91 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,11 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/op/go-logging" - "github.com/restitux/ikinuki-server/config" "github.com/restitux/ikinuki-server/database" "github.com/restitux/ikinuki-server/rest" + "gitlab.com/ohea/golang/config" ) - func get_data_dir() string { // Find data directory @@ -27,12 +26,14 @@ func get_data_dir() string { } } - - - - var log = logging.MustGetLogger("ikinuki-server") +type Config struct { + Address string + Port int + ScanDirectory string +} + func main() { var format = logging.MustStringFormatter( `%{color}%{time:15:04:05.000} %{level:.4s}:%{color:reset} %{message}`, @@ -45,31 +46,36 @@ func main() { logging.SetBackend(backendLeveled) - config_dir := config.GetConfigDir() + configData := config.Config[Config]{ + Name: "ikinuki", + Filename: "server", + Config: Config{ + Address: "127.0.0.1", + Port: 32520, + ScanDirectory: "FILL IN", + }, + } - config, err := config.GetConfig(config_dir) - if err != nil { - panic(err) + ret, err := configData.Get() + if ret == true { + log.Warningf("Created config file at %v, please edit and launch again\n", *configData.ConfigFile) } - if config == nil { - log.Warningf("Created config file at %v, please edit and launch again\n", config_dir) - os.Exit(0) - } - log.Debugf("Config: %v\n", config) data_dir := get_data_dir() - db, err := database.OpenDatabase(data_dir) + db, is_new, err := database.OpenDatabase(data_dir) if err != nil { log.Fatalf("Could not open database: %v", err) } defer db.Close() - err = database.ScanTvshowRoot(config.ScanDirectory, db) - if err != nil { - log.Fatalf("Could not scan directory \"%v\": %v\n", config.ScanDirectory, err) + if is_new { + err = database.ScanTvshowRoot(configData.Config.ScanDirectory, db) + if err != nil { + log.Fatalf("Could not scan directory \"%v\": %v\n", configData.Config.ScanDirectory, err) + } } - rest.RunHTTPServer(config.Address, config.Port, data_dir) + rest.RunHTTPServer(configData.Config.Address, configData.Config.Port, data_dir) }