2020-01-19 04:11:53 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-01-24 19:02:50 +02:00
|
|
|
"bufio"
|
2020-01-22 01:06:35 +02:00
|
|
|
"io/ioutil"
|
2020-01-19 04:11:53 +02:00
|
|
|
"os"
|
2020-03-22 02:10:24 +02:00
|
|
|
|
2020-03-22 00:22:08 +02:00
|
|
|
checkErr "../checkErr"
|
2020-01-19 04:11:53 +02:00
|
|
|
)
|
|
|
|
|
2020-01-24 19:02:50 +02:00
|
|
|
type delegateString func(string)
|
|
|
|
|
2020-01-19 04:11:53 +02:00
|
|
|
// CheckIfExists - Checks if file or directory exists
|
|
|
|
func CheckIfExists(path string) bool {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitOutputDirectory - Creates output directory
|
|
|
|
func InitOutputDirectory(path string) {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 01:06:35 +02:00
|
|
|
|
|
|
|
// ReadFileToString - Reads file and returns content
|
|
|
|
func ReadFileToString(path string) string {
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(content)
|
|
|
|
}
|
2020-01-24 19:02:50 +02:00
|
|
|
|
|
|
|
// ReadFileLineByLine - Reads file line by line and calls delegate
|
|
|
|
func ReadFileLineByLine(path string, delegate delegateString) {
|
|
|
|
file, err := os.Open(path)
|
2020-03-22 02:10:24 +02:00
|
|
|
checkErr.CheckErr(err)
|
2020-01-24 19:02:50 +02:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
delegate(scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 02:10:24 +02:00
|
|
|
|
|
|
|
// AppendToFile - Appends line to file
|
|
|
|
func AppendToFile(str string, filePath string) {
|
|
|
|
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
checkErr.CheckErr(err)
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := f.WriteString(str + "\n"); err != nil {
|
|
|
|
checkErr.CheckErr(err)
|
|
|
|
}
|
|
|
|
}
|