-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelper.go
89 lines (72 loc) · 1.6 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package sync
import (
"os"
"io/ioutil"
"log"
"strings"
"github.com/webdevops/go-shell"
"fmt"
"runtime/debug"
)
func CreateTempfile() *os.File {
tmpfile, err := ioutil.TempFile("", "gsync")
if err != nil {
log.Fatal(err)
}
return tmpfile
}
func CreateTempfileWithContent(content ...string) *os.File {
tmpfile := CreateTempfile()
if _, err := tmpfile.Write([]byte(strings.Join(content[:],"\n"))); err != nil {
log.Fatal(err)
}
return tmpfile
}
func PathExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func FileExists(name string) bool {
f, err := os.Stat(name);
if err != nil {
if os.IsNotExist(err) {
return false
}
}
if f.IsDir() {
return false
}
return true
}
func RsyncPath(name string) string {
return strings.TrimRight(name, "/") + "/"
}
func ShellErrorHandler(recover interface{}) {
if process, ok := recover.(*shell.Process); ok {
p := process.ExitStatus
p = 2
if p != 0 {
printMessage := func(header string, body string) {
fmt.Println(header)
fmt.Println(strings.Repeat("-", len(header)))
fmt.Println(" " + strings.Replace(body, "\n", "\n ", -1))
fmt.Println()
}
fmt.Println("\n\n[!!!] Command execution failed")
fmt.Println()
printMessage("Command", process.Command.ToString())
printMessage("Stdout", process.Stdout.String())
printMessage("Stderr", process.Stderr.String())
printMessage("Exit code", fmt.Sprintf("%d", p))
os.Exit(2)
}
} else if recover != nil {
fmt.Print("ERROR:")
fmt.Println(recover)
debug.PrintStack()
}
}