+ filesystem; upated validators

This commit is contained in:
Andreas Schulte 2023-10-13 20:38:08 +02:00
parent 68f6576663
commit f5c734e417
Signed by: andreas
GPG Key ID: DCD1B6A247B69DB6
4 changed files with 212 additions and 2 deletions

108
filesystem.go Normal file
View File

@ -0,0 +1,108 @@
package utils
import (
"errors"
"os"
"path/filepath"
)
// AppendLineToFile appends the line l to file
func AppendLineToFile(file string, l string) error {
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(l + "\n"); err != nil {
return err
}
return nil
}
// CreateFolder creates a the folder f
func CreateFolder(f string) error {
err := os.Mkdir(f, 0700)
if err != nil {
return err
}
return nil
}
// GetAllFilesInFolderByExtension returns all files by extension of a given folder recursively
func GetAllFilesInFolderByExtension(pathToFolder string, extension string) []string {
files := []string{}
filepath.Walk(pathToFolder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != extension {
return nil
}
files = append(files, path)
return nil
})
return files
}
// GetPathToParrentFolderOfThisExecutable returns the path to the folder where this executable is located
func GetPathToParrentFolderOfThisExecutable() string {
return filepath.Dir(GetPathToThisExecutable())
}
// GetPathToThisExecutable returns the path where this executable is located
func GetPathToThisExecutable() string {
e, err := os.Executable()
if err != nil {
return "."
}
return e
}
// GetSizeOfFileInBytes returns the size of a file in bytes
func GetSizeOfFileInBytes(p string) (int64, error) {
if !DoesFileExist(p) {
return 0, errors.New("not a file or does not exist")
}
fileInfo, err := os.Stat(p)
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}
// LoadStringFromFile writes a string to a file
func LoadStringFromFile(pathOfFile string) (string, error) {
if !DoesFileExist(pathOfFile) {
return "", errors.New("file " + pathOfFile + "does not exist")
}
fileContent, err := os.ReadFile(pathOfFile)
if err != nil {
return "", errors.New("could not read file " + pathOfFile)
}
return string(fileContent), nil
}
// WriteStringToFile writes a string to a file
func WriteStringToFile(file string, s string) error {
err := os.WriteFile(file, []byte(s+"\n"), 0660)
if err != nil {
return err
}
return nil
}

21
filesystem_test.go Normal file
View File

@ -0,0 +1,21 @@
package utils
import (
"testing"
)
func TestGetSizeOfFileInBytes(t *testing.T) {
data := map[string]int64{
"/usr/bin/go": 11466992, // ls -lH /usr/bin/go | awk -F ' ' '{print $5}'
"/etc/fstab": 1049, // ls -lH /etc/fstab | awk -F ' ' '{print $5}'
"/etc/os-release": 371, // ls -lH /etc/os-release | awk -F ' ' '{print $5}'
"/etc": 0,
}
for d, expectedResult := range data {
result, _ := GetSizeOfFileInBytes(d)
if result != expectedResult {
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, expectedResult, d)
}
}
}

View File

@ -1,6 +1,49 @@
package utils
import "net/url"
import (
"net/url"
"os"
)
// DoesFileExist returns true, if a file exists (and actually is a file)
func DoesFileExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if s.IsDir() {
return false
}
return true
}
// DoesFolderExist returns true, if a folder exists (and actually is a folder)
func DoesFolderExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if !s.IsDir() {
return false
}
return true
}
// DoesStringContainsNonWhitelistedSubstrings returns false if s contains substrings which are not in whitelist
func DoesStringContainsNonWhitelistedSubstrings(s string, whitelist map[string]string) bool {
for _, char := range s {
_, charIsWhitelisted := whitelist[string(char)]
if !charIsWhitelisted {
return true
}
}
return false
}
// IsStringInSliceOfStrings returns true, if slice contains target
func IsStringInSliceOfStrings(slice []string, target string) bool {

View File

@ -1,6 +1,8 @@
package utils
import "testing"
import (
"testing"
)
func TestIsStringInSliceOfStrings(t *testing.T) {
slice1 := []string{"apple", "banana", "cherry", "date"}
@ -53,3 +55,39 @@ func TestIsValidUrl(t *testing.T) {
}
}
}
func TestDoesFileExist(t *testing.T) {
data := map[string]bool{
"/usr/bin/go": true,
"/etc/fstab": true,
"/etc/os-release": true,
"/usr/bin": false,
"/home/derschmierigetypvomaldi": false,
"/tmp/plainTextPasswords.txt": false,
}
for d, expectedResult := range data {
result := DoesFileExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestDoesFolderExist(t *testing.T) {
data := map[string]bool{
"/home": true,
"/tmp": true,
"/usr/bin": true,
"/home/derschmierigetypvomaldi": false,
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
"/usr/bin/go": false,
}
for d, expectedResult := range data {
result := DoesFolderExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}