-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(shorebird_cli): shorebird channels list
#201
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/shorebird_cli/lib/src/commands/channels/channels.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'channels_command.dart'; | ||
export 'list_channels_command.dart'; |
19 changes: 19 additions & 0 deletions
19
packages/shorebird_cli/lib/src/commands/channels/channels_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:shorebird_cli/src/command.dart'; | ||
import 'package:shorebird_cli/src/commands/commands.dart'; | ||
|
||
/// {@template channels_command} | ||
/// `shorebird channels` | ||
/// Manage the channels for your Shorebird app. | ||
/// {@endtemplate} | ||
class ChannelsCommand extends ShorebirdCommand { | ||
/// {@macro channels_command} | ||
ChannelsCommand({required super.logger}) { | ||
addSubcommand(ListChannelsCommand(logger: logger)); | ||
} | ||
|
||
@override | ||
String get description => 'Manage the channels for your Shorebird app.'; | ||
|
||
@override | ||
String get name => 'channels'; | ||
} |
110 changes: 110 additions & 0 deletions
110
packages/shorebird_cli/lib/src/commands/channels/list_channels_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:barbecue/barbecue.dart'; | ||
import 'package:mason_logger/mason_logger.dart'; | ||
import 'package:shorebird_cli/src/command.dart'; | ||
import 'package:shorebird_cli/src/shorebird_config_mixin.dart'; | ||
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; | ||
|
||
/// {@template list_channels_command} | ||
/// `shorebird channels list` | ||
/// List all channels for a Shorebird app. | ||
/// {@endtemplate} | ||
class ListChannelsCommand extends ShorebirdCommand with ShorebirdConfigMixin { | ||
/// {@macro list_channels_command} | ||
ListChannelsCommand({ | ||
required super.logger, | ||
super.buildCodePushClient, | ||
super.auth, | ||
}) { | ||
argParser.addOption( | ||
_appIdOption, | ||
help: 'The app id to list channels for.', | ||
); | ||
} | ||
|
||
static const String _appIdOption = 'app-id'; | ||
|
||
@override | ||
String get description => 'List all channels for a Shorebird app.'; | ||
|
||
@override | ||
String get name => 'list'; | ||
|
||
@override | ||
List<String> get aliases => ['ls']; | ||
|
||
@override | ||
Future<int>? run() async { | ||
final session = auth.currentSession; | ||
if (session == null) { | ||
logger.err('You must be logged in to view channels.'); | ||
return ExitCode.noUser.code; | ||
} | ||
|
||
final client = buildCodePushClient( | ||
apiKey: session.apiKey, | ||
hostedUri: hostedUri, | ||
); | ||
|
||
final appId = results[_appIdOption] as String? ?? getShorebirdYaml()?.appId; | ||
if (appId == null) { | ||
logger.err( | ||
''' | ||
Could not find an app id. | ||
|
||
You must either specify an app id via the "--$_appIdOption" flag or run this command from within a directory with a valid "shorebird.yaml" file.''', | ||
); | ||
return ExitCode.usage.code; | ||
} | ||
|
||
final List<Channel> channels; | ||
try { | ||
channels = await client.getChannels(appId: appId); | ||
} catch (error) { | ||
logger.err('$error'); | ||
return ExitCode.software.code; | ||
} | ||
|
||
logger.info( | ||
''' | ||
π± App ID: ${lightCyan.wrap(appId)} | ||
πΊ Channels''', | ||
); | ||
|
||
if (channels.isEmpty) { | ||
logger.info('(empty)'); | ||
return ExitCode.success.code; | ||
} | ||
|
||
logger.info(channels.prettyPrint()); | ||
|
||
return ExitCode.success.code; | ||
} | ||
} | ||
|
||
extension on List<Channel> { | ||
String prettyPrint() { | ||
const cellStyle = CellStyle( | ||
paddingLeft: 1, | ||
paddingRight: 1, | ||
borderBottom: true, | ||
borderTop: true, | ||
borderLeft: true, | ||
borderRight: true, | ||
); | ||
return Table( | ||
cellStyle: cellStyle, | ||
header: const TableSection( | ||
rows: [ | ||
Row(cells: [Cell('Name')]) | ||
], | ||
), | ||
body: TableSection( | ||
rows: [ | ||
for (final channel in this) Row(cells: [Cell(channel.name)]), | ||
], | ||
), | ||
).render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/shorebird_cli/test/src/commands/channels/list_channels_command_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'package:args/args.dart'; | ||
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/auth/session.dart'; | ||
import 'package:shorebird_cli/src/commands/commands.dart'; | ||
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class _MockArgResults extends Mock implements ArgResults {} | ||
|
||
class _MockAuth extends Mock implements Auth {} | ||
|
||
class _MockCodePushClient extends Mock implements CodePushClient {} | ||
|
||
class _MockLogger extends Mock implements Logger {} | ||
|
||
void main() { | ||
group('list', () { | ||
const session = Session(apiKey: 'test-api-key'); | ||
const appId = 'test-app-id'; | ||
|
||
late ArgResults argResults; | ||
late Auth auth; | ||
late CodePushClient codePushClient; | ||
late Logger logger; | ||
late ListChannelsCommand command; | ||
|
||
setUp(() { | ||
argResults = _MockArgResults(); | ||
auth = _MockAuth(); | ||
codePushClient = _MockCodePushClient(); | ||
logger = _MockLogger(); | ||
command = ListChannelsCommand( | ||
auth: auth, | ||
buildCodePushClient: ({required String apiKey, Uri? hostedUri}) { | ||
return codePushClient; | ||
}, | ||
logger: logger, | ||
)..testArgResults = argResults; | ||
|
||
when(() => argResults['app-id']).thenReturn(appId); | ||
when(() => auth.currentSession).thenReturn(session); | ||
}); | ||
|
||
test('description is correct', () { | ||
expect( | ||
command.description, | ||
equals('List all channels for a Shorebird app.'), | ||
); | ||
}); | ||
|
||
test('returns ExitCode.noUser when not logged in', () async { | ||
when(() => auth.currentSession).thenReturn(null); | ||
expect(await command.run(), ExitCode.noUser.code); | ||
}); | ||
|
||
test('returns ExitCode.usage when app id is missing.', () async { | ||
when(() => argResults['app-id']).thenReturn(null); | ||
expect(await command.run(), ExitCode.usage.code); | ||
}); | ||
|
||
test('returns ExitCode.software when unable to get channels', () async { | ||
when( | ||
() => codePushClient.getChannels(appId: any(named: 'appId')), | ||
).thenThrow(Exception()); | ||
expect(await command.run(), ExitCode.software.code); | ||
}); | ||
|
||
test('returns ExitCode.success when channels are empty', () async { | ||
when( | ||
() => codePushClient.getChannels(appId: any(named: 'appId')), | ||
).thenAnswer((_) async => []); | ||
expect(await command.run(), ExitCode.success.code); | ||
verify(() => logger.info('(empty)')).called(1); | ||
}); | ||
|
||
test('returns ExitCode.success when channels are not empty', () async { | ||
final channels = [ | ||
const Channel(id: 0, appId: appId, name: 'stable'), | ||
const Channel(id: 1, appId: appId, name: 'development'), | ||
]; | ||
when( | ||
() => codePushClient.getChannels(appId: any(named: 'appId')), | ||
).thenAnswer((_) async => channels); | ||
expect(await command.run(), ExitCode.success.code); | ||
verify( | ||
() => logger.info( | ||
''' | ||
π± App ID: ${lightCyan.wrap(appId)} | ||
πΊ Channels''', | ||
), | ||
).called(1); | ||
verify( | ||
() => logger.info( | ||
''' | ||
βββββββββββββββ | ||
β Name β | ||
βββββββββββββββ€ | ||
β stable β | ||
βββββββββββββββ€ | ||
β development β | ||
βββββββββββββββ''', | ||
), | ||
).called(1); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminds me that we will eventually care about providence of our dependencies. Not yet, but as an SDK that will produce binaries that run on millions (billions!) of devices we will need to care about our supply chain so we're not an eventual EventStream/Solarwinds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup it's Apache 2.0 but we can always roll our own or use something else.