From ffe757ebbce5693ca7b03e45ddfa0df3ab478ec0 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Fri, 12 Apr 2024 01:56:04 +0200 Subject: [PATCH 1/4] automatically create the parent path of the db if missing --- internal/controller/controller.go | 2 ++ internal/persistence/persistence.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index b9116d2..d452727 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -97,6 +97,8 @@ func (f *PidFanController) GetStatistics() FanControllerStatistics { } func (f *PidFanController) Run(ctx context.Context) error { + f.persistence.Init() + fan := f.fan if fan.ShouldNeverStop() && !fan.Supports(fans.FeatureRpmSensor) { diff --git a/internal/persistence/persistence.go b/internal/persistence/persistence.go index 9373544..5c4e835 100644 --- a/internal/persistence/persistence.go +++ b/internal/persistence/persistence.go @@ -2,11 +2,14 @@ package persistence import ( "encoding/json" + "errors" "fmt" + "github.com/labstack/gommon/log" "github.com/markusressel/fan2go/internal/fans" "github.com/markusressel/fan2go/internal/ui" bolt "go.etcd.io/bbolt" "os" + "path/filepath" "time" ) @@ -16,6 +19,8 @@ const ( ) type Persistence interface { + Init() error + LoadFanPwmData(fan fans.Fan) (map[int]float64, error) SaveFanPwmData(fan fans.Fan) (err error) DeleteFanPwmData(fan fans.Fan) (err error) @@ -36,6 +41,21 @@ func NewPersistence(dbPath string) Persistence { return p } +func (p persistence) Init() (err error) { + // get parent path of dbPath + parentDir := filepath.Dir(p.dbPath) + _, err = os.Stat(parentDir) + if errors.Is(err, os.ErrNotExist) { + // create directory + log.Info("Creating directory for db: %s", parentDir) + err = os.MkdirAll(parentDir, 0755) + if err != nil { + return err + } + } + return nil +} + func (p persistence) openPersistence() (db *bolt.DB, err error) { db, err = bolt.Open(p.dbPath, 0600, &bolt.Options{Timeout: 1 * time.Minute}) if err != nil { From 31400eebcf7a385f0baef248ca18538b15c654f0 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Fri, 12 Apr 2024 02:14:28 +0200 Subject: [PATCH 2/4] add note about custom path do "As a Service" section --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8cd89fb..e6580e5 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ # UI -fan2go is a simple terminal client and controller daemon. To allow external programs to interact with it there is an API that +fan2go is a simple terminal client and controller daemon. To allow external programs to interact with it there is an API +that can be (optionally) enabled. ## UI Clients @@ -484,6 +485,9 @@ sudo systemctl enable --now fan2go journalctl -u fan2go -f ``` +> NOTE: If you want to use a config path that differs from the default one, make sure to edit the +> unit file and point the `-c` flag to the correct path. + ## CLI Commands Although fan2go is a fan controller daemon at heart, it also provides some handy cli commands to interact with the From 8b8a504405d2b5b9ba168383b710afbe494cd545 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Fri, 12 Apr 2024 02:20:07 +0200 Subject: [PATCH 3/4] add PHONY to Makefile, fix tests, fix logging --- Makefile | 2 ++ internal/controller/controller_test.go | 2 ++ internal/persistence/persistence.go | 3 +-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 94d3b87..fb358c7 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +.PHONY: help test build run clean + GO_FLAGS ?= NAME := fan2go OUTPUT_BIN ?= bin/${NAME} diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index 21eacab..1dab39a 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -203,6 +203,8 @@ var ( type mockPersistence struct{} +func (p mockPersistence) Init() (err error) { return nil } + func (p mockPersistence) SaveFanPwmData(fan fans.Fan) (err error) { return nil } func (p mockPersistence) LoadFanPwmData(fan fans.Fan) (map[int]float64, error) { fanCurveDataMap := map[int]float64{} diff --git a/internal/persistence/persistence.go b/internal/persistence/persistence.go index 5c4e835..6dec653 100644 --- a/internal/persistence/persistence.go +++ b/internal/persistence/persistence.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/labstack/gommon/log" "github.com/markusressel/fan2go/internal/fans" "github.com/markusressel/fan2go/internal/ui" bolt "go.etcd.io/bbolt" @@ -47,7 +46,7 @@ func (p persistence) Init() (err error) { _, err = os.Stat(parentDir) if errors.Is(err, os.ErrNotExist) { // create directory - log.Info("Creating directory for db: %s", parentDir) + ui.Info("Creating directory for db: %s", parentDir) err = os.MkdirAll(parentDir, 0755) if err != nil { return err From b787b19c3a3aa6b38e37fe75b7429bee462d83e0 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Fri, 12 Apr 2024 02:30:40 +0200 Subject: [PATCH 4/4] return error --- internal/controller/controller.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index d452727..3bd8a60 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -97,7 +97,10 @@ func (f *PidFanController) GetStatistics() FanControllerStatistics { } func (f *PidFanController) Run(ctx context.Context) error { - f.persistence.Init() + err := f.persistence.Init() + if err != nil { + return err + } fan := f.fan