-
Notifications
You must be signed in to change notification settings - Fork 3
/
dotfileclient.go
251 lines (202 loc) · 5.96 KB
/
dotfileclient.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package dotfileclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"time"
"github.com/knoebber/dotfile/dotfile"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
const timeoutSeconds = 30
// Revision is the bytes of a revision and its hash.
// Note that the bytes would not hash to Hash - its from the original uncompressed content.
type Revision struct {
Hash string
Bytes []byte
}
// Client contains a http client and the information needed for interacting with the dotfilehub api.
type Client struct {
Client *http.Client
Remote string
Username string
Token string
}
// New returns a client that is ready to communicate with a dotfilehub server.
func New(remote, username, token string) *Client {
return &Client{
Client: &http.Client{
Timeout: time.Second * timeoutSeconds,
},
Remote: remote,
Username: username,
Token: token,
}
}
func (c *Client) userURL() string {
return c.Remote + "/api/v1/user/" + c.Username
}
func (c *Client) fileURL(alias string) string {
return c.userURL() + "/" + alias
}
func (c *Client) rawFileURL(alias string) string {
return c.fileURL(alias) + "/raw"
}
func (c *Client) revisionURL(alias, hash string) string {
return c.fileURL(alias) + "/" + hash
}
// List lists the files that the remote user has saved.
func (c *Client) List(path bool) ([]string, error) {
var result []string
url := c.userURL()
if path {
url += "?path=true"
}
resp, err := c.Client.Get(url)
if err != nil {
return nil, errors.Wrapf(err, "getting file list from %q for %q", c.Remote, c.Username)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("getting file list: %v", resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, errors.Wrap(err, "decoding file list")
}
return result, nil
}
// TrackingDataBytes returns the tracking data for alias in bytes.
func (c *Client) TrackingDataBytes(alias string) ([]byte, error) {
resp, err := c.Client.Get(c.fileURL(alias))
if err != nil {
return nil, errors.Wrapf(err, "getting remote tracked file from %q for %q %q", c.Remote, c.Username, alias)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, nil
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetching remote tracked file %q: %s", alias, resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
// TrackingData returns the file tracking data for alias on remote.
func (c *Client) TrackingData(alias string) (*dotfile.TrackingData, error) {
data, err := c.TrackingDataBytes(alias)
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
result := new(dotfile.TrackingData)
if err := json.Unmarshal(data, &result); err != nil {
return nil, errors.Wrap(err, "unmarshalling remote tracked file")
}
return result, nil
}
func (c *Client) revision(alias, hash string) ([]byte, error) {
url := c.revisionURL(alias, hash)
resp, err := c.Client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fmt.Println("GET", url)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetching file content: %s", resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
// Content fetches the current content of alias.
func (c *Client) Content(alias string) ([]byte, error) {
resp, err := c.Client.Get(c.rawFileURL(alias))
if err != nil {
return nil, errors.Wrapf(err, "getting raw content from %q for %q %q", c.Remote, c.Username, alias)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetching file content: %s", resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
// Revisions fetches all of the revisions for alias in the hashes argument.
// Returns an error if any fetches fail or are non 200.
func (c *Client) Revisions(alias string, hashes []string) ([]*Revision, error) {
g := new(errgroup.Group)
results := make([]*Revision, len(hashes))
for i, hash := range hashes {
i, hash := i, hash // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
r, err := c.revision(alias, hash)
if err != nil {
return err
}
results[i] = &Revision{
Hash: hash,
Bytes: r,
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}
// UploadRevisions uploads revisions to remote using a multipart POST request.
// The first part is the fileData JSON the rest are form files with the revision bytes.
func (c *Client) UploadRevisions(alias string, data *dotfile.TrackingData, revisions []*Revision) error {
var body bytes.Buffer
if len(revisions) == 0 {
fmt.Println("Upto date.")
return nil
}
url := c.fileURL(alias)
writer := multipart.NewWriter(&body)
jsonPart, err := writer.CreatePart(textproto.MIMEHeader{
"Content-Type": {"application/json"},
})
if err != nil {
return errors.Wrap(err, "creating JSON part")
}
if err = json.NewEncoder(jsonPart).Encode(data); err != nil {
return errors.Wrap(err, "encoding json part: %v")
}
for _, r := range revisions {
revisionPart, err := writer.CreateFormFile("revision", r.Hash)
if err != nil {
return errors.Wrap(err, "creating revision part")
}
n, err := revisionPart.Write(r.Bytes)
if err != nil {
return err
}
fmt.Printf("pushing %s (%d bytes)\n", r.Hash, n)
}
contentType := fmt.Sprintf("multipart/mixed; boundary=%s", writer.Boundary())
if err = writer.Close(); err != nil {
return err
}
req, err := http.NewRequest("POST", url, &body)
if err != nil {
return err
}
req.Header.Set("Content-Type", contentType)
req.SetBasicAuth(c.Username, c.Token)
resp, err := c.Client.Do(req)
if err != nil {
return errors.Wrapf(err, "uploading revisions to %q for %q %q", c.Remote, c.Username, alias)
}
if err := resp.Body.Close(); err != nil {
return fmt.Errorf("closing response body after push: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("uploading file revisions: %s", resp.Status)
}
return nil
}