Skip to content

Commit

Permalink
fix: apply corrections from golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitdm-oslandia committed Dec 9, 2024
1 parent 9e05870 commit 9d98c56
Show file tree
Hide file tree
Showing 20 changed files with 272 additions and 199 deletions.
30 changes: 30 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
linters:
# Enable all available linters.
# Default: false
enable-all: true
# Disable specific linter
# https://golangci-lint.run/usage/linters/#disabled-by-default
disable:
- copyloopvar # we use a too old go version
- intrange # we use a too old go version
- execinquery # deprecated
- exportloopref # deprecated
# - depguard
# - gci
# - godox
# - gofumpt
- gomnd
- wsl
- wrapcheck # old code => no wrapped error
- varnamelen
- paralleltest
- ireturn
- mnd
- testpackage
- err113 # old code => no wrapped error
- stylecheck
- cyclop
# - revive
- nlreturn
- exhaustruct

9 changes: 7 additions & 2 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

// ConsoleLogger logs to the std err.
var ConsoleLogger = consoleLogger{}
var ConsoleLogger = consoleLogger{} //nolint:gochecknoglobals

type consoleLogger struct {
info, warn, err *log.Logger
}

func init() {
func init() { //nolint:gochecknoinits
ConsoleLogger.info = log.New(os.Stderr, "I: ", log.Ltime)
ConsoleLogger.warn = log.New(os.Stderr, "W: ", log.Ltime)
ConsoleLogger.err = log.New(os.Stderr, "E: ", log.Ltime)
Expand All @@ -26,22 +26,27 @@ func (c consoleLogger) Error(v ...interface{}) error {
c.err.Print(v...)
return nil
}

func (c consoleLogger) Warning(v ...interface{}) error {
c.warn.Print(v...)
return nil
}

func (c consoleLogger) Info(v ...interface{}) error {
c.info.Print(v...)
return nil
}

func (c consoleLogger) Errorf(format string, a ...interface{}) error {
c.err.Printf(format, a...)
return nil
}

func (c consoleLogger) Warningf(format string, a ...interface{}) error {
c.warn.Printf(format, a...)
return nil
}

func (c consoleLogger) Infof(format string, a ...interface{}) error {
c.info.Printf(format, a...)
return nil
Expand Down
23 changes: 16 additions & 7 deletions example/logging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"log"
"time"

"github.com/kardianos/service"
"github.com/kardianos/service" //nolint:depguard
)

var logger service.Logger
var logger service.Logger //nolint:gochecknoglobals

// Program structures.
//
Expand All @@ -22,7 +22,8 @@ type program struct {
exit chan struct{}
}

func (p *program) Start(s service.Service) error {
//nolint:errcheck
func (p *program) Start(_ service.Service) error {
if service.Interactive() {
logger.Info("Running in terminal.")
} else {
Expand All @@ -34,6 +35,8 @@ func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

//nolint:errcheck,unparam
func (p *program) run() error {
logger.Infof("I'm running %v.", service.Platform())
ticker := time.NewTicker(2 * time.Second)
Expand All @@ -47,7 +50,9 @@ func (p *program) run() error {
}
}
}
func (p *program) Stop(s service.Service) error {

//nolint:errcheck
func (p *program) Stop(_ service.Service) error {
// Any work in Stop should be quick, usually a few seconds at most.
logger.Info("I'm Stopping!")
close(p.exit)
Expand All @@ -74,7 +79,8 @@ func main() {
Description: "This is an example Go service that outputs log messages.",
Dependencies: []string{
"Requires=network.target",
"After=network-online.target syslog.target"},
"After=network-online.target syslog.target",
},
Option: options,
}

Expand All @@ -101,13 +107,16 @@ func main() {
if len(*svcFlag) != 0 {
err := service.Control(s, *svcFlag)
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Printf("Valid actions: %q\n", []string{
service.ControlActionStart, service.ControlActionStop,
service.ControlActionRestart, service.ControlActionInstall, service.ControlActionUninstall,
})
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}
44 changes: 26 additions & 18 deletions example/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ import (
"os/exec"
"path/filepath"

"github.com/kardianos/service"
"github.com/kardianos/service" //nolint:depguard
)

// Config is the runner app config structure.
type Config struct {
Name, DisplayName, Description string
Name string `json:"name"`
DisplayName string `json:"displyName"`
Description string `json:"description"`

Dir string
Exec string
Args []string
Env []string
Dir string `json:"dir"`
Exec string `json:"exec"`
Args []string `json:"args"`
Env []string `json:"env"`

Stderr, Stdout string
Stderr string `json:"stderr"`
Stdout string `json:"stdout"`
}

var logger service.Logger
var logger service.Logger //nolint:gochecknoglobals

type program struct {
exit chan struct{}
Expand All @@ -40,12 +43,12 @@ type program struct {
cmd *exec.Cmd
}

func (p *program) Start(s service.Service) error {
func (p *program) Start(_ service.Service) error {
// Look for exec.
// Verify home directory.
fullExec, err := exec.LookPath(p.Exec)
if err != nil {
return fmt.Errorf("Failed to find executable %q: %v", p.Exec, err)
return fmt.Errorf("Failed to find executable %q: %w", p.Exec, err)
}

p.cmd = exec.Command(fullExec, p.Args...)
Expand All @@ -55,6 +58,8 @@ func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

//nolint:errcheck
func (p *program) run() {
logger.Info("Starting ", p.DisplayName)
defer func() {
Expand All @@ -66,16 +71,16 @@ func (p *program) run() {
}()

if p.Stderr != "" {
f, err := os.OpenFile(p.Stderr, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777)
f, err := os.OpenFile(p.Stderr, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o777)
if err != nil {
logger.Warningf("Failed to open std err %q: %v", p.Stderr, err)
logger.Warningf("Failed to open std err %q: %w", p.Stderr, err)
return
}
defer f.Close()
p.cmd.Stderr = f
}
if p.Stdout != "" {
f, err := os.OpenFile(p.Stdout, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777)
f, err := os.OpenFile(p.Stdout, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o777)
if err != nil {
logger.Warningf("Failed to open std out %q: %v", p.Stdout, err)
return
Expand All @@ -88,10 +93,10 @@ func (p *program) run() {
if err != nil {
logger.Warningf("Error running: %v", err)
}

return
}
func (p *program) Stop(s service.Service) error {

//nolint:errcheck
func (p *program) Stop(_ service.Service) error {
close(p.exit)
logger.Info("Stopping ", p.DisplayName)
if p.cmd.Process != nil {
Expand Down Expand Up @@ -181,13 +186,16 @@ func main() {
if len(*svcFlag) != 0 {
err := service.Control(s, *svcFlag)
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Printf("Valid actions: %q\n", []string{
service.ControlActionStart, service.ControlActionStop,
service.ControlActionRestart, service.ControlActionInstall, service.ControlActionUninstall,
})
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}
12 changes: 7 additions & 5 deletions example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ package main
import (
"log"

"github.com/kardianos/service"
"github.com/kardianos/service" //nolint:depguard
)

var logger service.Logger
var logger service.Logger //nolint:gochecknoglobals

type program struct{}

func (p *program) Start(s service.Service) error {
func (p *program) Start(_ service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}

func (p *program) run() {
// Do work here
}
func (p *program) Stop(s service.Service) error {

func (p *program) Stop(_ service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
Expand All @@ -46,6 +48,6 @@ func main() {
}
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}
12 changes: 7 additions & 5 deletions example/stopPause/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ import (
"os"
"time"

"github.com/kardianos/service"
"github.com/kardianos/service" //nolint:depguard
)

var logger service.Logger
var logger service.Logger //nolint:gochecknoglobals

type program struct{}

func (p *program) Start(s service.Service) error {
func (p *program) Start(_ service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}

func (p *program) run() {
// Do work here
}
func (p *program) Stop(s service.Service) error {

func (p *program) Stop(_ service.Service) error {
// Stop should not block. Return with a few seconds.
<-time.After(time.Second * 13)
return nil
Expand Down Expand Up @@ -57,6 +59,6 @@ func main() {
}
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}
Loading

0 comments on commit 9d98c56

Please sign in to comment.