OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library command_uploader; | |
6 | |
7 import 'dart:io'; | |
8 import 'dart:uri'; | |
9 | |
10 import '../../pkg/args/lib/args.dart'; | |
11 import 'entrypoint.dart'; | |
12 import 'exit_codes.dart' as exit_codes; | |
13 import 'io.dart'; | |
14 import 'log.dart' as log; | |
15 import 'oauth2.dart' as oauth2; | |
16 import 'path.dart' as path; | |
17 import 'pub.dart'; | |
18 | |
19 /// Handles the `uploader` pub command. | |
20 class UploaderCommand extends PubCommand { | |
21 final description = "Manage uploaders for a package on pub.dartlang.org."; | |
22 final usage = "pub uploader [options] {add/remove} email"; | |
Bob Nystrom
2012/12/17 18:41:58
"email" -> "<email>"
nweiz
2012/12/17 21:18:48
Done.
| |
23 final requiresEntrypoint = false; | |
24 | |
25 ArgParser get commandParser { | |
26 var parser = new ArgParser(); | |
27 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | |
Bob Nystrom
2012/12/17 18:41:58
Instead of a literal URI here, how about we make t
nweiz
2012/12/17 21:18:48
Right now using the hosted_source constant won't w
| |
28 help: 'The package server to which to upload this package'); | |
Bob Nystrom
2012/12/17 18:41:58
Make this relevant to this command.
nweiz
2012/12/17 21:18:48
Done.
| |
29 parser.addOption('package', help: 'The package whose uploaders will be ' | |
30 'modified\n' | |
31 '(defaults to the current package)'); | |
32 return parser; | |
33 } | |
34 | |
35 /// The URL of the package hosting server. | |
36 Uri get server => new Uri.fromString(commandOptions['server']); | |
Bob Nystrom
2012/12/17 18:41:58
Does this fail gracefully if the argument isn't a
nweiz
2012/12/17 21:18:48
I don't think Uri.fromString throws any parse erro
| |
37 | |
38 Future onRun() { | |
39 if (commandOptions.rest.isEmpty) { | |
40 log.error('No uploader command given.'); | |
41 this.printUsage(); | |
42 exit(exit_codes.USAGE); | |
43 } | |
44 | |
45 var command = commandOptions.rest.removeAt(0); | |
Bob Nystrom
2012/12/17 18:41:58
I think sometime soon I want to add real command s
nweiz
2012/12/17 21:18:48
I thought about doing that here, but decided this
| |
46 if (!['add', 'remove'].contains(command)) { | |
47 log.error('Unknown uploader command "$command".'); | |
48 this.printUsage(); | |
49 exit(exit_codes.USAGE); | |
50 } else if (commandOptions.rest.isEmpty) { | |
51 log.error('No package or uploader given for "pub uploader $command".'); | |
52 this.printUsage(); | |
53 exit(exit_codes.USAGE); | |
54 } | |
55 | |
56 return new Future.immediate(null).chain((_) { | |
57 var package = commandOptions['package']; | |
58 if (package != null) return new Future.immediate(package); | |
59 return Entrypoint.load(path.current, cache) | |
60 .transform((entrypoint) => entrypoint.root.name); | |
61 }).chain((package) { | |
62 var uploader = commandOptions.rest[0]; | |
63 return oauth2.withClient(cache, (client) { | |
64 if (command == 'add') { | |
65 var url = server.resolve("/packages/${encodeUriComponent(package)}" | |
66 "/uploaders.json"); | |
67 return client.post(url, fields: {"email": uploader}); | |
68 } else { // command == 'remove' | |
69 var url = server.resolve("/packages/${encodeUriComponent(package)}" | |
70 "/uploaders/${encodeUriComponent(uploader)}.json"); | |
71 return client.delete(url); | |
72 } | |
73 }); | |
74 }).transform(handleJsonSuccess).transformException((e) { | |
75 if (e is! PubHttpException) throw e; | |
76 handleJsonError(e.response); | |
77 }); | |
78 } | |
79 } | |
OLD | NEW |