2020-11-17 12:48:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-11-27 02:23:59 -05:00
|
|
|
func parseDate(layout,datetime string) time.Time {
|
2020-11-17 12:48:26 -05:00
|
|
|
t, err := time.Parse(layout, datetime)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-11-27 02:23:59 -05:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDateToString(layout,datetime,format string) string {
|
|
|
|
return parseDate(layout, datetime).Format(format)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDateToUnix(layout,datetime string) int64 {
|
|
|
|
return parseDate(layout, datetime).Unix()
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func runBash(script string) string {
|
|
|
|
fmt.Println(script)
|
|
|
|
cmd := exec.Command("bash", "-c", script)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return (fmt.Sprint(err) + " : " + string(stdout))
|
|
|
|
}
|
|
|
|
return string(stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fExist(path string) bool {
|
|
|
|
var _, err = os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
} else if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fRead(path string) string {
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fCreate(path string) bool {
|
|
|
|
var _, err = os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
var file, err = os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fWrite(path, content string) {
|
|
|
|
err := ioutil.WriteFile(path, []byte(content), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fDelete(path string) {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fDownload(path, url string, basicAuth bool) error {
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if basicAuth {
|
|
|
|
req.SetBasicAuth(*masterBasicAuthUser, *masterBasicAuthPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("WARNING: Download file operation for url %s finished with status code %d", url, resp.StatusCode )
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fCreate(path)
|
|
|
|
fWrite(path, string(body))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|