tube/utils/utils.go
Heinrich 'Henrik' Langos 9952cc533a improve logging at startup (#51)
This bit me hard in the begining.
While I was trying to figure out why tube I didn't show any files,
it was silently creating directories all over the place. 😆

Reviewed-on: https://git.mills.io/prologic/tube/pulls/51
Co-authored-by: Heinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>
Co-committed-by: Heinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>
2023-01-16 11:29:55 +00:00

80 lines
1.3 KiB
Go

package utils
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"time"
)
// SafeParseInt64 ...
func SafeParseInt64(s string, d int64) int64 {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return d
}
return n
}
func Download(url, filename string) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if err := ioutil.WriteFile(filename, data, 0o644); err != nil {
return err
}
return nil
}
// FileExists ...
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// CmdExists ...
func CmdExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// RunCmd ...
func RunCmd(timeout int, command string, args ...string) error {
var (
ctx context.Context
cancel context.CancelFunc
)
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cmd.CombinedOutput error: %w\n%s", err, out)
}
return nil
}