Migrate to YAML config file
This commit is contained in:
parent
1a819db4f7
commit
a41a17c6a5
11 changed files with 171 additions and 32 deletions
2
Makefile
2
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 ./...
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/aceberg/WatchYourLAN/internal/web"
|
||||
)
|
||||
|
||||
const configPath = "/data/config"
|
||||
const configPath = "/data/config.yaml"
|
||||
const nodePath = ""
|
||||
|
||||
func main() {
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
2
go.mod
2
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 (
|
||||
|
|
|
|||
4
go.sum
4
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=
|
||||
|
|
|
|||
67
internal/check/file.go
Normal file
67
internal/check/file.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
44
internal/migrate/get-old.go
Normal file
44
internal/migrate/get-old.go
Normal file
|
|
@ -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
|
||||
}
|
||||
50
internal/migrate/migrate.go
Normal file
50
internal/migrate/migrate.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue