From 0170b7cd9032dde82078b7131d187734546eddd8 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Tue, 18 Jun 2024 18:38:28 -0700 Subject: [PATCH] D.R.Y: check for permissions error in `common.ErrorCheck` Reduce cognitive complexity. --- internal/common/errors.go | 40 ++++++++++++++++------------- internal/configs/configs.go | 50 +++++++------------------------------ pkg/fileio/fileio.go | 4 --- 3 files changed, 32 insertions(+), 62 deletions(-) diff --git a/internal/common/errors.go b/internal/common/errors.go index 335b915..0c83407 100644 --- a/internal/common/errors.go +++ b/internal/common/errors.go @@ -1,6 +1,7 @@ package common import ( + "errors" "os" "time" @@ -19,26 +20,31 @@ const PermissionNotice = ` Try running QuickPassthrough as root or using sudo if so. If this does not work, double check your filesystem's permissions, - and be sure to check the debug log for more information.` + and be sure to check the debug log for more information. +` // ErrorCheck serves as a wrapper for HikariKnight/ls-iommu/pkg/common.ErrorCheck that allows for visibile error messages func ErrorCheck(err error, msg ...string) { _, _ = os.Stdout.WriteString("\033[H\033[2J") // clear the screen - oneMsg := "" - if err != nil { - if len(msg) < 1 { - oneMsg = "" - } else { - for _, v := range msg { - oneMsg += v + "\n" - } - } - color.Printf("\nFATAL: %s\n%s\nAborting", err.Error(), oneMsg) - for i := 0; i < 10; i++ { - time.Sleep(1 * time.Second) - print(".") - } - print("\n") - errorcheck.ErrorCheck(err, msg...) + if err == nil { + return } + if errors.Is(err, os.ErrPermission) { + color.Printf(PermissionNotice) + } + oneMsg := "" + if len(msg) < 1 { + oneMsg = "" + } else { + for _, v := range msg { + oneMsg += v + "\n" + } + } + color.Printf("\nFATAL: %s\n%s\nAborting", err.Error(), oneMsg) + for i := 0; i < 10; i++ { + time.Sleep(1 * time.Second) + print(".") + } + print("\n") + errorcheck.ErrorCheck(err, msg...) } diff --git a/internal/configs/configs.go b/internal/configs/configs.go index 1977adc..f1bec83 100644 --- a/internal/configs/configs.go +++ b/internal/configs/configs.go @@ -81,20 +81,13 @@ func InitConfigs() { // Remove old config if err := os.RemoveAll("config"); err != nil && !errors.Is(err, os.ErrNotExist) { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } + // won't be called if the error is ErrNotExist common.ErrorCheck(err, "\nError removing old config") } // Make the config folder if err := os.Mkdir("config", os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } // won't be called if the error is ErrExist common.ErrorCheck(err, "\nError making config folder") } @@ -111,12 +104,8 @@ func InitConfigs() { // If we received an error that is not ErrNotExist if err != nil { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, "\nError checking for directory: "+syspath) - continue // note: also unreachable + continue // note: unreachable due to ErrorCheck calling fatal } // If the path exists @@ -134,11 +123,8 @@ func InitConfigs() { // Create the directories for our configs if err = os.MkdirAll(confpath, os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, "\nError making directory: "+confpath) + return // note: unreachable due to ErrorCheck calling fatal } } } @@ -166,12 +152,8 @@ func InitConfigs() { // If we received an error that is not ErrNotExist if err != nil { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, "\nError checking for file: "+sysfile) - continue // note: also unreachable + continue // note: unreachable due to ErrorCheck calling fatal } if exists { @@ -195,12 +177,8 @@ func InitConfigs() { exists, err = fileio.FileExist(conffile) if err != nil { - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, "\nError checking for file: "+conffile) - continue // note: also unreachable + continue // note: unreachable } // If we now have a config that exists @@ -267,12 +245,8 @@ func backupFile(source string) { // If we received an error that is not ErrNotExist on any of the files for _, err := range []error{configFileError, sysFileError, destFileError} { if err != nil { - if errors.Is(configFileError, os.ErrPermission) { - common.ErrorCheck(configFileError, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(configFileError, "\nError checking for file: "+source) - return // note: also unreachable + return // note: unreachable } } @@ -300,12 +274,8 @@ func makeBackupDir(dest string) { exists, err := fileio.FileExist("backup/") if err != nil { // If we received an error that is not ErrNotExist - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, "Error checking for backup/ folder") - return // note: also unreachable + return // note: unreachable } if !exists { @@ -315,12 +285,10 @@ func makeBackupDir(dest string) { // Make the empty directories if err = os.MkdirAll(fmt.Sprintf("backup/%s", dest), os.ModePerm); errors.Is(err, os.ErrExist) { + // ignore if the directory already exists err = nil } - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return // note: unreachable due to ErrorCheck calling fatal - } + // will return without incident if there's no error common.ErrorCheck(err, "Error making backup/ folder") } diff --git a/pkg/fileio/fileio.go b/pkg/fileio/fileio.go index 95a7938..b727ede 100644 --- a/pkg/fileio/fileio.go +++ b/pkg/fileio/fileio.go @@ -52,10 +52,6 @@ func ReadLines(fileName string) []string { func ReadFile(fileName string) string { // Read the whole file content, err := os.ReadFile(fileName) - if errors.Is(err, os.ErrPermission) { - common.ErrorCheck(err, common.PermissionNotice) - return "" // note: unreachable due to ErrorCheck calling fatal - } common.ErrorCheck(err, fmt.Sprintf("Failed to ReadFile on %s", fileName)) // Return all the lines as one string