forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
105 lines (90 loc) · 2.33 KB
/
command.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
package helpers
import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
const (
DebugCommandPrefix = "\nCMD>"
DebugOutPrefix = "OUT: "
DebugErrPrefix = "ERR: "
)
func CF(args ...string) *Session {
WriteCommand(nil, args)
session, err := Start(
exec.Command("cf", args...),
NewPrefixedWriter(DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(DebugErrPrefix, GinkgoWriter))
Expect(err).NotTo(HaveOccurred())
return session
}
type CFEnv struct {
WorkingDirectory string
EnvVars map[string]string
stdin io.Reader
}
func CustomCF(cfEnv CFEnv, args ...string) *Session {
command := exec.Command("cf", args...)
if cfEnv.stdin != nil {
command.Stdin = cfEnv.stdin
}
if cfEnv.WorkingDirectory != "" {
command.Dir = cfEnv.WorkingDirectory
}
if cfEnv.EnvVars != nil {
env := os.Environ()
for key, val := range cfEnv.EnvVars {
env = AddOrReplaceEnvironment(env, key, val)
}
command.Env = env
}
WriteCommand(cfEnv.EnvVars, args)
session, err := Start(
command,
NewPrefixedWriter(DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(DebugErrPrefix, GinkgoWriter))
Expect(err).NotTo(HaveOccurred())
return session
}
func DebugCustomCF(cfEnv CFEnv, args ...string) *Session {
if cfEnv.EnvVars == nil {
cfEnv.EnvVars = map[string]string{}
}
cfEnv.EnvVars["CF_LOG_LEVEL"] = "debug"
return CustomCF(cfEnv, args...)
}
func CFWithStdin(stdin io.Reader, args ...string) *Session {
WriteCommand(nil, args)
command := exec.Command("cf", args...)
command.Stdin = stdin
session, err := Start(
command,
NewPrefixedWriter(DebugOutPrefix, GinkgoWriter),
NewPrefixedWriter(DebugErrPrefix, GinkgoWriter))
Expect(err).NotTo(HaveOccurred())
return session
}
func CFWithEnv(envVars map[string]string, args ...string) *Session {
return CustomCF(CFEnv{EnvVars: envVars}, args...)
}
func WriteCommand(env map[string]string, args []string) {
display := []string{
DebugCommandPrefix,
}
isPass := regexp.MustCompile("(?i)password|token")
for key, val := range env {
if isPass.MatchString(key) {
val = "*****"
}
display = append(display, fmt.Sprintf("%s=%s", key, val))
}
display = append(display, "cf")
display = append(display, args...)
GinkgoWriter.Write([]byte(strings.Join(append(display, "\n"), " ")))
}