cutego/internal/utils/utils.go

124 lines
3.1 KiB
Go
Raw Normal View History

2015-10-24 18:18:24 +03:00
package utils
import (
"io/ioutil"
"os"
"os/exec"
2015-11-12 03:29:32 +03:00
"path/filepath"
2016-11-18 17:49:57 +03:00
"strings"
2016-11-18 10:30:09 +03:00
"github.com/sirupsen/logrus"
2015-10-24 18:18:24 +03:00
)
2016-12-11 23:03:06 +03:00
func ExistsFile(name string) bool {
_, err := ioutil.ReadFile(name)
2016-10-28 05:00:58 +03:00
return err == nil
}
2016-12-11 23:03:06 +03:00
func ExistsDir(name string) bool {
_, err := ioutil.ReadDir(name)
2016-12-11 23:03:06 +03:00
return err == nil
}
func MkdirAll(dir string) error {
err := os.MkdirAll(dir, 0755)
2015-10-24 18:18:24 +03:00
if err != nil {
2016-12-11 23:03:06 +03:00
Log.WithError(err).Panicf("failed to create dir %v", dir)
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
return err
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
func RemoveAll(name string) error {
err := os.RemoveAll(name)
2015-10-24 18:18:24 +03:00
if err != nil {
2016-10-28 05:00:58 +03:00
Log.WithError(err).Panicf("failed to remove %v", name)
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
return err
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
func Save(name, data string) error {
err := ioutil.WriteFile(name, []byte(data), 0644)
2017-04-12 06:24:54 +03:00
if err != nil {
Log.WithError(err).Panicf("failed to save %v", name)
} else {
Log.Debugf("saved file len(%v) %v", len(data), name)
}
return err
}
func SaveExec(name, data string) error {
err := ioutil.WriteFile(name, []byte(data), 0755)
2015-10-24 18:18:24 +03:00
if err != nil {
2016-10-28 05:00:58 +03:00
Log.WithError(err).Panicf("failed to save %v", name)
} else {
Log.Debugf("saved file len(%v) %v", len(data), name)
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
return err
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
func SaveBytes(name string, data []byte) error {
err := ioutil.WriteFile(name, data, 0644)
if err != nil {
2016-10-28 05:00:58 +03:00
Log.WithError(err).Panicf("failed to save %v", name)
}
2016-10-28 05:00:58 +03:00
return err
}
2016-10-28 05:00:58 +03:00
//TODO: export error
2015-10-24 18:18:24 +03:00
func Load(name string) string {
out, err := ioutil.ReadFile(name)
2015-10-24 18:18:24 +03:00
if err != nil {
2016-10-28 05:00:58 +03:00
Log.WithError(err).Errorf("failed to load %v", name)
2015-10-24 18:18:24 +03:00
}
2016-10-28 05:00:58 +03:00
return string(out)
2015-10-24 18:18:24 +03:00
}
2015-11-09 20:23:42 +03:00
2017-01-04 02:01:13 +03:00
//TODO: export error
func LoadOptional(name string) string {
out, err := ioutil.ReadFile(name)
2017-01-04 02:01:13 +03:00
if err != nil {
Log.WithError(err).Debugf("failed to load (optional) %v", name)
}
return string(out)
}
func GoQtPkgPath(s ...string) string {
2016-10-23 05:14:01 +03:00
return filepath.Join(MustGoPath(), "src", "github.com", "therecipe", "qt", filepath.Join(s...))
2015-11-09 20:23:42 +03:00
}
//TODO: export error
2016-10-14 20:06:35 +03:00
func RunCmd(cmd *exec.Cmd, name string) string {
fields := logrus.Fields{"_func": "RunCmd", "name": name, "cmd": strings.Join(cmd.Args, " "), "env": strings.Join(cmd.Env, " "), "dir": cmd.Dir}
2016-11-19 03:15:43 +03:00
Log.WithFields(fields).Debug("Execute")
out, err := cmd.CombinedOutput()
2016-10-14 20:06:35 +03:00
if err != nil {
2017-01-02 21:17:38 +03:00
Log.WithError(err).WithFields(fields).Error("failed to run command")
println(string(out))
os.Exit(1)
2016-10-14 20:06:35 +03:00
}
return string(out)
}
//TODO: export error
2016-10-14 20:06:35 +03:00
func RunCmdOptional(cmd *exec.Cmd, name string) string {
fields := logrus.Fields{"_func": "RunCmdOptional", "name": name, "cmd": strings.Join(cmd.Args, " "), "env": strings.Join(cmd.Env, " "), "dir": cmd.Dir}
2016-11-19 03:15:43 +03:00
Log.WithFields(fields).Debug("Execute")
out, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(out), "No template (-t) specified") {
2017-01-02 21:17:38 +03:00
Log.WithError(err).WithFields(fields).Error("failed to run command")
println(string(out))
2016-10-14 20:06:35 +03:00
}
return string(out)
}
func RunCmdOptionalError(cmd *exec.Cmd, name string) (string, error) {
fields := logrus.Fields{"_func": "RunCmdOptionalError", "name": name, "cmd": strings.Join(cmd.Args, " "), "env": strings.Join(cmd.Env, " "), "dir": cmd.Dir}
Log.WithFields(fields).Debug("Execute")
out, err := cmd.CombinedOutput()
if err != nil {
Log.WithError(err).WithFields(fields).Error("failed to run command")
println(string(out))
}
return string(out), err
}