From d204450e4f8bfd5d6513e6adc6753ca4f232ab66 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Feb 2023 21:39:31 +0100 Subject: [PATCH 01/27] added linting --- .github/workflows/go.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 58c6ab4..cee4c72 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -27,6 +27,9 @@ jobs: - name: Test run: make test + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3.4.0 + - name: Build run: make build From c18e2d382affaa46f4a8fa6a9035f132622875f7 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Feb 2023 21:49:41 +0100 Subject: [PATCH 02/27] lint fixes --- cmd/curve/list.go | 4 +--- internal/backend.go | 30 +++++++++++++----------------- internal/controller/controller.go | 12 +++++++----- internal/util/pid.go | 2 +- internal/util/slice.go | 2 +- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/cmd/curve/list.go b/cmd/curve/list.go index 16ac05e..ea3619f 100644 --- a/cmd/curve/list.go +++ b/cmd/curve/list.go @@ -37,9 +37,7 @@ var curveCmd = &cobra.Command{ } curveConfigsToPrint = append(curveConfigsToPrint, *curveConf) } else { - for _, curveConfig := range configuration.CurrentConfig.Curves { - curveConfigsToPrint = append(curveConfigsToPrint, curveConfig) - } + curveConfigsToPrint = append(curveConfigsToPrint, configuration.CurrentConfig.Curves...) } for idx, curveConfig := range curveConfigsToPrint { diff --git a/internal/backend.go b/internal/backend.go index e6332bd..b89cf2d 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -59,11 +59,9 @@ func RunDaemon() { ui.Error("Error running profiling webserver: %v", http.ListenAndServe(address, mux)) }() - select { - case <-ctx.Done(): - ui.Info("Stopping profiling webserver...") - return nil - } + <-ctx.Done() + ui.Info("Stopping profiling webserver...") + return nil }, func(err error) { if err != nil { ui.Warning("Error stopping parca webserver: " + err.Error()) @@ -81,20 +79,18 @@ func RunDaemon() { servers := createWebServer() - select { - case <-ctx.Done(): - ui.Debug("Stopping all webservers...") - timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 5*time.Second) - defer timeoutCancel() - - for _, server := range servers { - err := server.Shutdown(timeoutCtx) - if err != nil { - return err - } + <-ctx.Done() + ui.Debug("Stopping all webservers...") + timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 5*time.Second) + defer timeoutCancel() + + for _, server := range servers { + err := server.Shutdown(timeoutCtx) + if err != nil { + return err } - return nil } + return nil }, func(err error) { if err != nil { ui.Warning("Error stopping webservers: " + err.Error()) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index e687c49..c4612ac 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -149,7 +149,8 @@ func (f *PidFanController) Run(ctx context.Context) error { return err } - f.computePwmMap() + err1 := f.computePwmMap() + ui.Warning("Error computing PWM map: %v", err1) f.updateDistinctPwmValues() @@ -257,7 +258,8 @@ func (f *PidFanController) UpdateFanSpeed() error { func (f *PidFanController) RunInitializationSequence() (err error) { fan := f.fan - f.computePwmMap() + err1 := f.computePwmMap() + ui.Warning("Error computing PWM map: %v", err1) err = f.persistence.SaveFanPwmMap(fan.GetId(), f.pwmMap) if err != nil { @@ -553,12 +555,12 @@ func (f *PidFanController) computePwmMap() (err error) { func (f *PidFanController) computePwmMapAutomatically() { fan := f.fan - trySetManualPwm(fan) + _ = trySetManualPwm(fan) // check every pwm value pwmMap := map[int]int{} for i := fans.MaxPwmValue; i >= fans.MinPwmValue; i-- { - fan.SetPwm(i) + _ = fan.SetPwm(i) time.Sleep(10 * time.Millisecond) pwm, err := fan.GetPwm() if err != nil { @@ -568,7 +570,7 @@ func (f *PidFanController) computePwmMapAutomatically() { } f.pwmMap = pwmMap - fan.SetPwm(f.pwmMap[fan.GetStartPwm()]) + _ = fan.SetPwm(f.pwmMap[fan.GetStartPwm()]) } func (f *PidFanController) updateDistinctPwmValues() { diff --git a/internal/util/pid.go b/internal/util/pid.go index 601819b..4b0b1a9 100644 --- a/internal/util/pid.go +++ b/internal/util/pid.go @@ -15,7 +15,7 @@ type PidLoop struct { // integral from previous loop + error, i.e. integral error integral float64 // error - error from previous loop, i.e. differential error - differentialError float64 + //differentialError float64 // last execution time of the loop lastTime time.Time } diff --git a/internal/util/slice.go b/internal/util/slice.go index 560f1de..f8f9419 100644 --- a/internal/util/slice.go +++ b/internal/util/slice.go @@ -54,7 +54,7 @@ func sortSlice[T constraints.Ordered](s []T) { func SortedKeys[T constraints.Ordered, K any](input map[T]K) []T { result := make([]T, 0, len(input)) - for k, _ := range input { + for k := range input { result = append(result, k) } sortSlice(result) From 0bf5359ba470edf92fa7378c8acb5533848d504c Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Feb 2023 22:03:45 +0100 Subject: [PATCH 03/27] pin golancti-lint version --- .github/workflows/go.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index cee4c72..2e7ab19 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,6 +29,29 @@ jobs: - name: Run golangci-lint uses: golangci/golangci-lint-action@v3.4.0 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: v1.51.1 + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + args: --exclude-use-default + # --issues-exit-code=0 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the all caching functionality will be complete disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true - name: Build run: make build From 0690ae6ab519cfbabb0ffa2faeb2d6f39401cb1a Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Tue, 21 Feb 2023 05:50:14 +0100 Subject: [PATCH 04/27] Update go.yml --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2e7ab19..719b4ed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -48,7 +48,7 @@ jobs: # skip-cache: true # Optional: if set to true then the action don't cache or restore ~/go/pkg. - # skip-pkg-cache: true + skip-pkg-cache: true # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. # skip-build-cache: true From e0fb5c26db5d20100964196c7f9c62580802c6f1 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:20:45 +0100 Subject: [PATCH 05/27] lint fixes --- cmd/curve/curve.go | 3 +- cmd/fan/fan.go | 3 +- cmd/sensor/sensor.go | 7 ++-- internal/configuration/validation.go | 63 ++++++++++++++-------------- internal/fans/cmd.go | 3 +- internal/fans/hwmon.go | 3 +- internal/hwmon/hwmon.go | 5 +-- internal/sensors/cmd.go | 5 +-- internal/util/exec.go | 3 +- internal/util/file.go | 7 ++-- 10 files changed, 46 insertions(+), 56 deletions(-) diff --git a/cmd/curve/curve.go b/cmd/curve/curve.go index ed2bc7c..7755c37 100644 --- a/cmd/curve/curve.go +++ b/cmd/curve/curve.go @@ -1,7 +1,6 @@ package curve import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/configuration" "github.com/spf13/cobra" @@ -33,5 +32,5 @@ func getCurveConfig(id string, curves []configuration.CurveConfig) (*configurati } } - return nil, errors.New(fmt.Sprintf("No curve with id found: %s, options: %s", id, availableCurveIds)) + return nil, fmt.Errorf("no curve with id found: %s, options: %s", id, availableCurveIds) } diff --git a/cmd/fan/fan.go b/cmd/fan/fan.go index ed81396..c850c44 100644 --- a/cmd/fan/fan.go +++ b/cmd/fan/fan.go @@ -1,7 +1,6 @@ package fan import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/configuration" @@ -58,5 +57,5 @@ func getFan(id string) (fans.Fan, error) { } } - return nil, errors.New(fmt.Sprintf("No fan with id found: %s, options: %s", id, availableFanIds)) + return nil, fmt.Errorf("no fan with id found: %s, options: %s", id, availableFanIds) } diff --git a/cmd/sensor/sensor.go b/cmd/sensor/sensor.go index 849dbee..3270e81 100644 --- a/cmd/sensor/sensor.go +++ b/cmd/sensor/sensor.go @@ -1,7 +1,6 @@ package sensor import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/configuration" "github.com/markusressel/fan2go/internal/hwmon" @@ -66,13 +65,13 @@ func getSensor(id string) (sensors.Sensor, error) { for _, controller := range controllers { matched, err := regexp.MatchString("(?i)"+config.HwMon.Platform, controller.Platform) if err != nil { - return nil, errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform)) + return nil, fmt.Errorf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform) } if matched { sensor, exists := controller.Sensors[config.HwMon.Index] if exists { if len(sensor.Input) <= 0 { - return nil, errors.New(fmt.Sprintf("Unable to find temp input for sensor %s", id)) + return nil, fmt.Errorf("unable to find temp input for sensor %s", id) } config.HwMon.TempInput = sensor.Input break @@ -90,5 +89,5 @@ func getSensor(id string) (sensors.Sensor, error) { } } - return nil, errors.New(fmt.Sprintf("No sensor with id found: %s, options: %s", id, availableSensorIds)) + return nil, fmt.Errorf("no sensor with id found: %s, options: %s", id, availableSensorIds) } diff --git a/internal/configuration/validation.go b/internal/configuration/validation.go index ffeeb78..cec0f6d 100644 --- a/internal/configuration/validation.go +++ b/internal/configuration/validation.go @@ -1,7 +1,6 @@ package configuration import ( - "errors" "fmt" "strings" @@ -28,7 +27,7 @@ func validateConfig(config *Configuration, path string) error { if containsCmdSensors() || containsCmdFan() { if _, err := util.CheckFilePermissionsForExecution(path); err != nil { - return errors.New(fmt.Sprintf("Config file '%s' has invalid permissions: %s", path, err)) + return fmt.Errorf("config file '%s' has invalid permissions: %s", path, err) } } @@ -60,7 +59,7 @@ func validateSensors(config *Configuration) error { for _, sensorConfig := range config.Sensors { if slices.Contains(sensorIds, sensorConfig.ID) { - return errors.New(fmt.Sprintf("Duplicate sensor id detected: %s", sensorConfig.ID)) + return fmt.Errorf("duplicate sensor id detected: %s", sensorConfig.ID) } sensorIds = append(sensorIds, sensorConfig.ID) @@ -75,10 +74,10 @@ func validateSensors(config *Configuration) error { subConfigs++ } if subConfigs > 1 { - return errors.New(fmt.Sprintf("Sensor %s: only one sensor type can be used per sensor definition block", sensorConfig.ID)) + return fmt.Errorf("sensor %s: only one sensor type can be used per sensor definition block", sensorConfig.ID) } if subConfigs <= 0 { - return errors.New(fmt.Sprintf("Sensor %s: sub-configuration for sensor is missing, use one of: hwmon | file | cmd", sensorConfig.ID)) + return fmt.Errorf("sensor %s: sub-configuration for sensor is missing, use one of: hwmon | file | cmd", sensorConfig.ID) } if !isSensorConfigInUse(sensorConfig, config.Curves) { @@ -87,7 +86,7 @@ func validateSensors(config *Configuration) error { if sensorConfig.HwMon != nil { if sensorConfig.HwMon.Index <= 0 { - return errors.New(fmt.Sprintf("Sensor %s: invalid index, must be >= 1", sensorConfig.ID)) + return fmt.Errorf("sensor %s: invalid index, must be >= 1", sensorConfig.ID) } } } @@ -118,7 +117,7 @@ func validateCurves(config *Configuration) error { for _, curveConfig := range config.Curves { if slices.Contains(curveIds, curveConfig.ID) { - return errors.New(fmt.Sprintf("Duplicate curve id detected: %s", curveConfig.ID)) + return fmt.Errorf("duplicate curve id detected: %s", curveConfig.ID) } curveIds = append(curveIds, curveConfig.ID) @@ -133,10 +132,10 @@ func validateCurves(config *Configuration) error { subConfigs++ } if subConfigs > 1 { - return errors.New(fmt.Sprintf("Curve %s: only one curve type can be used per curve definition block", curveConfig.ID)) + return fmt.Errorf("curve %s: only one curve type can be used per curve definition block", curveConfig.ID) } if subConfigs <= 0 { - return errors.New(fmt.Sprintf("Curve %s: sub-configuration for curve is missing, use one of: linear | pid | function", curveConfig.ID)) + return fmt.Errorf("curve %s: sub-configuration for curve is missing, use one of: linear | pid | function", curveConfig.ID) } if !isCurveConfigInUse(curveConfig, config.Curves, config.Fans) { @@ -146,16 +145,16 @@ func validateCurves(config *Configuration) error { if curveConfig.Function != nil { supportedTypes := []string{FunctionMinimum, FunctionAverage, FunctionMaximum, FunctionDelta, FunctionSum, FunctionDifference} if !slices.Contains(supportedTypes, curveConfig.Function.Type) { - return errors.New(fmt.Sprintf("Curve %s: unsupported function type '%s', use one of: %s", curveConfig.ID, curveConfig.Function.Type, strings.Join(supportedTypes, " | "))) + return fmt.Errorf("curve %s: unsupported function type '%s', use one of: %s", curveConfig.ID, curveConfig.Function.Type, strings.Join(supportedTypes, " | ")) } var connections []interface{} for _, curve := range curveConfig.Function.Curves { if curve == curveConfig.ID { - return errors.New(fmt.Sprintf("Curve %s: a curve cannot reference itself", curveConfig.ID)) + return fmt.Errorf("curve %s: a curve cannot reference itself", curveConfig.ID) } if !curveIdExists(curve, config) { - return errors.New(fmt.Sprintf("Curve %s: no curve definition with id '%s' found", curveConfig.ID, curve)) + return fmt.Errorf("curve %s: no curve definition with id '%s' found", curveConfig.ID, curve) } connections = append(connections, curve) } @@ -164,26 +163,26 @@ func validateCurves(config *Configuration) error { if curveConfig.Linear != nil { if len(curveConfig.Linear.Sensor) <= 0 { - return errors.New(fmt.Sprintf("Curve %s: Missing sensorId", curveConfig.ID)) + return fmt.Errorf("curve %s: missing sensorId", curveConfig.ID) } if !sensorIdExists(curveConfig.Linear.Sensor, config) { - return errors.New(fmt.Sprintf("Curve %s: no sensor definition with id '%s' found", curveConfig.ID, curveConfig.Linear.Sensor)) + return fmt.Errorf("curve %s: no sensor definition with id '%s' found", curveConfig.ID, curveConfig.Linear.Sensor) } } if curveConfig.PID != nil { if len(curveConfig.PID.Sensor) <= 0 { - return errors.New(fmt.Sprintf("Curve %s: Missing sensorId", curveConfig.ID)) + return fmt.Errorf("curve %s: missing sensorId", curveConfig.ID) } if !sensorIdExists(curveConfig.PID.Sensor, config) { - return errors.New(fmt.Sprintf("Curve %s: no sensor definition with id '%s' found", curveConfig.ID, curveConfig.PID.Sensor)) + return fmt.Errorf("curve %s: no sensor definition with id '%s' found", curveConfig.ID, curveConfig.PID.Sensor) } pidConfig := curveConfig.PID if pidConfig.P == 0 && pidConfig.I == 0 && pidConfig.D == 0 { - return errors.New(fmt.Sprintf("Curve %s: all PID constants are zero", curveConfig.ID)) + return fmt.Errorf("curve %s: all PID constants are zero", curveConfig.ID) } } @@ -207,7 +206,7 @@ func validateNoLoops(graph map[interface{}][]interface{}) error { output := tarjan.Connections(graph) for _, items := range output { if len(items) > 1 { - return errors.New(fmt.Sprintf("You have created a curve dependency cycle: %v", items)) + return fmt.Errorf("you have created a curve dependency cycle: %v", items) } } return nil @@ -236,7 +235,7 @@ func validateFans(config *Configuration) error { for _, fanConfig := range config.Fans { if slices.Contains(fanIds, fanConfig.ID) { - return errors.New(fmt.Sprintf("Duplicate fan id detected: %s", fanConfig.ID)) + return fmt.Errorf("duplicate fan id detected: %s", fanConfig.ID) } fanIds = append(fanIds, fanConfig.ID) @@ -252,55 +251,55 @@ func validateFans(config *Configuration) error { } if subConfigs > 1 { - return errors.New(fmt.Sprintf("Fan %s: only one fan type can be used per fan definition block", fanConfig.ID)) + return fmt.Errorf("fan %s: only one fan type can be used per fan definition block", fanConfig.ID) } if subConfigs <= 0 { - return errors.New(fmt.Sprintf("Fan %s: sub-configuration for fan is missing, use one of: hwmon | file | cmd", fanConfig.ID)) + return fmt.Errorf("fan %s: sub-configuration for fan is missing, use one of: hwmon | file | cmd", fanConfig.ID) } if len(fanConfig.Curve) <= 0 { - return errors.New(fmt.Sprintf("Fan %s: missing curve definition in configuration entry", fanConfig.ID)) + return fmt.Errorf("fan %s: missing curve definition in configuration entry", fanConfig.ID) } if !curveIdExists(fanConfig.Curve, config) { - return errors.New(fmt.Sprintf("Fan %s: no curve definition with id '%s' found", fanConfig.ID, fanConfig.Curve)) + return fmt.Errorf("fan %s: no curve definition with id '%s' found", fanConfig.ID, fanConfig.Curve) } if fanConfig.HwMon != nil { if (fanConfig.HwMon.Index != 0 && fanConfig.HwMon.RpmChannel != 0) || (fanConfig.HwMon.Index == 0 && fanConfig.HwMon.RpmChannel == 0) { - return errors.New(fmt.Sprintf("Fan %s: must have one of index or rpmChannel, must be >= 1", fanConfig.ID)) + return fmt.Errorf("fan %s: must have one of index or rpmChannel, must be >= 1", fanConfig.ID) } if fanConfig.HwMon.Index < 0 { - return errors.New(fmt.Sprintf("Fan %s: invalid index, must be >= 1", fanConfig.ID)) + return fmt.Errorf("fan %s: invalid index, must be >= 1", fanConfig.ID) } if fanConfig.HwMon.RpmChannel < 0 { - return errors.New(fmt.Sprintf("Fan %s: invalid rpmChannel, must be >= 1", fanConfig.ID)) + return fmt.Errorf("fan %s: invalid rpmChannel, must be >= 1", fanConfig.ID) } if fanConfig.HwMon.PwmChannel < 0 { - return errors.New(fmt.Sprintf("Fan %s: invalid pwmChannel, must be >= 1", fanConfig.ID)) + return fmt.Errorf("fan %s: invalid pwmChannel, must be >= 1", fanConfig.ID) } } if fanConfig.File != nil { if len(fanConfig.File.Path) <= 0 { - return errors.New(fmt.Sprintf("Fan %s: no file path provided", fanConfig.ID)) + return fmt.Errorf("fan %s: no file path provided", fanConfig.ID) } } if fanConfig.Cmd != nil { cmdConfig := fanConfig.Cmd if cmdConfig.SetPwm == nil { - return errors.New(fmt.Sprintf("Fan %s: missing setPwm configuration", fanConfig.ID)) + return fmt.Errorf("fan %s: missing setPwm configuration", fanConfig.ID) } if len(cmdConfig.SetPwm.Exec) <= 0 { - return errors.New(fmt.Sprintf("Fan %s: setPwm executable is missing", fanConfig.ID)) + return fmt.Errorf("fan %s: setPwm executable is missing", fanConfig.ID) } if cmdConfig.GetPwm == nil { - return errors.New(fmt.Sprintf("Fan %s: missing getPwm configuration", fanConfig.ID)) + return fmt.Errorf("fan %s: missing getPwm configuration", fanConfig.ID) } if len(cmdConfig.GetPwm.Exec) <= 0 { - return errors.New(fmt.Sprintf("Fan %s: getPwm executable is missing", fanConfig.ID)) + return fmt.Errorf("fan %s: getPwm executable is missing", fanConfig.ID) } } } diff --git a/internal/fans/cmd.go b/internal/fans/cmd.go index 25b5bc4..17a9240 100644 --- a/internal/fans/cmd.go +++ b/internal/fans/cmd.go @@ -1,7 +1,6 @@ package fans import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/configuration" "github.com/markusressel/fan2go/internal/ui" @@ -114,7 +113,7 @@ func (fan *CmdFan) SetPwm(pwm int) (err error) { timeout := 2 * time.Second _, err = util.SafeCmdExecution(conf.Exec, args, timeout) if err != nil { - return errors.New(fmt.Sprintf("%s", err.Error())) + return fmt.Errorf("%s", err.Error()) } return nil diff --git a/internal/fans/hwmon.go b/internal/fans/hwmon.go index 6cf00d6..99121fa 100644 --- a/internal/fans/hwmon.go +++ b/internal/fans/hwmon.go @@ -1,7 +1,6 @@ package fans import ( - "errors" "fmt" "os" @@ -163,7 +162,7 @@ func (fan *HwMonFan) SetPwmEnabled(value ControlMode) (err error) { if err == nil { currentValue, err := util.ReadIntFromFile(fan.Config.HwMon.PwmEnablePath) if err != nil || ControlMode(currentValue) != value { - return errors.New(fmt.Sprintf("PWM mode stuck to %d", currentValue)) + return fmt.Errorf("PWM mode stuck to %d", currentValue) } } return err diff --git a/internal/hwmon/hwmon.go b/internal/hwmon/hwmon.go index 570b3c9..18172df 100644 --- a/internal/hwmon/hwmon.go +++ b/internal/hwmon/hwmon.go @@ -1,7 +1,6 @@ package hwmon import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/ui" "io/ioutil" @@ -287,7 +286,7 @@ func UpdateFanConfigFromHwMonControllers(controllers []*HwMonController, config for _, controller := range controllers { matched, err := regexp.MatchString("(?i)"+config.HwMon.Platform, controller.Platform) if err != nil { - return errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform)) + return fmt.Errorf("failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform) } if !matched { continue @@ -310,7 +309,7 @@ func UpdateFanConfigFromHwMonControllers(controllers []*HwMonController, config return nil } } - return errors.New(fmt.Sprintf("No hwmon fan matched fan config: %+v", config)) + return fmt.Errorf("no hwmon fan matched fan config: %+v", config) } func setFanConfigPaths(config *configuration.HwMonFanConfig) { diff --git a/internal/sensors/cmd.go b/internal/sensors/cmd.go index 4038b28..a12e72d 100644 --- a/internal/sensors/cmd.go +++ b/internal/sensors/cmd.go @@ -1,7 +1,6 @@ package sensors import ( - "errors" "fmt" "github.com/markusressel/fan2go/internal/configuration" "github.com/markusressel/fan2go/internal/ui" @@ -30,12 +29,12 @@ func (sensor CmdSensor) GetValue() (float64, error) { args := sensor.Config.Cmd.Args result, err := util.SafeCmdExecution(exec, args, timeout) if err != nil { - return 0, errors.New(fmt.Sprintf("Sensor %s: %s", sensor.GetId(), err.Error())) + return 0, fmt.Errorf("sensor %s: %s", sensor.GetId(), err.Error()) } temp, err := strconv.ParseFloat(result, 64) if err != nil { - ui.Warning("Sensor %s: Unable to read int from command output: %s", sensor.GetId(), exec) + ui.Warning("sensor %s: Unable to read int from command output: %s", sensor.GetId(), exec) return 0, err } diff --git a/internal/util/exec.go b/internal/util/exec.go index 27fbf48..69bd904 100644 --- a/internal/util/exec.go +++ b/internal/util/exec.go @@ -2,7 +2,6 @@ package util import ( "context" - "errors" "fmt" "github.com/markusressel/fan2go/internal/ui" "os/exec" @@ -12,7 +11,7 @@ import ( func SafeCmdExecution(executable string, args []string, timeout time.Duration) (string, error) { if _, err := CheckFilePermissionsForExecution(executable); err != nil { - return "", errors.New(fmt.Sprintf("Cannot execute %s: %s", executable, err)) + return "", fmt.Errorf("cannot execute %s: %s", executable, err) } ctx, cancel := context.WithTimeout(context.Background(), timeout) diff --git a/internal/util/file.go b/internal/util/file.go index 2f8a84f..d0608c8 100644 --- a/internal/util/file.go +++ b/internal/util/file.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "github.com/markusressel/fan2go/internal/ui" - "io/ioutil" "os" "path/filepath" "regexp" @@ -50,13 +49,13 @@ func CheckFilePermissionsForExecution(filePath string) (bool, error) { } func ReadIntFromFile(path string) (value int, err error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return -1, err } text := string(data) if len(text) <= 0 { - return 0, errors.New(fmt.Sprintf("File is empty: %s", path)) + return 0, fmt.Errorf("file is empty: %s", path) } text = strings.TrimSpace(text) value, err = strconv.Atoi(text) @@ -70,7 +69,7 @@ func WriteIntToFile(value int, path string) error { path = evaluatedPath } valueAsString := fmt.Sprintf("%d", value) - err = ioutil.WriteFile(path, []byte(valueAsString), 644) + err = os.WriteFile(path, []byte(valueAsString), 644) return err } From 4783ca086adcc4892023309585448ec965c91144 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:22:44 +0100 Subject: [PATCH 06/27] test fixes --- internal/configuration/validation_test.go | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/configuration/validation_test.go b/internal/configuration/validation_test.go index 788fefb..af00d93 100644 --- a/internal/configuration/validation_test.go +++ b/internal/configuration/validation_test.go @@ -54,7 +54,7 @@ func TestValidateDuplicateFanId(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, fmt.Sprintf("Duplicate fan id detected: %s", fanId)) + assert.EqualError(t, err, fmt.Sprintf("duplicate fan id detected: %s", fanId)) } func TestValidateFanSubConfigIsMissing(t *testing.T) { @@ -74,7 +74,7 @@ func TestValidateFanSubConfigIsMissing(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: sub-configuration for fan is missing, use one of: hwmon | file | cmd") + assert.EqualError(t, err, "fan fan: sub-configuration for fan is missing, use one of: hwmon | file | cmd") } func TestValidateFanCurveWithIdIsNotDefined(t *testing.T) { @@ -96,7 +96,7 @@ func TestValidateFanCurveWithIdIsNotDefined(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: no curve definition with id 'curve' found") + assert.EqualError(t, err, "fan fan: no curve definition with id 'curve' found") } func TestValidateCurveSubConfigSensorIdIsMissing(t *testing.T) { @@ -115,7 +115,7 @@ func TestValidateCurveSubConfigSensorIdIsMissing(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve: sub-configuration for curve is missing, use one of: linear | pid | function") + assert.EqualError(t, err, "curve curve: sub-configuration for curve is missing, use one of: linear | pid | function") } func TestValidateCurveSensorIdIsMissing(t *testing.T) { @@ -137,7 +137,7 @@ func TestValidateCurveSensorIdIsMissing(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve: Missing sensorId") + assert.EqualError(t, err, "curve curve: Missing sensorId") } func TestValidateCurveSensorWithIdIsNotDefined(t *testing.T) { @@ -159,7 +159,7 @@ func TestValidateCurveSensorWithIdIsNotDefined(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve: no sensor definition with id 'sensor' found") + assert.EqualError(t, err, "curve curve: no sensor definition with id 'sensor' found") } func TestValidateCurveDependencyToSelf(t *testing.T) { @@ -182,7 +182,7 @@ func TestValidateCurveDependencyToSelf(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve: a curve cannot reference itself") + assert.EqualError(t, err, "curve curve: a curve cannot reference itself") } func TestValidateCurveDependencyCycle(t *testing.T) { @@ -231,7 +231,7 @@ func TestValidateCurveDependencyCycle(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.Contains(t, err.Error(), "You have created a curve dependency cycle") + assert.Contains(t, err.Error(), "you have created a curve dependency cycle") // the order of these items is sometimes different, so we use this // "manual" check to avoid a flaky test assert.Contains(t, err.Error(), "curve1") @@ -258,7 +258,7 @@ func TestValidateCurveDependencyWithIdIsNotDefined(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve1: no curve definition with id 'curve2' found") + assert.EqualError(t, err, "curve curve1: no curve definition with id 'curve2' found") } func TestValidateDuplicateCurveId(t *testing.T) { @@ -298,7 +298,7 @@ func TestValidateDuplicateCurveId(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, fmt.Sprintf("Duplicate curve id detected: %s", curveId)) + assert.EqualError(t, err, fmt.Sprintf("duplicate curve id detected: %s", curveId)) } func TestValidateCurve(t *testing.T) { @@ -352,7 +352,7 @@ func TestValidateCurveFunctionTypeUnsupported(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Curve curve1: unsupported function type 'unsupported', use one of: minimum | average | maximum | delta | sum | difference") + assert.EqualError(t, err, "curve curve1: unsupported function type 'unsupported', use one of: minimum | average | maximum | delta | sum | difference") } func TestValidateSensorSubConfigSensorIdIsMissing(t *testing.T) { @@ -369,7 +369,7 @@ func TestValidateSensorSubConfigSensorIdIsMissing(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Sensor sensor: sub-configuration for sensor is missing, use one of: hwmon | file | cmd") + assert.EqualError(t, err, "sensor sensor: sub-configuration for sensor is missing, use one of: hwmon | file | cmd") } func TestValidateSensor(t *testing.T) { @@ -416,7 +416,7 @@ func TestValidateDuplicateSensorId(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, fmt.Sprintf("Duplicate sensor id detected: %s", sensorId)) + assert.EqualError(t, err, fmt.Sprintf("duplicate sensor id detected: %s", sensorId)) } func TestValidateFanHasIndexOrChannel(t *testing.T) { @@ -454,7 +454,7 @@ func TestValidateFanHasIndexOrChannel(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: must have one of index or rpmChannel, must be >= 1") + assert.EqualError(t, err, "fan fan: must have one of index or rpmChannel, must be >= 1") } func TestValidateFanIndex(t *testing.T) { @@ -494,7 +494,7 @@ func TestValidateFanIndex(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: invalid index, must be >= 1") + assert.EqualError(t, err, "fan fan: invalid index, must be >= 1") } func TestValidateFanChannel(t *testing.T) { @@ -534,7 +534,7 @@ func TestValidateFanChannel(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: invalid rpmChannel, must be >= 1") + assert.EqualError(t, err, "fan fan: invalid rpmChannel, must be >= 1") } func TestValidateFanPwmChannel(t *testing.T) { @@ -575,5 +575,5 @@ func TestValidateFanPwmChannel(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "Fan fan: invalid pwmChannel, must be >= 1") + assert.EqualError(t, err, "fan fan: invalid pwmChannel, must be >= 1") } From 016c972c35415f04591ca6beb8a5098abcecddc2 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:23:55 +0100 Subject: [PATCH 07/27] test fixes --- internal/configuration/validation_test.go | 2 +- internal/hwmon/hwmon_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/configuration/validation_test.go b/internal/configuration/validation_test.go index af00d93..272c8ce 100644 --- a/internal/configuration/validation_test.go +++ b/internal/configuration/validation_test.go @@ -137,7 +137,7 @@ func TestValidateCurveSensorIdIsMissing(t *testing.T) { err := validateConfig(&config, "") // THEN - assert.EqualError(t, err, "curve curve: Missing sensorId") + assert.EqualError(t, err, "curve curve: missing sensorId") } func TestValidateCurveSensorWithIdIsNotDefined(t *testing.T) { diff --git a/internal/hwmon/hwmon_test.go b/internal/hwmon/hwmon_test.go index e877ff3..2cdd7d5 100644 --- a/internal/hwmon/hwmon_test.go +++ b/internal/hwmon/hwmon_test.go @@ -160,7 +160,7 @@ func TestUpdateFanConfigFromHwMonControllers(t *testing.T) { configConfig: configuration.HwMonFanConfig{ Index: 1, }, - wantErr: "No hwmon fan matched fan config", + wantErr: "no hwmon fan matched fan config", }, { tn: "no matching index", hwMonConfigs: []configuration.HwMonFanConfig{ @@ -171,7 +171,7 @@ func TestUpdateFanConfigFromHwMonControllers(t *testing.T) { configConfig: configuration.HwMonFanConfig{ Index: 1, }, - wantErr: "No hwmon fan matched fan config", + wantErr: "no hwmon fan matched fan config", }, { tn: "no matching platform", hwMonConfigs: []configuration.HwMonFanConfig{ @@ -183,7 +183,7 @@ func TestUpdateFanConfigFromHwMonControllers(t *testing.T) { configConfig: configuration.HwMonFanConfig{ Index: 1, }, - wantErr: "No hwmon fan matched fan config", + wantErr: "no hwmon fan matched fan config", }} for _, tt := range tests { From 2365e06506e35998036c6a916a62be9eed5da5f8 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:29:52 +0100 Subject: [PATCH 08/27] lint fix --- cmd/fan/curve.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/fan/curve.go b/cmd/fan/curve.go index 2ae10bf..3c42b8d 100644 --- a/cmd/fan/curve.go +++ b/cmd/fan/curve.go @@ -39,7 +39,7 @@ var curveCmd = &cobra.Command{ } for idx, fan := range fanList { - if &fanId != nil && fan.GetId() != fanId { + if fan.GetId() != fanId { continue } From 6bb304d6ad5e19c2541141c37f45529e2b1a9d61 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:35:35 +0100 Subject: [PATCH 09/27] lint fixes --- cmd/fan/speed.go | 6 ++++-- internal/curves/functional_test.go | 5 ++++- internal/curves/pid.go | 3 ++- internal/curves/pid_test.go | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/fan/speed.go b/cmd/fan/speed.go index ed04082..1ae730c 100644 --- a/cmd/fan/speed.go +++ b/cmd/fan/speed.go @@ -21,13 +21,15 @@ var speedCmd = &cobra.Command{ } if len(args) > 0 { - pwmValue, err := strconv.Atoi(args[0]) + var pwmValue int + pwmValue, err = strconv.Atoi(args[0]) if err != nil { return err } err = fan.SetPwm(pwmValue) } else { - if pwm, err := fan.GetPwm(); err == nil { + var pwm int + if pwm, err = fan.GetPwm(); err == nil { fmt.Printf("%d", pwm) } } diff --git a/internal/curves/functional_test.go b/internal/curves/functional_test.go index 28c64bb..3953e33 100644 --- a/internal/curves/functional_test.go +++ b/internal/curves/functional_test.go @@ -48,6 +48,7 @@ func TestFunctionCurveSum(t *testing.T) { 40, 80, ) + c1, err := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 @@ -57,7 +58,9 @@ func TestFunctionCurveSum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + + var c2 SpeedCurve + c2, err = NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionSum diff --git a/internal/curves/pid.go b/internal/curves/pid.go index 5b34f2f..b008e7d 100644 --- a/internal/curves/pid.go +++ b/internal/curves/pid.go @@ -19,7 +19,8 @@ func (c PidSpeedCurve) GetId() string { func (c PidSpeedCurve) Evaluate() (value int, err error) { sensor := sensors.SensorMap[c.Config.PID.Sensor] - measured, err := sensor.GetValue() + var measured float64 + measured, err = sensor.GetValue() pidTarget := c.Config.PID.SetPoint loopValue := c.pidLoop.Loop(pidTarget, measured/1000.0) diff --git a/internal/curves/pid_test.go b/internal/curves/pid_test.go index c71a07e..ab40429 100644 --- a/internal/curves/pid_test.go +++ b/internal/curves/pid_test.go @@ -398,7 +398,8 @@ func TestPidCurveOnTarget(t *testing.T) { curve, err := NewSpeedCurve(curveConfig) // WHEN - result, err := curve.Evaluate() + var result int + result, err = curve.Evaluate() if err != nil { assert.Fail(t, err.Error()) } From fc2a16955bd68b6eacaa0ab33413d9db5d46c764 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:36:41 +0100 Subject: [PATCH 10/27] lint fixes --- internal/fans/cmd.go | 3 --- internal/fans/file.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/internal/fans/cmd.go b/internal/fans/cmd.go index 17a9240..9c72e24 100644 --- a/internal/fans/cmd.go +++ b/internal/fans/cmd.go @@ -36,7 +36,6 @@ func (fan CmdFan) GetMinPwm() int { func (fan *CmdFan) SetMinPwm(pwm int, force bool) { // not supported - return } func (fan CmdFan) GetMaxPwm() int { @@ -45,7 +44,6 @@ func (fan CmdFan) GetMaxPwm() int { func (fan *CmdFan) SetMaxPwm(pwm int, force bool) { // not supported - return } func (fan *CmdFan) GetRpm() (int, error) { @@ -78,7 +76,6 @@ func (fan CmdFan) GetRpmAvg() float64 { func (fan *CmdFan) SetRpmAvg(rpm float64) { // not supported - return } func (fan *CmdFan) GetPwm() (result int, err error) { diff --git a/internal/fans/file.go b/internal/fans/file.go index 78d3683..34fed40 100644 --- a/internal/fans/file.go +++ b/internal/fans/file.go @@ -34,7 +34,6 @@ func (fan FileFan) GetMinPwm() int { func (fan *FileFan) SetMinPwm(pwm int, force bool) { // not supported - return } func (fan FileFan) GetMaxPwm() int { @@ -43,7 +42,6 @@ func (fan FileFan) GetMaxPwm() int { func (fan *FileFan) SetMaxPwm(pwm int, force bool) { // not supported - return } func (fan FileFan) GetRpm() (int, error) { @@ -56,7 +54,6 @@ func (fan FileFan) GetRpmAvg() float64 { func (fan *FileFan) SetRpmAvg(rpm float64) { // not supported - return } func (fan *FileFan) GetPwm() (result int, err error) { From 7fcc9897d70969fe5011a2c89c4ef224ac713ddc Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:37:24 +0100 Subject: [PATCH 11/27] lint fix --- internal/controller/controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index c4612ac..7ff15d8 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -506,7 +506,7 @@ func (f *PidFanController) mapToClosestDistinct(target int) int { // computePwmMap computes a mapping between "requested pwm value" -> "actual set pwm value" func (f *PidFanController) computePwmMap() (err error) { - if configuration.CurrentConfig.RunFanInitializationInParallel == false { + if !configuration.CurrentConfig.RunFanInitializationInParallel { InitializationSequenceMutex.Lock() defer InitializationSequenceMutex.Unlock() } From 61d1391106915dac1287795744bda7f9d1ed8dbc Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:41:50 +0100 Subject: [PATCH 12/27] lint fix --- internal/curves/functional_test.go | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/curves/functional_test.go b/internal/curves/functional_test.go index 3953e33..854d386 100644 --- a/internal/curves/functional_test.go +++ b/internal/curves/functional_test.go @@ -49,7 +49,7 @@ func TestFunctionCurveSum(t *testing.T) { 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -60,7 +60,7 @@ func TestFunctionCurveSum(t *testing.T) { ) var c2 SpeedCurve - c2, err = NewSpeedCurve(curve2) + c2, _ = NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionSum @@ -72,7 +72,7 @@ func TestFunctionCurveSum(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -110,7 +110,7 @@ func TestFunctionCurveDifference(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -119,7 +119,7 @@ func TestFunctionCurveDifference(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionDifference @@ -131,7 +131,7 @@ func TestFunctionCurveDifference(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -169,7 +169,7 @@ func TestFunctionCurveAverage(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -178,7 +178,7 @@ func TestFunctionCurveAverage(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionAverage @@ -190,7 +190,7 @@ func TestFunctionCurveAverage(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -228,7 +228,7 @@ func TestFunctionCurveDelta(t *testing.T) { 18, 60, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -237,7 +237,7 @@ func TestFunctionCurveDelta(t *testing.T) { 18, 60, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionDelta @@ -249,7 +249,7 @@ func TestFunctionCurveDelta(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -287,7 +287,7 @@ func TestFunctionCurveMinimum(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -296,7 +296,7 @@ func TestFunctionCurveMinimum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionMinimum @@ -308,7 +308,7 @@ func TestFunctionCurveMinimum(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) // WHEN result, err := functionCurve.Evaluate() @@ -345,7 +345,7 @@ func TestFunctionCurveMaximum(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -354,7 +354,7 @@ func TestFunctionCurveMaximum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionMaximum @@ -366,7 +366,7 @@ func TestFunctionCurveMaximum(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) // WHEN result, err := functionCurve.Evaluate() From e602f8d33d125ffbb4ae45894280b16588e7ff38 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:44:17 +0100 Subject: [PATCH 13/27] lint fix --- internal/curves/pid.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/curves/pid.go b/internal/curves/pid.go index b008e7d..3f04ea2 100644 --- a/internal/curves/pid.go +++ b/internal/curves/pid.go @@ -21,6 +21,9 @@ func (c PidSpeedCurve) Evaluate() (value int, err error) { sensor := sensors.SensorMap[c.Config.PID.Sensor] var measured float64 measured, err = sensor.GetValue() + if err != nil { + return c.Value, err + } pidTarget := c.Config.PID.SetPoint loopValue := c.pidLoop.Loop(pidTarget, measured/1000.0) From 4355186eaf1acae3df186d5e1b251b9f3f17a901 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:45:57 +0100 Subject: [PATCH 14/27] lint fix --- internal/curves/pid_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/curves/pid_test.go b/internal/curves/pid_test.go index ab40429..939bf10 100644 --- a/internal/curves/pid_test.go +++ b/internal/curves/pid_test.go @@ -395,11 +395,10 @@ func TestPidCurveOnTarget(t *testing.T) { -0.005, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) + curve, _ := NewSpeedCurve(curveConfig) // WHEN - var result int - result, err = curve.Evaluate() + result, err := curve.Evaluate() if err != nil { assert.Fail(t, err.Error()) } From f009d722656f1f1b6252cd63331ba57acea57f28 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:46:35 +0100 Subject: [PATCH 15/27] lint fix --- internal/curves/pid_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/curves/pid_test.go b/internal/curves/pid_test.go index 939bf10..10aa232 100644 --- a/internal/curves/pid_test.go +++ b/internal/curves/pid_test.go @@ -398,13 +398,14 @@ func TestPidCurveOnTarget(t *testing.T) { curve, _ := NewSpeedCurve(curveConfig) // WHEN - result, err := curve.Evaluate() + _, err := curve.Evaluate() if err != nil { assert.Fail(t, err.Error()) } time.Sleep(1 * time.Second) + var result int result, err = curve.Evaluate() if err != nil { assert.Fail(t, err.Error()) From 9d7f8959d716c8e05facc9fe222fcf5e0d74f8fe Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:47:07 +0100 Subject: [PATCH 16/27] lint fix --- internal/fans/cmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/fans/cmd.go b/internal/fans/cmd.go index 9c72e24..f2fc2e5 100644 --- a/internal/fans/cmd.go +++ b/internal/fans/cmd.go @@ -27,7 +27,6 @@ func (fan CmdFan) GetStartPwm() int { } func (fan *CmdFan) SetStartPwm(pwm int, force bool) { - return } func (fan CmdFan) GetMinPwm() int { From b79ab02dbd83b0082ca39d1221f3470501298fb0 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:47:23 +0100 Subject: [PATCH 17/27] lint fix --- internal/fans/file.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/fans/file.go b/internal/fans/file.go index 34fed40..282772f 100644 --- a/internal/fans/file.go +++ b/internal/fans/file.go @@ -25,7 +25,6 @@ func (fan FileFan) GetStartPwm() int { } func (fan *FileFan) SetStartPwm(pwm int, force bool) { - return } func (fan FileFan) GetMinPwm() int { From 6c4ca82f769aafdf1a226c31b4ffa1a659f6d85e Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:47:53 +0100 Subject: [PATCH 18/27] lint fix --- internal/util/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/util/file.go b/internal/util/file.go index d0608c8..8a48188 100644 --- a/internal/util/file.go +++ b/internal/util/file.go @@ -69,7 +69,7 @@ func WriteIntToFile(value int, path string) error { path = evaluatedPath } valueAsString := fmt.Sprintf("%d", value) - err = os.WriteFile(path, []byte(valueAsString), 644) + err = os.WriteFile(path, []byte(valueAsString), 0644) return err } From d041ce02c347274bbc623c1d1ae8f0e693982f9e Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:49:13 +0100 Subject: [PATCH 19/27] lint fix --- internal/ui/notification.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ui/notification.go b/internal/ui/notification.go index d0206c6..101a244 100644 --- a/internal/ui/notification.go +++ b/internal/ui/notification.go @@ -60,7 +60,7 @@ func NotifySend(urgency, title, text, icon string) { output, err = cmd.Output() userIdString := strings.TrimSpace(string(output)) if len(userIdString) <= 0 { - Warning("Cannot send notification, unable to detect user id") + Warning("Cannot send notification, unable to detect user id: %s", err.Error()) return } From c655fdfd3aeb2044851b0b9672ef5d9bbf6f6114 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:49:50 +0100 Subject: [PATCH 20/27] lint fix --- cmd/root.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d12f9b0..8dd92c8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/pterm/pterm/putils" "os" "github.com/markusressel/fan2go/cmd/config" @@ -66,9 +67,9 @@ func setupUi() { // Print a large text with the LetterStyle from the standard theme. func printHeader() { err := pterm.DefaultBigText.WithLetters( - pterm.NewLettersFromStringWithStyle("fan", pterm.NewStyle(pterm.FgLightBlue)), - pterm.NewLettersFromStringWithStyle("2", pterm.NewStyle(pterm.FgWhite)), - pterm.NewLettersFromStringWithStyle("go", pterm.NewStyle(pterm.FgLightBlue)), + putils.LettersFromStringWithStyle("fan", pterm.NewStyle(pterm.FgLightBlue)), + putils.LettersFromStringWithStyle("2", pterm.NewStyle(pterm.FgWhite)), + putils.LettersFromStringWithStyle("go", pterm.NewStyle(pterm.FgLightBlue)), ).Render() if err != nil { fmt.Println("fan2go") From c671151f845aa94a9d6dc41f8972005c1bc17b18 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:54:35 +0100 Subject: [PATCH 21/27] lint fix --- internal/curves/linear_test.go | 4 +-- internal/curves/pid_test.go | 55 +++++++--------------------------- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/internal/curves/linear_test.go b/internal/curves/linear_test.go index 14e8565..7fc20e3 100644 --- a/internal/curves/linear_test.go +++ b/internal/curves/linear_test.go @@ -57,7 +57,7 @@ func TestLinearCurveWithMinMax(t *testing.T) { 40, 80, ) - curve, err := NewSpeedCurve(curveConfig) + curve, _ := NewSpeedCurve(curveConfig) // WHEN result, err := curve.Evaluate() @@ -88,7 +88,7 @@ func TestLinearCurveWithSteps(t *testing.T) { 70: 255, }, ) - curve, err := NewSpeedCurve(curveConfig) + curve, _ := NewSpeedCurve(curveConfig) // WHEN result, err := curve.Evaluate() diff --git a/internal/curves/pid_test.go b/internal/curves/pid_test.go index 10aa232..67dcd43 100644 --- a/internal/curves/pid_test.go +++ b/internal/curves/pid_test.go @@ -50,10 +50,7 @@ func TestPidCurveProportionalBelowTarget(t *testing.T) { 0, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 0, 0} { // WHEN @@ -87,10 +84,7 @@ func TestPidCurveProportionalAboveTarget(t *testing.T) { 0, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 127, 127} { // WHEN @@ -124,10 +118,7 @@ func TestPidCurveProportionalWayAboveTarget(t *testing.T) { 0, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 255, 255} { // WHEN @@ -163,10 +154,7 @@ func TestPidCurveIntegralBelowTarget(t *testing.T) { -0.005, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 0, 0} { // WHEN @@ -200,10 +188,7 @@ func TestPidCurveIntegralAboveTarget(t *testing.T) { -0.005, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 2, 5} { // WHEN @@ -237,10 +222,7 @@ func TestPidCurveIntegralWayAboveTarget(t *testing.T) { -0.005, 0, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 5, 10} { // WHEN @@ -276,10 +258,7 @@ func TestPidCurveDerivativeNoDiff(t *testing.T) { 0, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 0, 0} { // WHEN @@ -313,10 +292,7 @@ func TestPidCurveDerivativePositiveStaticDiff(t *testing.T) { 0, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 7, 7} { // WHEN @@ -353,10 +329,7 @@ func TestPidCurveDerivativeIncreasingDiff(t *testing.T) { 0, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 7, 15, 22, 30} { // WHEN @@ -433,10 +406,7 @@ func TestPidCurveAboveTarget(t *testing.T) { -0.005, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 15, 17, 20, 22, 25} { // WHEN @@ -470,10 +440,7 @@ func TestPidCurveWayAboveTarget(t *testing.T) { -0.005, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) - if err != nil { - assert.Fail(t, err.Error()) - } + curve, _ := NewSpeedCurve(curveConfig) for loopIdx, expected := range []int{0, 30, 35, 40, 45, 51} { // WHEN From da5a3edca4ceed32ad91e3805959e874d63ae9f1 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:58:04 +0100 Subject: [PATCH 22/27] lint fix --- internal/curves/functional.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/curves/functional.go b/internal/curves/functional.go index 53ef039..a51c2f4 100644 --- a/internal/curves/functional.go +++ b/internal/curves/functional.go @@ -11,11 +11,11 @@ type FunctionSpeedCurve struct { Value int `json:"value"` } -func (c FunctionSpeedCurve) GetId() string { +func (c *FunctionSpeedCurve) GetId() string { return c.Config.ID } -func (c FunctionSpeedCurve) Evaluate() (value int, err error) { +func (c *FunctionSpeedCurve) Evaluate() (value int, err error) { var curves []SpeedCurve for _, curveId := range c.Config.Function.Curves { curves = append(curves, SpeedCurveMap[curveId]) From f26c8f2f4a5a940d36e7f268527a9b25e85e5636 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 17:58:37 +0100 Subject: [PATCH 23/27] lint fix --- internal/curves/linear.go | 4 ++-- internal/curves/pid.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/curves/linear.go b/internal/curves/linear.go index 2091abb..56d9b5d 100644 --- a/internal/curves/linear.go +++ b/internal/curves/linear.go @@ -12,11 +12,11 @@ type LinearSpeedCurve struct { Value int `json:"value"` } -func (c LinearSpeedCurve) GetId() string { +func (c *LinearSpeedCurve) GetId() string { return c.Config.ID } -func (c LinearSpeedCurve) Evaluate() (value int, err error) { +func (c *LinearSpeedCurve) Evaluate() (value int, err error) { sensor := sensors.SensorMap[c.Config.Linear.Sensor] var avgTemp = sensor.GetMovingAvg() diff --git a/internal/curves/pid.go b/internal/curves/pid.go index 3f04ea2..8fc1524 100644 --- a/internal/curves/pid.go +++ b/internal/curves/pid.go @@ -13,11 +13,11 @@ type PidSpeedCurve struct { pidLoop *util.PidLoop } -func (c PidSpeedCurve) GetId() string { +func (c *PidSpeedCurve) GetId() string { return c.Config.ID } -func (c PidSpeedCurve) Evaluate() (value int, err error) { +func (c *PidSpeedCurve) Evaluate() (value int, err error) { sensor := sensors.SensorMap[c.Config.PID.Sensor] var measured float64 measured, err = sensor.GetValue() From a8c14a406ee763988c01213187cea25540c6f830 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 18:23:28 +0100 Subject: [PATCH 24/27] lint fix --- internal/controller/controller.go | 8 ++++---- internal/monitor.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/controller/controller.go b/internal/controller/controller.go index 7ff15d8..41ebd3c 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -169,13 +169,13 @@ func (f *PidFanController) Run(ctx context.Context) error { pollingRate := configuration.CurrentConfig.RpmPollingRate g.Add(func() error { - tick := time.Tick(pollingRate) + tick := time.NewTicker(pollingRate) for { select { case <-ctx.Done(): ui.Info("Stopping RPM monitor of fan controller for fan %s...", fan.GetId()) return nil - case <-tick: + case <-tick.C: measureRpm(fan) } } @@ -189,14 +189,14 @@ func (f *PidFanController) Run(ctx context.Context) error { { g.Add(func() error { time.Sleep(1 * time.Second) - tick := time.Tick(f.updateRate) + tick := time.NewTicker(f.updateRate) for { select { case <-ctx.Done(): ui.Info("Stopping fan controller for fan %s...", fan.GetId()) f.restorePwmEnabled() return nil - case <-tick: + case <-tick.C: err = f.UpdateFanSpeed() if err != nil { ui.ErrorAndNotify("Fan Control Error", "Fan %s: %v", fan.GetId(), err) diff --git a/internal/monitor.go b/internal/monitor.go index d1ea79d..bd02418 100644 --- a/internal/monitor.go +++ b/internal/monitor.go @@ -26,13 +26,13 @@ func NewSensorMonitor(sensor sensors.Sensor, pollingRate time.Duration) SensorMo } func (s sensorMonitor) Run(ctx context.Context) error { - tick := time.Tick(s.pollingRate) + tick := time.NewTicker(s.pollingRate) for { select { case <-ctx.Done(): ui.Info("Stopping sensor monitor for sensor %s...", s.sensor.GetId()) return nil - case <-tick: + case <-tick.C: err := updateSensor(s.sensor) if err != nil { ui.Warning("Error updating sensor: %v", err) From ad69639f0d3fde13a7eb67716d2ca30555d85637 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 18:23:58 +0100 Subject: [PATCH 25/27] lint fix --- internal/persistence/persistence_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/persistence/persistence_test.go b/internal/persistence/persistence_test.go index 6ee2fa7..976f289 100644 --- a/internal/persistence/persistence_test.go +++ b/internal/persistence/persistence_test.go @@ -30,10 +30,10 @@ func TestPersistence_DeleteFanPwmData(t *testing.T) { // GIVEN p := NewPersistence(dbTestingPath) fan, _ := createFan(false, LinearFan) - err := p.SaveFanPwmData(fan) + _ = p.SaveFanPwmData(fan) // WHEN - err = p.DeleteFanPwmData(fan) + err := p.DeleteFanPwmData(fan) assert.NoError(t, err) // THEN From 5ffaa000361998ce51e4e6c0856ac899019aa1a2 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 18:29:10 +0100 Subject: [PATCH 26/27] lint fix --- internal/backend.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backend.go b/internal/backend.go index 622f3c0..1aed76e 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -147,7 +147,7 @@ func RunDaemon() { } } { - sig := make(chan os.Signal) + sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill) g.Add(func() error { From b4f92983e2a85caed80804ca9ae148f5b4f9ded6 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Wed, 15 Mar 2023 18:32:03 +0100 Subject: [PATCH 27/27] lint fix --- internal/backend.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backend.go b/internal/backend.go index 1aed76e..f76760d 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -148,7 +148,7 @@ func RunDaemon() { } { sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) g.Add(func() error { <-sig