Initial commit

This commit is contained in:
Pijus Kamandulis
2020-01-19 04:11:53 +02:00
commit 69574dcb7e
10 changed files with 313 additions and 0 deletions

31
utils/downloadFile.go Normal file
View File

@@ -0,0 +1,31 @@
package utils
import (
"io"
"net/http"
"os"
)
// DownloadFile - Downloads content from `url` and stores it in `outputPath`
func DownloadFile(outputPath string, url string) {
// Get the data
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(outputPath)
if err != nil {
panic(err)
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}

21
utils/fileio.go Normal file
View File

@@ -0,0 +1,21 @@
package utils
import (
"os"
)
// 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)
}
}

15
utils/readFileAsString.go Normal file
View File

@@ -0,0 +1,15 @@
package utils
import (
"io/ioutil"
"log"
)
// ReadFileAsString - Returns contents of given file
func ReadFileAsString(fileName string) string {
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
return string(content)
}