Skip to content

Commit

Permalink
Merge pull request #288 from markusressel/bugfix/#287-create-db-direc…
Browse files Browse the repository at this point in the history
…tory-if-missing

automatically create the parent path of the db if missing
  • Loading branch information
markusressel authored Apr 12, 2024
2 parents 907c316 + b787b19 commit 7ba79ee
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.PHONY: help test build run clean

GO_FLAGS ?=
NAME := fan2go
OUTPUT_BIN ?= bin/${NAME}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (f *PidFanController) GetStatistics() FanControllerStatistics {
}

func (f *PidFanController) Run(ctx context.Context) error {
err := f.persistence.Init()
if err != nil {
return err
}

fan := f.fan

if fan.ShouldNeverStop() && !fan.Supports(fans.FeatureRpmSensor) {
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
19 changes: 19 additions & 0 deletions internal/persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package persistence

import (
"encoding/json"
"errors"
"fmt"
"github.com/markusressel/fan2go/internal/fans"
"github.com/markusressel/fan2go/internal/ui"
bolt "go.etcd.io/bbolt"
"os"
"path/filepath"
"time"
)

Expand All @@ -16,6 +18,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)
Expand All @@ -36,6 +40,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
ui.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 {
Expand Down

0 comments on commit 7ba79ee

Please sign in to comment.