-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathsnap_op.go
260 lines (220 loc) · 7.04 KB
/
snap_op.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
252
253
254
255
256
257
258
259
260
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
)
type SnapOptions struct {
Channel string `json:"channel,omitempty"`
Revision string `json:"revision,omitempty"`
DevMode bool `json:"devmode,omitempty"`
JailMode bool `json:"jailmode,omitempty"`
Classic bool `json:"classic,omitempty"`
Dangerous bool `json:"dangerous,omitempty"`
IgnoreValidation bool `json:"ignore-validation,omitempty"`
Unaliased bool `json:"unaliased,omitempty"`
}
func (opts *SnapOptions) writeModeFields(mw *multipart.Writer) error {
fields := []struct {
f string
b bool
}{
{"devmode", opts.DevMode},
{"classic", opts.Classic},
{"jailmode", opts.JailMode},
{"dangerous", opts.Dangerous},
}
for _, o := range fields {
if !o.b {
continue
}
err := mw.WriteField(o.f, "true")
if err != nil {
return err
}
}
return nil
}
type actionData struct {
Action string `json:"action"`
Name string `json:"name,omitempty"`
SnapPath string `json:"snap-path,omitempty"`
*SnapOptions
}
type multiActionData struct {
Action string `json:"action"`
Snaps []string `json:"snaps,omitempty"`
}
// Install adds the snap with the given name from the given channel (or
// the system default channel if not).
func (client *Client) Install(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("install", name, options)
}
func (client *Client) InstallMany(names []string, options *SnapOptions) (changeID string, err error) {
return client.doMultiSnapAction("install", names, options)
}
// Remove removes the snap with the given name.
func (client *Client) Remove(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("remove", name, options)
}
func (client *Client) RemoveMany(names []string, options *SnapOptions) (changeID string, err error) {
return client.doMultiSnapAction("remove", names, options)
}
// Refresh refreshes the snap with the given name (switching it to track
// the given channel if given).
func (client *Client) Refresh(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("refresh", name, options)
}
func (client *Client) RefreshMany(names []string, options *SnapOptions) (changeID string, err error) {
return client.doMultiSnapAction("refresh", names, options)
}
func (client *Client) Enable(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("enable", name, options)
}
func (client *Client) Disable(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("disable", name, options)
}
// Revert rolls the snap back to the previous on-disk state
func (client *Client) Revert(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("revert", name, options)
}
// Switch moves the snap to a different channel without a refresh
func (client *Client) Switch(name string, options *SnapOptions) (changeID string, err error) {
return client.doSnapAction("switch", name, options)
}
var ErrDangerousNotApplicable = fmt.Errorf("dangerous option only meaningful when installing from a local file")
func (client *Client) doSnapAction(actionName string, snapName string, options *SnapOptions) (changeID string, err error) {
if options != nil && options.Dangerous {
return "", ErrDangerousNotApplicable
}
action := actionData{
Action: actionName,
SnapOptions: options,
}
data, err := json.Marshal(&action)
if err != nil {
return "", fmt.Errorf("cannot marshal snap action: %s", err)
}
path := fmt.Sprintf("/v2/snaps/%s", snapName)
headers := map[string]string{
"Content-Type": "application/json",
}
return client.doAsync("POST", path, nil, headers, bytes.NewBuffer(data))
}
func (client *Client) doMultiSnapAction(actionName string, snaps []string, options *SnapOptions) (changeID string, err error) {
if options != nil {
return "", fmt.Errorf("cannot use options for multi-action") // (yet)
}
action := multiActionData{
Action: actionName,
Snaps: snaps,
}
data, err := json.Marshal(&action)
if err != nil {
return "", fmt.Errorf("cannot marshal multi-snap action: %s", err)
}
headers := map[string]string{
"Content-Type": "application/json",
}
return client.doAsync("POST", "/v2/snaps", nil, headers, bytes.NewBuffer(data))
}
// InstallPath sideloads the snap with the given path, returning the UUID
// of the background operation upon success.
func (client *Client) InstallPath(path string, options *SnapOptions) (changeID string, err error) {
f, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("cannot open: %q", path)
}
action := actionData{
Action: "install",
SnapPath: path,
SnapOptions: options,
}
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
go sendSnapFile(path, f, pw, mw, &action)
headers := map[string]string{
"Content-Type": mw.FormDataContentType(),
}
return client.doAsync("POST", "/v2/snaps", nil, headers, pr)
}
// Try
func (client *Client) Try(path string, options *SnapOptions) (changeID string, err error) {
if options == nil {
options = &SnapOptions{}
}
if options.Dangerous {
return "", ErrDangerousNotApplicable
}
buf := bytes.NewBuffer(nil)
mw := multipart.NewWriter(buf)
mw.WriteField("action", "try")
mw.WriteField("snap-path", path)
options.writeModeFields(mw)
mw.Close()
headers := map[string]string{
"Content-Type": mw.FormDataContentType(),
}
return client.doAsync("POST", "/v2/snaps", nil, headers, buf)
}
func sendSnapFile(snapPath string, snapFile *os.File, pw *io.PipeWriter, mw *multipart.Writer, action *actionData) {
defer snapFile.Close()
if action.SnapOptions == nil {
action.SnapOptions = &SnapOptions{}
}
fields := []struct {
name string
value string
}{
{"action", action.Action},
{"name", action.Name},
{"snap-path", action.SnapPath},
{"channel", action.Channel},
}
for _, s := range fields {
if s.value == "" {
continue
}
if err := mw.WriteField(s.name, s.value); err != nil {
pw.CloseWithError(err)
return
}
}
if err := action.writeModeFields(mw); err != nil {
pw.CloseWithError(err)
return
}
fw, err := mw.CreateFormFile("snap", filepath.Base(snapPath))
if err != nil {
pw.CloseWithError(err)
return
}
_, err = io.Copy(fw, snapFile)
if err != nil {
pw.CloseWithError(err)
return
}
mw.Close()
pw.Close()
}