diff --git a/packages/shorebird_cli/README.md b/packages/shorebird_cli/README.md index fcae6fd8..6835b5c7 100644 --- a/packages/shorebird_cli/README.md +++ b/packages/shorebird_cli/README.md @@ -78,6 +78,21 @@ shorebird logout ✓ Logging out of shorebird.dev (1ms) ``` +### Account + +To see information about your Shorebird account, use the `shorebird account` command. + +```bash +shorebird account +``` + +**Sample** + +``` +$ shorebird account +You are logged in as +``` + ### Doctor To check your environment for common issues, use the `shorebird doctor` command. @@ -90,10 +105,13 @@ shorebird doctor ``` $ shorebird doctor -Doctor summary Shorebird v0.0.3 +✓ Shorebird is up-to-date (0.7s) +✓ Flutter install is correct (0.1s) +✓ AndroidManifest.xml files contain INTERNET permission (26ms) + No issues detected! ``` diff --git a/packages/shorebird_cli/lib/src/command_runner.dart b/packages/shorebird_cli/lib/src/command_runner.dart index 25671a30..04243cbe 100644 --- a/packages/shorebird_cli/lib/src/command_runner.dart +++ b/packages/shorebird_cli/lib/src/command_runner.dart @@ -35,6 +35,7 @@ class ShorebirdCliCommandRunner extends CompletionCommandRunner { help: 'Noisy logging, including all shell commands executed.', ); + addCommand(AccountCommand(logger: _logger)); addCommand(AppsCommand(logger: _logger)); addCommand(BuildCommand(logger: _logger)); addCommand(ChannelsCommand(logger: _logger)); diff --git a/packages/shorebird_cli/lib/src/commands/account_command.dart b/packages/shorebird_cli/lib/src/commands/account_command.dart new file mode 100644 index 00000000..ee466b96 --- /dev/null +++ b/packages/shorebird_cli/lib/src/commands/account_command.dart @@ -0,0 +1,29 @@ +import 'dart:async'; + +import 'package:mason_logger/mason_logger.dart'; +import 'package:shorebird_cli/src/command.dart'; + +class AccountCommand extends ShorebirdCommand { + AccountCommand({required super.logger, super.auth}); + + @override + String get name => 'account'; + + @override + String get description => 'Show information about the logged-in user'; + + @override + Future run() async { + if (!auth.isAuthenticated) { + logger.info( + 'You are not logged in.' + ' Run ${green.wrap('shorebird login')} to log in.', + ); + return ExitCode.success.code; + } + + logger.info('You are logged in as <${auth.user!.email}>'); + + return ExitCode.success.code; + } +} diff --git a/packages/shorebird_cli/lib/src/commands/commands.dart b/packages/shorebird_cli/lib/src/commands/commands.dart index 2b2cca7e..497b7ce5 100644 --- a/packages/shorebird_cli/lib/src/commands/commands.dart +++ b/packages/shorebird_cli/lib/src/commands/commands.dart @@ -1,3 +1,4 @@ +export 'account_command.dart'; export 'apps/apps.dart'; export 'build_command.dart'; export 'channels/channels.dart'; diff --git a/packages/shorebird_cli/test/src/commands/account_command_test.dart b/packages/shorebird_cli/test/src/commands/account_command_test.dart new file mode 100644 index 00000000..c2f6af6c --- /dev/null +++ b/packages/shorebird_cli/test/src/commands/account_command_test.dart @@ -0,0 +1,48 @@ +import 'package:mason_logger/mason_logger.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:shorebird_cli/src/auth/auth.dart'; +import 'package:shorebird_cli/src/commands/account_command.dart'; +import 'package:test/test.dart'; + +class _MockAuth extends Mock implements Auth {} + +class _MockLogger extends Mock implements Logger {} + +void main() { + group('AccountCommand', () { + const user = User(email: 'hello@shorebird.dev'); + + late Auth auth; + late Logger logger; + late AccountCommand accountCommand; + + setUp(() { + auth = _MockAuth(); + logger = _MockLogger(); + accountCommand = AccountCommand(logger: logger, auth: auth); + + when(() => auth.isAuthenticated).thenReturn(true); + when(() => auth.user).thenReturn(user); + }); + + test("doesn't do anything if no user is logged in", () async { + when(() => auth.isAuthenticated).thenReturn(false); + + final result = await accountCommand.run(); + + expect(result, equals(ExitCode.success.code)); + verify( + () => logger.info(any(that: contains('You are not logged in'))), + ).called(1); + }); + + test('prints the email address of the current user', () async { + final result = await accountCommand.run(); + + expect(result, equals(ExitCode.success.code)); + verify( + () => logger.info('You are logged in as '), + ).called(1); + }); + }); +}