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:async'; | |
8 import 'dart:io'; | |
9 import 'dart:uri'; | |
10 | |
11 import 'package:args/args.dart'; | |
12 import 'package:pathos/path.dart' as path; | |
13 | |
14 import 'entrypoint.dart'; | |
15 import 'exit_codes.dart' as exit_codes; | |
16 import 'http.dart'; | |
17 import 'io.dart'; | |
18 import 'log.dart' as log; | |
19 import 'oauth2.dart' as oauth2; | |
20 import 'pub.dart'; | |
21 import 'utils.dart'; | |
22 | |
23 /// Handles the `uploader` pub command. | |
24 class UploaderCommand extends PubCommand { | |
25 final description = "Manage uploaders for a package on pub.dartlang.org."; | |
26 final usage = "pub uploader [options] {add/remove} <email>"; | |
27 final requiresEntrypoint = false; | |
28 | |
29 ArgParser get commandParser { | |
30 var parser = new ArgParser(); | |
31 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use | |
32 // dart:io for HTTPS requests. | |
33 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | |
34 help: 'The package server on which the package is hosted'); | |
35 parser.addOption('package', help: 'The package whose uploaders will be ' | |
36 'modified\n' | |
37 '(defaults to the current package)'); | |
38 return parser; | |
39 } | |
40 | |
41 /// The URL of the package hosting server. | |
42 Uri get server => Uri.parse(commandOptions['server']); | |
43 | |
44 Future onRun() { | |
45 if (commandOptions.rest.isEmpty) { | |
46 log.error('No uploader command given.'); | |
47 this.printUsage(); | |
48 exit(exit_codes.USAGE); | |
49 } | |
50 | |
51 var command = commandOptions.rest.removeAt(0); | |
52 if (!['add', 'remove'].contains(command)) { | |
53 log.error('Unknown uploader command "$command".'); | |
54 this.printUsage(); | |
55 exit(exit_codes.USAGE); | |
56 } else if (commandOptions.rest.isEmpty) { | |
57 log.error('No uploader given for "pub uploader $command".'); | |
58 this.printUsage(); | |
59 exit(exit_codes.USAGE); | |
60 } | |
61 | |
62 return new Future.sync(() { | |
63 var package = commandOptions['package']; | |
64 if (package != null) return package; | |
65 return new Entrypoint(path.current, cache).root.name; | |
66 }).then((package) { | |
67 var uploader = commandOptions.rest[0]; | |
68 return oauth2.withClient(cache, (client) { | |
69 if (command == 'add') { | |
70 var url = server.resolve("/packages/${encodeUriComponent(package)}" | |
71 "/uploaders.json"); | |
72 return client.post(url, fields: {"email": uploader}); | |
73 } else { // command == 'remove' | |
74 var url = server.resolve("/packages/${encodeUriComponent(package)}" | |
75 "/uploaders/${encodeUriComponent(uploader)}.json"); | |
76 return client.delete(url); | |
77 } | |
78 }); | |
79 }).then(handleJsonSuccess) | |
80 .catchError((error) => handleJsonError(error.response), | |
81 test: (e) => e is PubHttpException); | |
82 } | |
83 } | |
OLD | NEW |