From f5c734e417e44b059b69e8485e0fa8e200bec078 Mon Sep 17 00:00:00 2001 From: Andreas Schulte Date: Fri, 13 Oct 2023 20:38:08 +0200 Subject: [PATCH] + filesystem; upated validators --- filesystem.go | 108 +++++++++++++++++++++++++++++++++++++++++++++ filesystem_test.go | 21 +++++++++ validators.go | 45 ++++++++++++++++++- validators_test.go | 40 ++++++++++++++++- 4 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 filesystem.go create mode 100644 filesystem_test.go diff --git a/filesystem.go b/filesystem.go new file mode 100644 index 0000000..5286669 --- /dev/null +++ b/filesystem.go @@ -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 +} diff --git a/filesystem_test.go b/filesystem_test.go new file mode 100644 index 0000000..f6859dd --- /dev/null +++ b/filesystem_test.go @@ -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) + } + } +} diff --git a/validators.go b/validators.go index 8557f2c..ec71bce 100644 --- a/validators.go +++ b/validators.go @@ -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 { diff --git a/validators_test.go b/validators_test.go index 516332e..0ec21fd 100644 --- a/validators_test.go +++ b/validators_test.go @@ -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) + } + } +}