Skip to content

Commit 73b66cd

Browse files
authored
Merge pull request #2035 from sigurdm/logout_command
Add logout command
2 parents 9007942 + 2b03587 commit 73b66cd

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

lib/src/command/logout.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019, 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+
import 'dart:async';
6+
7+
import '../command.dart';
8+
import '../oauth2.dart' as oauth2;
9+
10+
/// Handles the `logout` pub command.
11+
class LogoutCommand extends PubCommand {
12+
String get name => "logout";
13+
String get description => 'Log out of pub.dartlang.org.';
14+
String get invocation => 'pub logout';
15+
16+
LogoutCommand();
17+
18+
Future run() async {
19+
oauth2.logout(cache);
20+
}
21+
}

lib/src/command_runner.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'command/get.dart';
1818
import 'command/global.dart';
1919
import 'command/lish.dart';
2020
import 'command/list_package_dirs.dart';
21+
import 'command/logout.dart';
2122
import 'command/run.dart';
2223
import 'command/serve.dart';
2324
import 'command/upgrade.dart';
@@ -104,6 +105,7 @@ class PubCommandRunner extends CommandRunner {
104105
addCommand(ServeCommand());
105106
addCommand(UpgradeCommand());
106107
addCommand(UploaderCommand());
108+
addCommand(LogoutCommand());
107109
addCommand(VersionCommand());
108110
}
109111

lib/src/oauth2.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ void _clearCredentials(SystemCache cache) {
7373
if (entryExists(credentialsFile)) deleteEntry(credentialsFile);
7474
}
7575

76+
/// Try to delete the cached credentials.
77+
void logout(SystemCache cache) {
78+
var credentialsFile = _credentialsFile(cache);
79+
if (entryExists(_credentialsFile(cache))) {
80+
log.message("Logging out of pub.dartlang.org.");
81+
log.message("Deleting $credentialsFile");
82+
_clearCredentials(cache);
83+
} else {
84+
log.message(
85+
"No existing credentials file $credentialsFile. Cannot log out.");
86+
}
87+
}
88+
7689
/// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when
7790
/// the [Future] returned by [fn] completes.
7891
///

test/oauth2/logout_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2019, 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+
import 'package:shelf_test_handler/shelf_test_handler.dart';
6+
import 'package:test/test.dart';
7+
8+
import '../descriptor.dart' as d;
9+
import '../test_pub.dart';
10+
11+
main() {
12+
test('with an existing credentials.json, deletes it.', () async {
13+
var server = await ShelfTestServer.create();
14+
await d
15+
.credentialsFile(server, 'access token',
16+
refreshToken: 'refresh token',
17+
expiration: DateTime.now().add(Duration(hours: 1)))
18+
.create();
19+
20+
await runPub(
21+
args: ['logout'],
22+
output: contains('Logging out of pub.dartlang.org.'),
23+
exitCode: 0);
24+
25+
await d.dir(cachePath, [d.nothing('credentials.json')]).validate();
26+
});
27+
test('with no existing credentials.json, notifies.', () async {
28+
await d.dir(cachePath, [d.nothing('credentials.json')]).create();
29+
await runPub(
30+
args: ['logout'],
31+
output: contains('No existing credentials file'),
32+
exitCode: 0);
33+
34+
await d.dir(cachePath, [d.nothing('credentials.json')]).validate();
35+
});
36+
}

test/pub_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ main() {
3535
get Get the current package's dependencies.
3636
global Work with global packages.
3737
help Display help information for pub.
38+
logout Log out of pub.dartlang.org.
3839
publish Publish the current package to pub.dartlang.org.
3940
run Run an executable from a package.
4041
upgrade Upgrade the current package's dependencies to latest versions.

0 commit comments

Comments
 (0)