forked from rclone/rclone
/
rc.go
280 lines (247 loc) · 6.6 KB
/
rc.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package config
import (
"context"
"errors"
"os"
"github.com/artpar/rclone/fs"
"github.com/artpar/rclone/fs/rc"
)
func init() {
rc.Add(rc.Call{
Path: "config/dump",
Fn: rcDump,
Title: "Dumps the config file.",
AuthRequired: true,
Help: `
Returns a JSON object:
- key: value
Where keys are remote names and values are the config parameters.
See the [config dump](/commands/rclone_config_dump/) command for more information on the above.
`,
})
}
// Return the config file dump
func rcDump(ctx context.Context, in rc.Params) (out rc.Params, err error) {
return DumpRcBlob(), nil
}
func init() {
rc.Add(rc.Call{
Path: "config/get",
Fn: rcGet,
Title: "Get a remote in the config file.",
AuthRequired: true,
Help: `
Parameters:
- name - name of remote to get
See the [config dump](/commands/rclone_config_dump/) command for more information on the above.
`,
})
}
// Return the config file get
func rcGet(ctx context.Context, in rc.Params) (out rc.Params, err error) {
name, err := in.GetString("name")
if err != nil {
return nil, err
}
return DumpRcRemote(name), nil
}
func init() {
rc.Add(rc.Call{
Path: "config/listremotes",
Fn: rcListRemotes,
Title: "Lists the remotes in the config file and defined in environment variables.",
AuthRequired: true,
Help: `
Returns
- remotes - array of remote names
See the [listremotes](/commands/rclone_listremotes/) command for more information on the above.
`,
})
}
// Return the a list of remotes in the config file
// including any defined by environment variables.
func rcListRemotes(ctx context.Context, in rc.Params) (out rc.Params, err error) {
remotes := FileSections()
out = rc.Params{
"remotes": remotes,
}
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "config/providers",
Fn: rcProviders,
Title: "Shows how providers are configured in the config file.",
AuthRequired: true,
Help: `
Returns a JSON object:
- providers - array of objects
See the [config providers](/commands/rclone_config_providers/) command for more information on the above.
`,
})
}
// Return the config file providers
func rcProviders(ctx context.Context, in rc.Params) (out rc.Params, err error) {
out = rc.Params{
"providers": fs.Registry,
}
return out, nil
}
func init() {
for _, name := range []string{"create", "update", "password"} {
name := name
extraHelp := ""
if name == "create" {
extraHelp = "- type - type of the new remote\n"
}
if name == "create" || name == "update" {
extraHelp += `- opt - a dictionary of options to control the configuration
- obscure - declare passwords are plain and need obscuring
- noObscure - declare passwords are already obscured and don't need obscuring
- nonInteractive - don't interact with a user, return questions
- continue - continue the config process with an answer
- all - ask all the config questions not just the post config ones
- state - state to restart with - used with continue
- result - result to restart with - used with continue
`
}
rc.Add(rc.Call{
Path: "config/" + name,
AuthRequired: true,
Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
return rcConfig(ctx, in, name)
},
Title: name + " the config for a remote.",
Help: `This takes the following parameters:
- name - name of remote
- parameters - a map of \{ "key": "value" \} pairs
` + extraHelp + `
See the [config ` + name + `](/commands/rclone_config_` + name + `/) command for more information on the above.`,
})
}
}
// Manipulate the config file
func rcConfig(ctx context.Context, in rc.Params, what string) (out rc.Params, err error) {
name, err := in.GetString("name")
if err != nil {
return nil, err
}
parameters := rc.Params{}
err = in.GetStruct("parameters", ¶meters)
if err != nil {
return nil, err
}
var opt UpdateRemoteOpt
err = in.GetStruct("opt", &opt)
if err != nil && !rc.IsErrParamNotFound(err) {
return nil, err
}
// Backwards compatibility
if value, err := in.GetBool("obscure"); err == nil {
opt.Obscure = value
}
if value, err := in.GetBool("noObscure"); err == nil {
opt.NoObscure = value
}
var configOut *fs.ConfigOut
switch what {
case "create":
remoteType, typeErr := in.GetString("type")
if typeErr != nil {
return nil, typeErr
}
configOut, err = CreateRemote(ctx, name, remoteType, parameters, opt)
case "update":
configOut, err = UpdateRemote(ctx, name, parameters, opt)
case "password":
err = PasswordRemote(ctx, name, parameters)
default:
err = errors.New("unknown rcConfig type")
}
if err != nil {
return nil, err
}
if !opt.NonInteractive {
return nil, nil
}
if configOut == nil {
configOut = &fs.ConfigOut{}
}
err = rc.Reshape(&out, configOut)
if err != nil {
return nil, err
}
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "config/delete",
Fn: rcDelete,
Title: "Delete a remote in the config file.",
AuthRequired: true,
Help: `
Parameters:
- name - name of remote to delete
See the [config delete](/commands/rclone_config_delete/) command for more information on the above.
`,
})
}
// Return the config file delete
func rcDelete(ctx context.Context, in rc.Params) (out rc.Params, err error) {
name, err := in.GetString("name")
if err != nil {
return nil, err
}
DeleteRemote(name)
return nil, nil
}
func init() {
rc.Add(rc.Call{
Path: "config/setpath",
Fn: rcSetPath,
Title: "Set the path of the config file",
AuthRequired: true,
Help: `
Parameters:
- path - path to the config file to use
`,
})
}
// Set the config file path
func rcSetPath(ctx context.Context, in rc.Params) (out rc.Params, err error) {
path, err := in.GetString("path")
if err != nil {
return nil, err
}
err = SetConfigPath(path)
return nil, err
}
func init() {
rc.Add(rc.Call{
Path: "config/paths",
Fn: rcPaths,
Title: "Reads the config file path and other important paths.",
AuthRequired: true,
Help: `
Returns a JSON object with the following keys:
- config: path to config file
- cache: path to root of cache directory
- temp: path to root of temporary directory
Eg
{
"cache": "/home/USER/.cache/rclone",
"config": "/home/USER/.rclone.conf",
"temp": "/tmp"
}
See the [config paths](/commands/rclone_config_paths/) command for more information on the above.
`,
})
}
// Set the config file path
func rcPaths(ctx context.Context, in rc.Params) (out rc.Params, err error) {
return rc.Params{
"config": GetConfigPath(),
"cache": GetCacheDir(),
"temp": os.TempDir(),
}, nil
}