Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically create the parent path of the db if missing #288

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading