From a41a17c6a53a2f8365decce3c2d700fea6be9120 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Thu, 3 Aug 2023 01:03:19 +0700 Subject: [PATCH] Migrate to YAML config file --- Makefile | 2 +- cmd/WatchYourLAN/main.go | 2 +- configs/watchyourlan.service | 2 +- go.mod | 2 +- go.sum | 4 +- internal/check/file.go | 67 ++++++++++++++++++++++ internal/check/path.go | 23 -------- internal/conf/{get-config.go => config.go} | 4 +- internal/migrate/get-old.go | 44 ++++++++++++++ internal/migrate/migrate.go | 50 ++++++++++++++++ internal/web/webgui.go | 3 +- 11 files changed, 171 insertions(+), 32 deletions(-) create mode 100644 internal/check/file.go delete mode 100644 internal/check/path.go rename internal/conf/{get-config.go => config.go} (96%) create mode 100644 internal/migrate/get-old.go create mode 100644 internal/migrate/migrate.go diff --git a/Makefile b/Makefile index 6237689..9fbe9c8 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ run: cd cmd/WatchYourLAN/ && \ sudo \ env IFACE=$(IFACE) DBPATH=$(DBPATH) THEME=$(THEME) \ - go run . #-n http://192.168.2.3:8850 + go run . -c /data/config #-n http://192.168.2.3:8850 fmt: go fmt ./... diff --git a/cmd/WatchYourLAN/main.go b/cmd/WatchYourLAN/main.go index fd023e7..40eb5db 100644 --- a/cmd/WatchYourLAN/main.go +++ b/cmd/WatchYourLAN/main.go @@ -7,7 +7,7 @@ import ( "github.com/aceberg/WatchYourLAN/internal/web" ) -const configPath = "/data/config" +const configPath = "/data/config.yaml" const nodePath = "" func main() { diff --git a/configs/watchyourlan.service b/configs/watchyourlan.service index 4258359..eb6ce84 100644 --- a/configs/watchyourlan.service +++ b/configs/watchyourlan.service @@ -5,7 +5,7 @@ After=network-online.target Wants=network-online.target [Service] -ExecStart=/usr/bin/watchyourlan -c /etc/watchyourlan/config +ExecStart=/usr/bin/watchyourlan -c /etc/watchyourlan/config.yaml Restart=on-failure [Install] diff --git a/go.mod b/go.mod index bc0cbc1..15e43f6 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/containrrr/shoutrrr v0.7.1 github.com/spf13/viper v1.16.0 - modernc.org/sqlite v1.23.1 + modernc.org/sqlite v1.24.0 ) require ( diff --git a/go.sum b/go.sum index 16c44a0..2bcf4f0 100644 --- a/go.sum +++ b/go.sum @@ -1189,8 +1189,8 @@ modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= -modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= -modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= +modernc.org/sqlite v1.24.0 h1:EsClRIWHGhLTCX44p+Ri/JLD+vFGo0QGjasg2/F9TlI= +modernc.org/sqlite v1.24.0/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY= diff --git a/internal/check/file.go b/internal/check/file.go new file mode 100644 index 0000000..155ec51 --- /dev/null +++ b/internal/check/file.go @@ -0,0 +1,67 @@ +package check + +import ( + "os" + "path/filepath" +) + +// Path - create path if not exists +func Path(path string) bool { + + _, err := os.Stat(path) + + if path != "" && err != nil { + + dir := filepath.Dir(path) + + err = os.MkdirAll(dir, os.ModePerm) + IfError(err) + + _, err = os.Create(path) + IfError(err) + + return false + } + + return true +} + +// Exists - check is file exists +func Exists(path string) bool { + + _, err := os.Stat(path) + + if path != "" && err != nil { + + return false + } + + return true +} + +// IsYaml - check if file got .yaml or .yml extension +func IsYaml(path string) bool { + + if Exists(path) { + ext := filepath.Ext(path) + if ext == ".yaml" || ext == ".yml" { + return true + } + } + + return false +} + +// IsEmpty - check if file is empty +func IsEmpty(path string) bool { + + if Exists(path) { + stat, _ := os.Stat(path) + size := stat.Size() + if size > 0 { + return false + } + } + + return true +} diff --git a/internal/check/path.go b/internal/check/path.go deleted file mode 100644 index c4d1643..0000000 --- a/internal/check/path.go +++ /dev/null @@ -1,23 +0,0 @@ -package check - -import ( - "os" - "path/filepath" -) - -// Path - create path if not exist -func Path(path string) { - - _, err := os.Stat(path) - - if path != "" && err != nil { - - dir := filepath.Dir(path) - - err = os.MkdirAll(dir, os.ModePerm) - IfError(err) - - _, err = os.Create(path) - IfError(err) - } -} diff --git a/internal/conf/get-config.go b/internal/conf/config.go similarity index 96% rename from internal/conf/get-config.go rename to internal/conf/config.go index 78cf886..5b69bc4 100644 --- a/internal/conf/get-config.go +++ b/internal/conf/config.go @@ -21,7 +21,7 @@ func Get(path string) (config models.Conf) { viper.SetDefault("LOGLEVEL", "verbose") viper.SetConfigFile(path) - viper.SetConfigType("env") + viper.SetConfigType("yaml") err := viper.ReadInConfig() check.IfError(err) @@ -45,7 +45,7 @@ func Get(path string) (config models.Conf) { func Write(path string, config models.Conf) { viper.SetConfigFile(path) - viper.SetConfigType("env") + viper.SetConfigType("yaml") viper.Set("IFACE", config.Iface) viper.Set("DBPATH", config.DbPath) diff --git a/internal/migrate/get-old.go b/internal/migrate/get-old.go new file mode 100644 index 0000000..8f70957 --- /dev/null +++ b/internal/migrate/get-old.go @@ -0,0 +1,44 @@ +package migrate + +import ( + "github.com/spf13/viper" + + "github.com/aceberg/WatchYourLAN/internal/models" +) + +func getOldConfig(path string) (config models.Conf) { + + viper.SetDefault("IFACE", "enp1s0") + viper.SetDefault("DBPATH", "/data/db.sqlite") + viper.SetDefault("GUIIP", "localhost") + viper.SetDefault("GUIPORT", "8840") + viper.SetDefault("TIMEOUT", "60") + viper.SetDefault("SHOUTRRR_URL", "") + viper.SetDefault("THEME", "solar") + viper.SetDefault("COLOR", "light") + viper.SetDefault("IGNOREIP", "no") + viper.SetDefault("LOGLEVEL", "verbose") + + viper.SetConfigFile(path) + viper.SetConfigType("env") + + viper.AutomaticEnv() // Get ENVIRONMENT variables + + err := viper.ReadInConfig() + + if err == nil { + + config.Iface = viper.Get("IFACE").(string) + config.DbPath = viper.Get("DBPATH").(string) + config.GuiIP = viper.Get("GUIIP").(string) + config.GuiPort = viper.Get("GUIPORT").(string) + config.Timeout = viper.GetInt("TIMEOUT") + config.ShoutURL = viper.Get("SHOUTRRR_URL").(string) + config.Theme = viper.Get("THEME").(string) + config.Color = viper.Get("COLOR").(string) + config.IgnoreIP = viper.Get("IGNOREIP").(string) + config.LogLevel = viper.Get("LOGLEVEL").(string) + } + + return config +} diff --git a/internal/migrate/migrate.go b/internal/migrate/migrate.go new file mode 100644 index 0000000..4d6f471 --- /dev/null +++ b/internal/migrate/migrate.go @@ -0,0 +1,50 @@ +package migrate + +import ( + "fmt" + "log" + + "github.com/aceberg/WatchYourLAN/internal/check" + "github.com/aceberg/WatchYourLAN/internal/conf" + "github.com/aceberg/WatchYourLAN/internal/notify" +) + +// ToYAML - check if config file is YAML and migrate from ENV-file if not +func ToYAML(path string) string { + + if check.IsYaml(path) { + if check.IsEmpty(path) { + + oldPath := path[:len(path)-5] // path w/o .yaml + + if !check.IsEmpty(oldPath) { + migrateConfig(oldPath, path) + } + } + } else { + newPath := path + ".yaml" + check.Path(newPath) + + if check.IsEmpty(newPath) && !check.IsEmpty(path) { + + migrateConfig(path, newPath) + } + + path = newPath + } + + return path +} + +func migrateConfig(oldPath, newPath string) { + + config := getOldConfig(oldPath) + conf.Write(newPath, config) + + msg := fmt.Sprintf("Config file migrated from ENV to YAML format. \nOld config file %s is no longer used and can be deleted. \nNew config file is %s, please update your settings.", oldPath, newPath) + + log.Println("=================================== ") + log.Println("WARNING:", msg) + log.Println("=================================== ") + notify.Shoutrrr("WatchYourLAN: "+msg, config.ShoutURL) +} diff --git a/internal/web/webgui.go b/internal/web/webgui.go index b3f92aa..8d6db31 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -7,13 +7,14 @@ import ( "github.com/aceberg/WatchYourLAN/internal/check" "github.com/aceberg/WatchYourLAN/internal/conf" "github.com/aceberg/WatchYourLAN/internal/db" + "github.com/aceberg/WatchYourLAN/internal/migrate" "github.com/aceberg/WatchYourLAN/internal/scan" ) // Gui - start web GUI func Gui(configPath, nodePath string) { - ConfigPath = configPath + ConfigPath = migrate.ToYAML(configPath) AppConfig = conf.Get(ConfigPath) AppConfig.NodePath = nodePath AppConfig.Icon = Icon