forked from hypersleep/easyssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellutils.go
98 lines (86 loc) · 2.62 KB
/
shellutils.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
package easyssh
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/gaols/goutils"
)
// Local run a cmd on local pc.
// https://studygolang.com/articles/4004 <- run shell command and read output line by line
// https://studygolang.com/articles/7767 <- run command without known args
func Local(localCmd string, paras ...interface{}) (out string, err error) {
localCmd = fmt.Sprintf(localCmd, paras...)
cmd := exec.Command("/bin/bash", "-c", localCmd)
ret, err := cmd.CombinedOutput()
out = string(ret)
return
}
// RtLocalHandle run a cmd on local machine and show command output in real time.
func RtLocalHandle(localCmd string, lineHandler func(line string, lineType int8), paras ...interface{}) error {
localCmd = fmt.Sprintf(localCmd, paras...)
cmd := exec.Command("/bin/bash", "-c", localCmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
go func() {
defer stdout.Close()
defer stderr.Close()
stdoutScanner := bufio.NewScanner(stdout)
stderrScanner := bufio.NewScanner(stderr)
for stdoutScanner.Scan() {
lineHandler(stdoutScanner.Text(), TypeStdout)
}
for stderrScanner.Scan() {
lineHandler(stderrScanner.Text(), TypeStderr)
}
}()
return cmd.Wait()
}
// RtLocal run a cmd on local machine and show command output in real time.
func RtLocal(localCmd string, workspace string, paras ...interface{}) error {
localCmd = fmt.Sprintf(localCmd, paras...)
cmd := exec.Command("/bin/bash", "-c", localCmd)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if workspace != "" {
cmd.Dir = workspace
}
err := cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}
// Tar pack the targetPath and put tarball to tgzPath, targetPath and tgzPath should both the absolute path.
func Tar(tgzPath, targetPath string) error {
if !goutils.IsDir(targetPath) && !goutils.IsRegular(targetPath) {
return errors.New("invalid pack path: " + targetPath)
}
targetPathDir := filepath.Dir(RemoveTrailingSlash(targetPath))
target := filepath.Base(RemoveTrailingSlash(targetPath))
_, err := Local("tar czf %s -C %s %s", tgzPath, targetPathDir, target)
return err
}
// UnTar unpack the tarball specified by tgzPath and extract it to the path specified by targetPath
func UnTar(tgzPath, targetPath string) error {
if !goutils.IsDir(targetPath) {
return errors.New("tar extract path invalid: " + targetPath)
}
if !goutils.IsRegular(tgzPath) {
return errors.New("tar path invalid: " + tgzPath)
}
_, err := Local("tar xf %s -C %s", tgzPath, targetPath)
return err
}