Feat: conditional permissions behavior (#28)

* Fix: don't need sudo if we're root + other aesthetics

* Heavy refactoring, see PR #28

* Fix: avoid silent fatalities

demo: https://tcp.ac/i/JMSUc.gif

* Fix: Inverse check on `IsRoot`

* D.R.Y: check for permissions error in `common.ErrorCheck`

Reduce cognitive complexity.

* Fix: Issue with copying

* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646535918

* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646606680 and https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646594105

* Revert "Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646606680 and https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646594105"

This reverts commit ce15213009.

* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646730751
This commit is contained in:
kayos 2024-07-27 08:38:39 -07:00 committed by GitHub
parent 4d0086df41
commit 6c48a35180
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 604 additions and 255 deletions

50
internal/common/errors.go Normal file
View file

@ -0,0 +1,50 @@
package common
import (
"errors"
"os"
"time"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/gookit/color"
)
const PermissionNotice = `
<yellowB>Permissions error occured during file operations.</>
<blue_b>Hint</>:
If you initially ran QuickPassthrough as root or using sudo,
but are now running it as a normal user, this is expected behavior.
<us>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.
`
// 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
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("\n<red_b>FATAL</>: %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...)
}