-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.go
62 lines (51 loc) · 1.31 KB
/
utils.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
package main
import (
"archive/tar"
"fmt"
"io"
"os"
"time"
"golang.org/x/crypto/ssh"
)
func writeToTar(tarw *tar.Writer, name string, file io.Reader, fi os.FileInfo) {
header := new(tar.Header)
header.Name = name
header.Size = fi.Size()
header.Mode = int64(fi.Mode())
header.ModTime = fi.ModTime()
err := tarw.WriteHeader(header)
if err != nil {
exitf("failed to write tar header for %s: %s", name, err)
}
_, err = io.Copy(tarw, file)
if err != nil {
exitf("failed to write %s into tar file: %s", name, err)
}
}
func writeInfoToTar(tarw *tar.Writer, info string) {
header := new(tar.Header)
header.Name = fmt.Sprintf("src/%s/harp-build.info", cfg.App.ImportPath)
header.Size = int64(len(info))
header.Mode = int64(0644)
header.ModTime = time.Now()
err := tarw.WriteHeader(header)
if err != nil {
exitf("failed to write tar header for %s: %s", header.Name, err)
}
_, err = tarw.Write([]byte(info))
if err != nil {
exitf("failed to write %s into tar file: %s", header.Name, err)
}
}
func runCmd(sshc *ssh.Client, cmd string) (output []byte) {
session, err := sshc.NewSession()
if err != nil {
exitf("failed to create session: %s", err)
}
defer session.Close()
output, err = session.CombinedOutput(cmd)
if err != nil {
exitf("failed to exec %s: %s %s", cmd, string(output), err)
}
return
}