2021-02-15 20:37:16 +01:00
package utils
import (
"bytes"
2021-02-20 19:09:41 +01:00
"fmt"
2021-02-20 22:43:27 +01:00
"io"
2021-02-15 20:37:16 +01:00
"log"
2021-02-20 22:43:27 +01:00
"os"
2021-02-15 20:37:16 +01:00
"os/exec"
2021-05-30 13:28:42 +02:00
"time"
2021-02-20 19:09:41 +01:00
2021-02-17 12:10:15 +01:00
"github.com/joho/godotenv"
2021-02-15 20:37:16 +01:00
)
2021-02-20 22:43:27 +01:00
// ExecAndStream : Runs a terminal command, but streams progress instead of outputting. Ideal for long lived process that need to be logged.
func ExecAndStream ( command string , args [ ] string ) {
cmd := exec . Command ( command , args ... )
var stdoutBuf , stderrBuf bytes . Buffer
cmd . Stdout = io . MultiWriter ( os . Stdout , & stdoutBuf )
cmd . Stderr = io . MultiWriter ( os . Stderr , & stderrBuf )
cmd . Dir = GetPath ( "wsPath" )
err := cmd . Run ( )
if err != nil {
log . Fatalf ( "cmd.Run() failed with %s\n" , err )
}
outStr , errStr := string ( stdoutBuf . Bytes ( ) ) , string ( stderrBuf . Bytes ( ) )
fmt . Printf ( "\nout:\n%s\nerr:\n%s\n" , outStr , errStr )
}
2021-02-15 20:37:16 +01:00
// Exec : Runs a terminal Command, catches and logs errors, returns the result.
func Exec ( command string , args [ ] string ) string {
cmd := exec . Command ( command , args ... )
var out bytes . Buffer
var stderr bytes . Buffer
cmd . Stdout = & out
cmd . Stderr = & stderr
2021-02-20 19:09:41 +01:00
cmd . Dir = GetPath ( "wsPath" )
2021-02-15 20:37:16 +01:00
err := cmd . Run ( )
if err != nil {
2021-02-17 13:34:53 +01:00
// TODO: Deal with possibility of error in command, allow explicit error handling and return proper formatted stderr
2021-02-21 00:55:59 +01:00
// log.Println(fmt.Sprint(err) + ": " + stderr.String()) // ... Silence...
2021-02-15 20:37:16 +01:00
}
2021-02-17 01:08:10 +01:00
2021-02-21 00:55:59 +01:00
// log.Println("Result: " + out.String()) // ... Silence ...
2021-02-15 20:37:16 +01:00
return out . String ( )
2021-02-17 01:08:10 +01:00
}
// DeleteEmptySlices : Given a string array, delete empty entries.
func DeleteEmptySlices ( s [ ] string ) [ ] string {
var r [ ] string
for _ , str := range s {
if str != "" {
r = append ( r , str )
}
}
return r
2021-02-15 20:37:16 +01:00
}
2021-02-17 12:10:15 +01:00
// GetMySQLDbConnectionDetails : Returns the necessary string as connection info for SQL.db()
func GetMySQLDbConnectionDetails ( ) string {
var apiEnv map [ string ] string
2021-02-17 12:35:22 +01:00
apiEnv , err := godotenv . Read ( GetPath ( "apiEnvFileLocation" ) )
2021-02-17 12:10:15 +01:00
if err != nil {
log . Fatal ( "Error loading .env file" )
}
Dbhost := "127.0.0.1:" + apiEnv [ "HOST_MACHINE_MYSQL_PORT" ]
Dbname := apiEnv [ "MYSQL_DATABASE" ]
Dbuser := apiEnv [ "MYSQL_USER" ]
Dbpass := apiEnv [ "MYSQL_PASSWORD" ]
return Dbuser + ":" + Dbpass + "@tcp(" + Dbhost + ")/" + Dbname
}
2021-02-17 12:35:22 +01:00
2021-05-02 10:52:32 +02:00
// GetSQLiteDbConnectionDetails : Returns the necessary string as connection info for SQL.db()
func GetSQLiteDbConnectionDetails ( ) string {
var apiEnv map [ string ] string
apiEnv , err := godotenv . Read ( GetPath ( "apiEnvFileLocation" ) )
if err != nil {
log . Fatal ( "Error loading .env file" )
}
return apiEnv [ "SQLITE_DATABASE" ] // Will read from api project edgebox.env file
}
2021-05-30 13:28:42 +02:00
// GetSQLiteFormattedDateTime: Given a Time, Returns a string that is formatted ready to be inserted into an SQLite Datetime field using sql.Prepare.
func GetSQLiteFormattedDateTime ( t time . Time ) string {
// This date is used to indicate the layout.
const datetimeLayout = "2006-01-02 15:04:05"
formatedDatetime := t . Format ( datetimeLayout )
return formatedDatetime
}
2021-02-17 12:35:22 +01:00
// GetPath : Returns either the hardcoded path, or a overwritten value via .env file at project root. Register paths here for seamless working code between dev and prod environments ;)
func GetPath ( pathKey string ) string {
// Read whole of .env file to map.
var env map [ string ] string
env , err := godotenv . Read ( )
targetPath := ""
if err != nil {
2021-02-19 00:36:51 +01:00
// log.Println("Project .env file not found withing project root. Using only hardcoded path variables.")
// Do Nothing...
2021-02-17 12:35:22 +01:00
}
switch pathKey {
case "apiEnvFileLocation" :
if env [ "API_ENV_FILE_LOCATION" ] != "" {
targetPath = env [ "API_ENV_FILE_LOCATION" ]
} else {
targetPath = "/home/system/components/api/edgebox.env"
}
case "edgeAppsPath" :
if env [ "EDGEAPPS_PATH" ] != "" {
targetPath = env [ "EDGEAPPS_PATH" ]
} else {
targetPath = "/home/system/components/apps/"
}
case "wsPath" :
if env [ "WS_PATH" ] != "" {
targetPath = env [ "WS_PATH" ]
} else {
targetPath = "/home/system/components/ws/"
}
default :
log . Printf ( "path_key %s nonexistant in GetPath().\n" , pathKey )
}
return targetPath
}