diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 58c6ab4..719b4ed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -27,6 +27,32 @@ jobs: - name: Test run: make test + - 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 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/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/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 } 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/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/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") 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/backend.go b/internal/backend.go index 518d3b1..f76760d 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -60,11 +60,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()) @@ -82,20 +80,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()) @@ -151,8 +147,8 @@ func RunDaemon() { } } { - sig := make(chan os.Signal) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill) + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) g.Add(func() error { <-sig 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/configuration/validation_test.go b/internal/configuration/validation_test.go index 788fefb..272c8ce 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") } diff --git a/internal/controller/controller.go b/internal/controller/controller.go index e687c49..41ebd3c 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() @@ -168,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) } } @@ -188,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) @@ -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 { @@ -504,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() } @@ -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/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]) diff --git a/internal/curves/functional_test.go b/internal/curves/functional_test.go index 28c64bb..854d386 100644 --- a/internal/curves/functional_test.go +++ b/internal/curves/functional_test.go @@ -48,7 +48,8 @@ func TestFunctionCurveSum(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -57,7 +58,9 @@ func TestFunctionCurveSum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + + var c2 SpeedCurve + c2, _ = NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionSum @@ -69,7 +72,7 @@ func TestFunctionCurveSum(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -107,7 +110,7 @@ func TestFunctionCurveDifference(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -116,7 +119,7 @@ func TestFunctionCurveDifference(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionDifference @@ -128,7 +131,7 @@ func TestFunctionCurveDifference(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -166,7 +169,7 @@ func TestFunctionCurveAverage(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -175,7 +178,7 @@ func TestFunctionCurveAverage(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionAverage @@ -187,7 +190,7 @@ func TestFunctionCurveAverage(t *testing.T) { c2.GetId(), }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -225,7 +228,7 @@ func TestFunctionCurveDelta(t *testing.T) { 18, 60, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -234,7 +237,7 @@ func TestFunctionCurveDelta(t *testing.T) { 18, 60, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionDelta @@ -246,7 +249,7 @@ func TestFunctionCurveDelta(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) SpeedCurveMap[functionCurve.GetId()] = functionCurve // WHEN @@ -284,7 +287,7 @@ func TestFunctionCurveMinimum(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -293,7 +296,7 @@ func TestFunctionCurveMinimum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionMinimum @@ -305,7 +308,7 @@ func TestFunctionCurveMinimum(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) // WHEN result, err := functionCurve.Evaluate() @@ -342,7 +345,7 @@ func TestFunctionCurveMaximum(t *testing.T) { 40, 80, ) - c1, err := NewSpeedCurve(curve1) + c1, _ := NewSpeedCurve(curve1) SpeedCurveMap[c1.GetId()] = c1 curve2 := createLinearCurveConfig( @@ -351,7 +354,7 @@ func TestFunctionCurveMaximum(t *testing.T) { 40, 80, ) - c2, err := NewSpeedCurve(curve2) + c2, _ := NewSpeedCurve(curve2) SpeedCurveMap[c2.GetId()] = c2 function := configuration.FunctionMaximum @@ -363,7 +366,7 @@ func TestFunctionCurveMaximum(t *testing.T) { curve2.ID, }, ) - functionCurve, err := NewSpeedCurve(functionCurveConfig) + functionCurve, _ := NewSpeedCurve(functionCurveConfig) // WHEN result, err := functionCurve.Evaluate() 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/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.go b/internal/curves/pid.go index 5b34f2f..8fc1524 100644 --- a/internal/curves/pid.go +++ b/internal/curves/pid.go @@ -13,13 +13,17 @@ 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] - measured, err := sensor.GetValue() + 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) diff --git a/internal/curves/pid_test.go b/internal/curves/pid_test.go index c71a07e..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 @@ -395,16 +368,17 @@ func TestPidCurveOnTarget(t *testing.T) { -0.005, -0.006, ) - curve, err := NewSpeedCurve(curveConfig) + 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()) @@ -432,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 @@ -469,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 diff --git a/internal/fans/cmd.go b/internal/fans/cmd.go index 25b5bc4..f2fc2e5 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" @@ -28,7 +27,6 @@ func (fan CmdFan) GetStartPwm() int { } func (fan *CmdFan) SetStartPwm(pwm int, force bool) { - return } func (fan CmdFan) GetMinPwm() int { @@ -37,7 +35,6 @@ func (fan CmdFan) GetMinPwm() int { func (fan *CmdFan) SetMinPwm(pwm int, force bool) { // not supported - return } func (fan CmdFan) GetMaxPwm() int { @@ -46,7 +43,6 @@ func (fan CmdFan) GetMaxPwm() int { func (fan *CmdFan) SetMaxPwm(pwm int, force bool) { // not supported - return } func (fan *CmdFan) GetRpm() (int, error) { @@ -79,7 +75,6 @@ func (fan CmdFan) GetRpmAvg() float64 { func (fan *CmdFan) SetRpmAvg(rpm float64) { // not supported - return } func (fan *CmdFan) GetPwm() (result int, err error) { @@ -114,7 +109,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/file.go b/internal/fans/file.go index 78d3683..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 { @@ -34,7 +33,6 @@ func (fan FileFan) GetMinPwm() int { func (fan *FileFan) SetMinPwm(pwm int, force bool) { // not supported - return } func (fan FileFan) GetMaxPwm() int { @@ -43,7 +41,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 +53,6 @@ func (fan FileFan) GetRpmAvg() float64 { func (fan *FileFan) SetRpmAvg(rpm float64) { // not supported - return } func (fan *FileFan) GetPwm() (result int, err error) { 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/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 { 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) 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 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/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 } 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..8a48188 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), 0644) return err } 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)