-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgcmd.go
60 lines (46 loc) · 1.11 KB
/
gcmd.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
//Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//Licensed under the Universal Permissive License (UPL) Version 1.0 as shown at http://oss.oracle.com/licenses/upl.
package testutil
import (
"bytes"
"os/exec"
)
//Class for exec.Cmd, standard output & error message & execute error
type Cmd struct {
cmd *exec.Cmd
stdout string
stderr string
runError error
}
// Constructor for Cmd class
func Command(name string, arg ...string) *Cmd {
return &Cmd{
cmd: exec.Command(name, arg...),
}
}
var cmdInstance = &Cmd{}
// execute the command.
func (c *Cmd) Run() {
var outBuf bytes.Buffer
c.cmd.Stdout = &outBuf
var errBuf bytes.Buffer
c.cmd.Stderr = &errBuf
if err := c.cmd.Run(); err != nil {
c.runError = err
}
c.stdout = string(outBuf.Bytes())
c.stderr = string(errBuf.Bytes())
}
// Get the Stdout of exec.Cmd
func (c *Cmd) Stdout() string {
return c.stdout
}
// Gget the Stderr of exec.Cmd
func (c *Cmd) Stderr() string {
return c.stderr
}
// Return true if command run successfully
// Otherwise, return false
func (c *Cmd) Success() bool {
return c.runError == nil
}