Fix tests and linters

This commit is contained in:
Daniel
2022-02-02 12:48:42 +01:00
parent f2fcad4d11
commit 60d8664e7b
171 changed files with 944 additions and 874 deletions

View File

@@ -1,6 +1,6 @@
package main
import (
import ( //nolint:gci,nolintlint
"os"
"github.com/safing/portbase/info"
@@ -8,7 +8,7 @@ import (
"github.com/safing/portbase/run"
"github.com/safing/spn/conf"
// include packages here
// Include packages here.
_ "github.com/safing/portbase/modules/subsystems"
_ "github.com/safing/portmaster/core"
_ "github.com/safing/portmaster/firewall"
@@ -22,7 +22,7 @@ func main() {
info.Set("Portmaster", "0.7.21", "AGPLv3", true)
// Configure metrics.
metrics.SetNamespace("portmaster")
_ = metrics.SetNamespace("portmaster")
// enable SPN client mode
conf.EnableClient(true)

View File

@@ -1,4 +1,4 @@
// +build !windows
// go:build !windows
package main
@@ -8,5 +8,4 @@ func attachToParentConsole() (attached bool, err error) {
return true, nil
}
func hideWindow(cmd *exec.Cmd) {
}
func hideWindow(cmd *exec.Cmd) {}

View File

@@ -86,7 +86,7 @@ func createInstanceLock(lockFilePath string) error {
// create lock file
// TODO: Investigate required permissions.
err = ioutil.WriteFile(lockFilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0666) //nolint:gosec
err = ioutil.WriteFile(lockFilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0o0666) //nolint:gosec
if err != nil {
return err
}

View File

@@ -8,15 +8,16 @@ import (
"runtime"
"time"
"github.com/spf13/cobra"
"github.com/safing/portbase/container"
"github.com/safing/portbase/database/record"
"github.com/safing/portbase/formats/dsd"
"github.com/safing/portbase/info"
"github.com/spf13/cobra"
)
func initializeLogFile(logFilePath string, identifier string, version string) *os.File {
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0444)
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0o0444)
if err != nil {
log.Printf("failed to create log file %s: %s\n", logFilePath, err)
return nil
@@ -107,7 +108,9 @@ func logControlError(cErr error) {
if errorFile == nil {
return
}
defer errorFile.Close()
defer func() {
_ = errorFile.Close()
}()
fmt.Fprintln(errorFile, cErr.Error())
}

View File

@@ -11,15 +11,14 @@ import (
"strings"
"syscall"
"github.com/safing/portmaster/updates/helper"
"github.com/spf13/cobra"
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/info"
portlog "github.com/safing/portbase/log"
"github.com/safing/portbase/updater"
"github.com/safing/portbase/utils"
"github.com/spf13/cobra"
"github.com/safing/portmaster/updates/helper"
)
var (
@@ -29,7 +28,7 @@ var (
dataRoot *utils.DirStructure
logsRoot *utils.DirStructure
// create registry
// Create registry.
registry = &updater.ResourceRegistry{
Name: "updates",
UpdateURLs: []string{
@@ -153,14 +152,14 @@ func configureRegistry(mustLoadIndex bool) error {
// Remove left over quotes.
dataDir = strings.Trim(dataDir, `\"`)
// Initialize data root.
err := dataroot.Initialize(dataDir, 0755)
err := dataroot.Initialize(dataDir, 0o0755)
if err != nil {
return fmt.Errorf("failed to initialize data root: %s", err)
return fmt.Errorf("failed to initialize data root: %w", err)
}
dataRoot = dataroot.Root()
// Initialize registry.
err = registry.Initialize(dataRoot.ChildDir("updates", 0755))
err = registry.Initialize(dataRoot.ChildDir("updates", 0o0755))
if err != nil {
return err
}
@@ -170,10 +169,10 @@ func configureRegistry(mustLoadIndex bool) error {
func ensureLoggingDir() error {
// set up logs root
logsRoot = dataRoot.ChildDir("logs", 0777)
logsRoot = dataRoot.ChildDir("logs", 0o0777)
err := logsRoot.Ensure()
if err != nil {
return fmt.Errorf("failed to initialize logs root (%q): %s", logsRoot.Path, err)
return fmt.Errorf("failed to initialize logs root (%q): %w", logsRoot.Path, err)
}
// warn about CTRL-C on windows

View File

@@ -1,13 +1,15 @@
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/safing/portmaster/firewall/interception"
"github.com/spf13/cobra"
"github.com/safing/portmaster/firewall/interception"
)
var recoverIPTablesCmd = &cobra.Command{
@@ -19,8 +21,10 @@ var recoverIPTablesCmd = &cobra.Command{
// we don't get the errno of the actual error and need to parse the
// output instead. Make sure it's always english by setting LC_ALL=C
currentLocale := os.Getenv("LC_ALL")
os.Setenv("LC_ALL", "C")
defer os.Setenv("LC_ALL", currentLocale)
_ = os.Setenv("LC_ALL", "C")
defer func() {
_ = os.Setenv("LC_ALL", currentLocale)
}()
err := interception.DeactivateNfqueueFirewall()
if err == nil {
@@ -29,8 +33,8 @@ var recoverIPTablesCmd = &cobra.Command{
// we don't want to show ErrNotExists to the user
// as that only means portmaster did the cleanup itself.
mr, ok := err.(*multierror.Error)
if !ok {
var mr *multierror.Error
if !errors.As(err, &mr) {
return err
}

View File

@@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"io"
"log"
@@ -12,9 +13,10 @@ import (
"strings"
"time"
"github.com/safing/portmaster/updates/helper"
"github.com/spf13/cobra"
"github.com/tevino/abool"
"github.com/safing/portmaster/updates/helper"
)
const (
@@ -223,11 +225,11 @@ func fixExecPerm(path string) error {
return fmt.Errorf("failed to stat %s: %w", path, err)
}
if info.Mode() == 0755 {
if info.Mode() == 0o0755 {
return nil
}
if err := os.Chmod(path, 0755); err != nil {
if err := os.Chmod(path, 0o0755); err != nil { //nolint:gosec // Set execution rights.
return fmt.Errorf("failed to chmod %s: %w", path, err)
}
@@ -367,7 +369,7 @@ func execute(opts *Options, args []string) (cont bool, err error) {
case <-time.After(3 * time.Minute): // portmaster core prints stack if not able to shutdown in 3 minutes, give it one more ...
err = exc.Process.Kill()
if err != nil {
return false, fmt.Errorf("failed to kill %s: %s", opts.Identifier, err)
return false, fmt.Errorf("failed to kill %s: %w", opts.Identifier, err)
}
return false, fmt.Errorf("killed %s", opts.Identifier)
}
@@ -402,7 +404,8 @@ func parseExitError(err error) (restart bool, errWithCtx error) {
return false, nil
}
if exErr, ok := err.(*exec.ExitError); ok {
var exErr *exec.ExitError
if errors.As(err, &exErr) {
switch exErr.ProcessState.ExitCode() {
case 0:
return false, fmt.Errorf("clean exit with error: %w", err)

View File

@@ -4,8 +4,9 @@ import (
"fmt"
"strings"
"github.com/safing/portmaster/updates/helper"
"github.com/spf13/cobra"
"github.com/safing/portmaster/updates/helper"
)
func init() {
@@ -35,7 +36,7 @@ func show(opts *Options, cmdArgs []string) error {
helper.PlatformIdentifier(opts.Identifier),
)
if err != nil {
return fmt.Errorf("could not get component: %s", err)
return fmt.Errorf("could not get component: %w", err)
}
fmt.Printf("%s %s\n", file.Path(), strings.Join(args, " "))

View File

@@ -5,10 +5,16 @@ import (
)
var (
startupComplete = make(chan struct{}) // signal that the start procedure completed (is never closed, just signaled once)
shuttingDown = make(chan struct{}) // signal that we are shutting down (will be closed, may not be closed directly, use initiateShutdown)
//nolint:unused // false positive on linux, currently used by windows only
shutdownError error // protected by shutdownLock
// startupComplete signals that the start procedure completed.
// The channel is not closed, just signaled once.
startupComplete = make(chan struct{})
// shuttingDown signals that we are shutting down.
// The channel will be closed, but may not be closed directly - only via initiateShutdown.
shuttingDown = make(chan struct{})
// shutdownError is protected by shutdownLock.
shutdownError error //nolint:unused,errname // Not what the linter thinks it is. Currently used on windows only.
shutdownLock sync.Mutex
)

View File

@@ -5,9 +5,10 @@ import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/updates/helper"
"github.com/spf13/cobra"
)
var reset bool

View File

@@ -8,8 +8,9 @@ import (
"strings"
"text/tabwriter"
"github.com/safing/portbase/info"
"github.com/spf13/cobra"
"github.com/safing/portbase/info"
)
var (
@@ -64,9 +65,7 @@ var (
fmt.Fprintf(tw, " %s\t%s\n", identifier, res.SelectedVersion.VersionNumber)
}
tw.Flush()
return nil
return tw.Flush()
},
}
)

View File

@@ -12,9 +12,7 @@ import (
"github.com/safing/portbase/log"
)
const (
dnsResolver = "1.1.1.1:53"
)
const dnsResolver = "1.1.1.1:53"
var (
url string
@@ -72,7 +70,9 @@ func makeHTTPRequest(i int) {
log.Errorf("http request #%d failed after %s: %s", i, time.Since(start).Round(time.Millisecond), err)
return
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
log.Infof("http response #%d after %s: %d", i, time.Since(start).Round(time.Millisecond), resp.StatusCode)
}

View File

@@ -5,9 +5,10 @@ import (
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/safing/portbase/updater"
"github.com/safing/portbase/utils"
"github.com/spf13/cobra"
)
var (
@@ -30,7 +31,7 @@ var rootCmd = &cobra.Command{
}
registry = &updater.ResourceRegistry{}
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0755))
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0o0755))
if err != nil {
return err
}

View File

@@ -8,9 +8,9 @@ import (
"os"
"path/filepath"
"github.com/safing/portbase/updater"
"github.com/spf13/cobra"
"github.com/safing/portbase/updater"
)
var (
@@ -44,13 +44,13 @@ func release(cmd *cobra.Command, args []string) error {
// Check if we want to reset instead.
if resetPreReleases {
return removeFilesFromIndex(getChannelVersions(channel, preReleaseFrom, true))
return removeFilesFromIndex(getChannelVersions(preReleaseFrom, true))
}
// Write new index.
err := writeIndex(
channel,
getChannelVersions(channel, preReleaseFrom, false),
getChannelVersions(preReleaseFrom, false),
)
if err != nil {
return err
@@ -95,7 +95,7 @@ func writeIndex(channel string, versions map[string]string) error {
}
// Write new index to disk.
err = ioutil.WriteFile(indexFilePath, versionData, 0644) //nolint:gosec // 0644 is intended
err = ioutil.WriteFile(indexFilePath, versionData, 0o0644) //nolint:gosec // 0644 is intended
if err != nil {
return err
}
@@ -129,7 +129,7 @@ func removeFilesFromIndex(versions map[string]string) error {
return nil
}
func getChannelVersions(channel string, prereleaseFrom string, storagePath bool) map[string]string {
func getChannelVersions(prereleaseFrom string, storagePath bool) map[string]string {
if prereleaseFrom != "" {
registry.AddIndex(updater.Index{
Path: prereleaseFrom + ".json",