forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
120 lines (106 loc) · 2.61 KB
/
util.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package rsync
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
var (
testRsyncCommand = []string{"rsync", "--version"}
testTarCommand = []string{"tar", "--version"}
)
// executeWithLogging will execute a command and log its output
func executeWithLogging(e executor, cmd []string) error {
w := &bytes.Buffer{}
err := e.Execute(cmd, nil, w, w)
glog.V(4).Infof("%s", w.String())
glog.V(4).Infof("error: %v", err)
return err
}
// isWindows returns true if the current platform is windows
func isWindows() bool {
return runtime.GOOS == "windows"
}
// hasLocalRsync returns true if rsync is in current exec path
func hasLocalRsync() bool {
_, err := exec.LookPath("rsync")
if err != nil {
return false
}
return true
}
// siblingCommand returns a sibling command to the current command
func siblingCommand(cmd *cobra.Command, name string) string {
c := cmd.Parent()
command := []string{}
for c != nil {
glog.V(5).Infof("Found parent command: %s", c.Name())
command = append([]string{c.Name()}, command...)
c = c.Parent()
}
// Replace the root command with what was actually used
// in the command line
glog.V(4).Infof("Setting root command to: %s", os.Args[0])
command[0] = os.Args[0]
// Append the sibling command
command = append(command, name)
glog.V(4).Infof("The sibling command is: %s", strings.Join(command, " "))
return strings.Join(command, " ")
}
func isExitError(err error) bool {
if err == nil {
return false
}
_, exitErr := err.(*exec.ExitError)
return exitErr
}
func checkRsync(e executor) error {
return executeWithLogging(e, testRsyncCommand)
}
func checkTar(e executor) error {
return executeWithLogging(e, testTarCommand)
}
func rsyncFlagsFromOptions(o *RsyncOptions) []string {
flags := []string{}
if o.Quiet {
flags = append(flags, "-q")
} else {
flags = append(flags, "-v")
}
if o.Delete {
flags = append(flags, "--delete")
}
if len(o.RsyncInclude) > 0 {
flags = append(flags, fmt.Sprintf("--include=%s", o.RsyncInclude))
}
if len(o.RsyncExclude) > 0 {
flags = append(flags, fmt.Sprintf("--exclude=%s", o.RsyncExclude))
}
if o.RsyncProgress {
flags = append(flags, "--progress")
}
if o.RsyncNoPerms {
flags = append(flags, "--no-perms")
}
return flags
}
func rsyncSpecificFlags(o *RsyncOptions) []string {
flags := []string{}
if len(o.RsyncInclude) > 0 {
flags = append(flags, "--include")
}
if len(o.RsyncExclude) > 0 {
flags = append(flags, "--exclude")
}
if o.RsyncProgress {
flags = append(flags, "--progress")
}
if o.RsyncNoPerms {
flags = append(flags, "--no-perms")
}
return flags
}