Files
libmutton/core/utilitiesMisc.go
T

80 lines
2.7 KiB
Go

package core
import (
"os"
"strings"
"github.com/rwinkhart/go-boilerplate/back"
"github.com/rwinkhart/libmutton/crypt"
"github.com/rwinkhart/libmutton/global"
)
// WriteEntry writes entryData to an encrypted file at targetLocation.
func WriteEntry(targetLocation string, entryData []byte) {
encBytes := crypt.EncryptBytes(entryData)
err := os.WriteFile(targetLocation, encBytes, 0600)
if err != nil {
back.PrintError("Failed to write to file: "+err.Error(), back.ErrorWrite, true)
}
}
// ClampTrailingWhitespace strips trailing newlines, carriage returns, and tabs from each line in a note.
// Additionally, it removes single trailing spaces and truncates multiple trailing spaces to two (for Markdown formatting).
func ClampTrailingWhitespace(note []string) {
for i, line := range note {
// remove trailing tabs, carriage returns, and newlines
note[i] = strings.TrimRight(line, "\t\r\n")
// determine the number of trailing spaces
var endSpacesCount int
for j := len(line) - 1; j >= 0; j-- {
if line[j] != ' ' {
break
}
endSpacesCount++
}
// remove single spaces, truncate multiple spaces (leave two for Markdown formatting)
switch endSpacesCount {
case 0:
// do nothing
case 1:
// remove the trailing space
note[i] = strings.TrimRight(line, " ")
default:
// truncate the trailing spaces to two
note[i] = line[:len(line)-endSpacesCount+2]
}
}
}
// EntryAddPrecheck ensures the directory meant to contain a new
// entry exists and that the target entry location is not already used.
// Returns: statusCode (0 = success, 1 = target location already exists, 2 = containing directory is invalid).
func EntryAddPrecheck(targetLocation string) uint8 {
// ensure target location does not already exist
_, isAccessible := back.TargetIsFile(targetLocation, false, 0)
if isAccessible {
back.PrintError("Target location already exists", global.ErrorTargetExists, false)
return 1 // inform interactive clients that the target location already exists
}
// ensure target containing directory exists and is a directory (not a file)
containingDir := targetLocation[:strings.LastIndex(targetLocation, global.PathSeparator)]
isFile, isAccessible := back.TargetIsFile(containingDir, false, 1)
if isFile || !isAccessible {
back.PrintError("\""+containingDir+"\" is not a valid containing directory", back.ErrorTargetWrongType, false)
return 2 // inform interactive clients that the containing directory is invalid
}
return 0
}
// EntryIsNotEmpty iterates through entryData and returns true if any line is not empty.
func EntryIsNotEmpty(entryData []string) bool {
for _, line := range entryData {
if line != "" {
return true
}
}
return false
}