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

View file

@ -7,29 +7,31 @@ import (
"io"
"os"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/common"
)
/*
* This just implements repetetive tasks I have to do with files
*/
// Creates a file and appends the content to the file (ending newline must be supplied with content string)
// AppendContent creates a file and appends the content to the file.
// (ending newline must be supplied with content string)
func AppendContent(content string, fileName string) {
// Open the file
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening \"%s\" for writing", fileName))
common.ErrorCheck(err, fmt.Sprintf("Error opening \"%s\" for writing", fileName))
defer f.Close()
// Write the content
_, err = f.WriteString(content)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error writing to %s", fileName))
common.ErrorCheck(err, fmt.Sprintf("Error writing to %s", fileName))
}
// Reads the file and returns a stringlist with each line
// ReadLines reads the file and returns a stringlist with each line.
func ReadLines(fileName string) []string {
content, err := os.Open(fileName)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error reading file %s", fileName))
common.ErrorCheck(err, fmt.Sprintf("Error reading file %s", fileName))
defer content.Close()
// Make a list of lines
@ -46,54 +48,55 @@ func ReadLines(fileName string) []string {
}
// Reads a file and returns all the content as a string
// ReadFile reads a file and returns all the content as a string.
func ReadFile(fileName string) string {
// Read the whole file
content, err := os.ReadFile(fileName)
errorcheck.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 string(content)
}
// Checks if a file exists and returns a bool
func FileExist(fileName string) bool {
// FileExist checks if a file exists and returns a bool and any error that isn't os.ErrNotExist.
func FileExist(fileName string) (bool, error) {
var exist bool
// Check if the file exists
if _, err := os.Stat(fileName); !errors.Is(err, os.ErrNotExist) {
// Set the value to true
_, err := os.Stat(fileName)
switch {
case err == nil:
exist = true
} else {
// Set the value to false
case errors.Is(err, os.ErrNotExist):
// Set the value to true
exist = false
err = nil
}
// Return if the file exists
return exist
return exist, err
}
// Copies a FILE from source to dest
// FileCopy copies a FILE from source to dest.
func FileCopy(sourceFile, destFile string) {
// Get the file info
filestat, err := os.Stat(sourceFile)
errorcheck.ErrorCheck(err, "Error getting fileinfo of: %s", sourceFile)
common.ErrorCheck(err, "Error getting fileinfo of: %s", sourceFile)
// If the file is a regular file
if filestat.Mode().IsRegular() {
// Open the source file for reading
source, err := os.Open(sourceFile)
errorcheck.ErrorCheck(err, "Error opening %s for copying", sourceFile)
common.ErrorCheck(err, "Error opening %s for copying", sourceFile)
defer source.Close()
// Create the destination file
dest, err := os.Create(destFile)
errorcheck.ErrorCheck(err, "Error creating %s", destFile)
common.ErrorCheck(err, "Error creating %s", destFile)
defer dest.Close()
// Copy the contents of source to dest using io
_, err = io.Copy(dest, source)
errorcheck.ErrorCheck(err, "Failed to copy \"%s\" to \"%s\"", sourceFile, destFile)
common.ErrorCheck(err, "Failed to copy \"%s\" to \"%s\"", sourceFile, destFile)
}
}