Chromium Code Reviews| Index: utils/pub/command_uploader.dart |
| diff --git a/utils/pub/command_uploader.dart b/utils/pub/command_uploader.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9edbb8f6eaec06272965805683dcd1d717d69456 |
| --- /dev/null |
| +++ b/utils/pub/command_uploader.dart |
| @@ -0,0 +1,79 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library command_uploader; |
| + |
| +import 'dart:io'; |
| +import 'dart:uri'; |
| + |
| +import '../../pkg/args/lib/args.dart'; |
| +import 'entrypoint.dart'; |
| +import 'exit_codes.dart' as exit_codes; |
| +import 'io.dart'; |
| +import 'log.dart' as log; |
| +import 'oauth2.dart' as oauth2; |
| +import 'path.dart' as path; |
| +import 'pub.dart'; |
| + |
| +/// Handles the `uploader` pub command. |
| +class UploaderCommand extends PubCommand { |
| + final description = "Manage uploaders for a package on pub.dartlang.org."; |
| + 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.
|
| + final requiresEntrypoint = false; |
| + |
| + ArgParser get commandParser { |
| + var parser = new ArgParser(); |
| + 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
|
| + 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.
|
| + parser.addOption('package', help: 'The package whose uploaders will be ' |
| + 'modified\n' |
| + '(defaults to the current package)'); |
| + return parser; |
| + } |
| + |
| + /// The URL of the package hosting server. |
| + 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
|
| + |
| + Future onRun() { |
| + if (commandOptions.rest.isEmpty) { |
| + log.error('No uploader command given.'); |
| + this.printUsage(); |
| + exit(exit_codes.USAGE); |
| + } |
| + |
| + 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
|
| + if (!['add', 'remove'].contains(command)) { |
| + log.error('Unknown uploader command "$command".'); |
| + this.printUsage(); |
| + exit(exit_codes.USAGE); |
| + } else if (commandOptions.rest.isEmpty) { |
| + log.error('No package or uploader given for "pub uploader $command".'); |
| + this.printUsage(); |
| + exit(exit_codes.USAGE); |
| + } |
| + |
| + return new Future.immediate(null).chain((_) { |
| + var package = commandOptions['package']; |
| + if (package != null) return new Future.immediate(package); |
| + return Entrypoint.load(path.current, cache) |
| + .transform((entrypoint) => entrypoint.root.name); |
| + }).chain((package) { |
| + var uploader = commandOptions.rest[0]; |
| + return oauth2.withClient(cache, (client) { |
| + if (command == 'add') { |
| + var url = server.resolve("/packages/${encodeUriComponent(package)}" |
| + "/uploaders.json"); |
| + return client.post(url, fields: {"email": uploader}); |
| + } else { // command == 'remove' |
| + var url = server.resolve("/packages/${encodeUriComponent(package)}" |
| + "/uploaders/${encodeUriComponent(uploader)}.json"); |
| + return client.delete(url); |
| + } |
| + }); |
| + }).transform(handleJsonSuccess).transformException((e) { |
| + if (e is! PubHttpException) throw e; |
| + handleJsonError(e.response); |
| + }); |
| + } |
| +} |