2020-01-19 02:11:53 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-01-24 17:02:50 +00:00
|
|
|
"bufio"
|
2020-01-21 23:06:35 +00:00
|
|
|
"io/ioutil"
|
2020-01-19 02:11:53 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-01-24 17:02:50 +00:00
|
|
|
type delegateString func(string)
|
|
|
|
|
2020-01-19 02:11:53 +00: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-21 23:06:35 +00: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 17:02:50 +00:00
|
|
|
|
|
|
|
// ReadFileLineByLine - Reads file line by line and calls delegate
|
|
|
|
func ReadFileLineByLine(path string, delegate delegateString) {
|
|
|
|
file, err := os.Open(path)
|
2020-02-07 23:51:17 +00:00
|
|
|
CheckErr(err)
|
2020-01-24 17:02:50 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
delegate(scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|