forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploads.go
120 lines (94 loc) · 2.22 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
package executehelpers
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"os/exec"
"github.com/concourse/fly/ui"
"github.com/concourse/go-archive/tgzfs"
"github.com/concourse/go-concourse/concourse"
)
func Upload(client concourse.Client, input Input, excludeIgnored bool) {
path := input.Path
pipe := input.Pipe
var files []string
var err error
if excludeIgnored {
files, err = getGitFiles(path)
if err != nil {
fmt.Fprintln(ui.Stderr, "could not determine ignored files:", err)
return
}
} else {
files = []string{"."}
}
archiveStream, archiveWriter := io.Pipe()
go func() {
archiveWriter.CloseWithError(tgzfs.Compress(archiveWriter, path, files...))
}()
upload, err := http.NewRequest("PUT", pipe.WriteURL, archiveStream)
if err != nil {
panic(err)
}
response, err := client.HTTPClient().Do(upload)
if err != nil {
fmt.Fprintln(ui.Stderr, "upload request failed:", err)
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
fmt.Fprintln(ui.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
}