D.R.Y: check for permissions error in common.ErrorCheck
Reduce cognitive complexity.
This commit is contained in:
parent
2cec7b2e05
commit
0170b7cd90
3 changed files with 32 additions and 62 deletions
|
@ -1,6 +1,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -19,13 +20,19 @@ const PermissionNotice = `
|
||||||
<us>Try running QuickPassthrough as root or using sudo if so.</>
|
<us>Try running QuickPassthrough as root or using sudo if so.</>
|
||||||
|
|
||||||
If this does not work, double check your filesystem's permissions,
|
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
|
// ErrorCheck serves as a wrapper for HikariKnight/ls-iommu/pkg/common.ErrorCheck that allows for visibile error messages
|
||||||
func ErrorCheck(err error, msg ...string) {
|
func ErrorCheck(err error, msg ...string) {
|
||||||
_, _ = os.Stdout.WriteString("\033[H\033[2J") // clear the screen
|
_, _ = os.Stdout.WriteString("\033[H\033[2J") // clear the screen
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if errors.Is(err, os.ErrPermission) {
|
||||||
|
color.Printf(PermissionNotice)
|
||||||
|
}
|
||||||
oneMsg := ""
|
oneMsg := ""
|
||||||
if err != nil {
|
|
||||||
if len(msg) < 1 {
|
if len(msg) < 1 {
|
||||||
oneMsg = ""
|
oneMsg = ""
|
||||||
} else {
|
} else {
|
||||||
|
@ -41,4 +48,3 @@ func ErrorCheck(err error, msg ...string) {
|
||||||
print("\n")
|
print("\n")
|
||||||
errorcheck.ErrorCheck(err, msg...)
|
errorcheck.ErrorCheck(err, msg...)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -81,20 +81,13 @@ func InitConfigs() {
|
||||||
|
|
||||||
// Remove old config
|
// Remove old config
|
||||||
if err := os.RemoveAll("config"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
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
|
// won't be called if the error is ErrNotExist
|
||||||
common.ErrorCheck(err, "\nError removing old config")
|
common.ErrorCheck(err, "\nError removing old config")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the config folder
|
// Make the config folder
|
||||||
if err := os.Mkdir("config", os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
|
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
|
// won't be called if the error is ErrExist
|
||||||
common.ErrorCheck(err, "\nError making config folder")
|
common.ErrorCheck(err, "\nError making config folder")
|
||||||
}
|
}
|
||||||
|
@ -111,12 +104,8 @@ func InitConfigs() {
|
||||||
|
|
||||||
// If we received an error that is not ErrNotExist
|
// If we received an error that is not ErrNotExist
|
||||||
if err != nil {
|
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)
|
common.ErrorCheck(err, "\nError checking for directory: "+syspath)
|
||||||
continue // note: also unreachable
|
continue // note: unreachable due to ErrorCheck calling fatal
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the path exists
|
// If the path exists
|
||||||
|
@ -134,11 +123,8 @@ func InitConfigs() {
|
||||||
|
|
||||||
// Create the directories for our configs
|
// Create the directories for our configs
|
||||||
if err = os.MkdirAll(confpath, os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
|
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)
|
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 we received an error that is not ErrNotExist
|
||||||
if err != nil {
|
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)
|
common.ErrorCheck(err, "\nError checking for file: "+sysfile)
|
||||||
continue // note: also unreachable
|
continue // note: unreachable due to ErrorCheck calling fatal
|
||||||
}
|
}
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
|
@ -195,12 +177,8 @@ func InitConfigs() {
|
||||||
|
|
||||||
exists, err = fileio.FileExist(conffile)
|
exists, err = fileio.FileExist(conffile)
|
||||||
if err != nil {
|
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)
|
common.ErrorCheck(err, "\nError checking for file: "+conffile)
|
||||||
continue // note: also unreachable
|
continue // note: unreachable
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we now have a config that exists
|
// 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
|
// If we received an error that is not ErrNotExist on any of the files
|
||||||
for _, err := range []error{configFileError, sysFileError, destFileError} {
|
for _, err := range []error{configFileError, sysFileError, destFileError} {
|
||||||
if err != nil {
|
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)
|
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/")
|
exists, err := fileio.FileExist("backup/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we received an error that is not ErrNotExist
|
// 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")
|
common.ErrorCheck(err, "Error checking for backup/ folder")
|
||||||
return // note: also unreachable
|
return // note: unreachable
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
|
@ -315,12 +285,10 @@ func makeBackupDir(dest string) {
|
||||||
|
|
||||||
// Make the empty directories
|
// Make the empty directories
|
||||||
if err = os.MkdirAll(fmt.Sprintf("backup/%s", dest), os.ModePerm); errors.Is(err, os.ErrExist) {
|
if err = os.MkdirAll(fmt.Sprintf("backup/%s", dest), os.ModePerm); errors.Is(err, os.ErrExist) {
|
||||||
|
// ignore if the directory already exists
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if errors.Is(err, os.ErrPermission) {
|
// will return without incident if there's no error
|
||||||
common.ErrorCheck(err, common.PermissionNotice)
|
|
||||||
return // note: unreachable due to ErrorCheck calling fatal
|
|
||||||
}
|
|
||||||
common.ErrorCheck(err, "Error making backup/ folder")
|
common.ErrorCheck(err, "Error making backup/ folder")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,10 +52,6 @@ func ReadLines(fileName string) []string {
|
||||||
func ReadFile(fileName string) string {
|
func ReadFile(fileName string) string {
|
||||||
// Read the whole file
|
// Read the whole file
|
||||||
content, err := os.ReadFile(fileName)
|
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))
|
common.ErrorCheck(err, fmt.Sprintf("Failed to ReadFile on %s", fileName))
|
||||||
|
|
||||||
// Return all the lines as one string
|
// Return all the lines as one string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue