forked from vmware-archive/fly
/
uploads.go
125 lines (99 loc) · 2.28 KB
/
uploads.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
121
122
123
124
125
package executehelpers
import (
"bufio"
"bytes"
"fmt"
"net/http"
"os"
"os/exec"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/deprecated"
"github.com/tedsuo/rata"
)
func Upload(input Input, excludeIgnored bool, atcRequester *deprecated.AtcRequester) {
path := input.Path
pipe := input.Pipe
var files []string
var err error
if excludeIgnored {
files, err = getGitFiles(path)
if err != nil {
fmt.Fprintln(os.Stderr, "could not determine ignored files:", err)
return
}
} else {
files = []string{"."}
}
archive, err := tarStreamFrom(path, files)
if err != nil {
fmt.Fprintln(os.Stderr, "could create tar stream:", err)
return
}
defer archive.Close()
uploadBits, err := atcRequester.CreateRequest(
atc.WritePipe,
rata.Params{"pipe_id": pipe.ID},
archive,
)
if err != nil {
panic(err)
}
response, err := atcRequester.HttpClient.Do(uploadBits)
if err != nil {
fmt.Fprintln(os.Stderr, "upload request failed:", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
fmt.Fprintln(os.Stderr, badResponseError("uploading bits", response))
}
}
func getGitFiles(dir string) ([]string, error) {
tracked, err := gitLS(dir)
if err != nil {
return nil, err
}
untracked, err := gitLS(dir, "--others", "--exclude-standard")
if err != nil {
return nil, err
}
return append(tracked, untracked...), nil
}
func gitLS(dir string, flags ...string) ([]string, error) {
files := []string{}
gitLS := exec.Command("git", append([]string{"ls-files", "-z"}, flags...)...)
gitLS.Dir = dir
gitOut, err := gitLS.StdoutPipe()
if err != nil {
return nil, err
}
outScan := bufio.NewScanner(gitOut)
outScan.Split(scanNull)
err = gitLS.Start()
if err != nil {
return nil, err
}
for outScan.Scan() {
files = append(files, outScan.Text())
}
err = gitLS.Wait()
if err != nil {
return nil, err
}
return files, nil
}
func scanNull(data []byte, atEOF bool) (int, []byte, error) {
// eof, no more data; terminate
if atEOF && len(data) == 0 {
return 0, nil, nil
}
// look for terminating null byte
if i := bytes.IndexByte(data, 0); i >= 0 {
return i + 1, data[0:i], nil
}
// no final terminator; return what's left
if atEOF {
return len(data), data, nil
}
// request more data
return 0, nil, nil
}