Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
remove need for scope validation
  • Loading branch information
codekeyz committed Feb 23, 2024
commit 17dd861014ec12b5cdf2da5393b810a90d251acb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class TokenCreateCommand extends BaseGlobeCommand {
FutureOr<int> run() async {
requireAuth();

final validated = await scope.validate();

final organization = await selectOrganization(logger: logger, api: api);
final name = argResults?['name']?.toString() ??
logger.prompt('❓ Provide name for token:');
final dateString = argResults?['expiry']?.toString() ??
Expand All @@ -52,7 +51,8 @@ class TokenCreateCommand extends BaseGlobeCommand {
}

final projects = await selectProjects(
validated.organization,
'Select projects to associate token with:',
organization,
logger: logger,
api: api,
scope: scope,
Expand All @@ -65,7 +65,7 @@ class TokenCreateCommand extends BaseGlobeCommand {

try {
final token = await api.createToken(
orgId: validated.organization.id,
orgId: organization.id,
name: name,
projectUuids: projects.map((e) => e.id).toList(),
expiresAt: expiry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mason_logger/mason_logger.dart';

import '../../command.dart';
import '../../utils/api.dart';
import '../../utils/prompts.dart';

class TokenDeleteCommand extends BaseGlobeCommand {
TokenDeleteCommand() {
Expand All @@ -23,7 +24,7 @@ class TokenDeleteCommand extends BaseGlobeCommand {
FutureOr<int> run() async {
requireAuth();

final validated = await scope.validate();
final organization = await selectOrganization(logger: logger, api: api);
final tokenId = (argResults?['tokenId'] as String?) ??
logger.prompt('❓ Provide id for token:');

Expand All @@ -32,7 +33,7 @@ class TokenDeleteCommand extends BaseGlobeCommand {

try {
await api.deleteToken(
orgId: validated.organization.id,
orgId: organization.id,
tokenId: tokenId,
);
deleteTokenProgress.complete('Token deleted');
Expand Down
34 changes: 26 additions & 8 deletions packages/globe_cli/lib/src/commands/token/token_list_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import 'package:mason_logger/mason_logger.dart';

import '../../command.dart';
import '../../utils/api.dart';
import '../../utils/prompts.dart';

class TokenListCommand extends BaseGlobeCommand {
TokenListCommand() {
argParser.addOption(
'project',
help: 'Specify project to list token for.',
);
}
@override
String get description => 'List globe auth tokens for current project';

Expand All @@ -16,19 +23,30 @@ class TokenListCommand extends BaseGlobeCommand {
FutureOr<int>? run() async {
requireAuth();

final validated = await scope.validate();
final projectName = cyan.wrap(validated.project.slug);
final organization = await selectOrganization(logger: logger, api: api);
final projectUuidsFromArgs = argResults?['project'] as String?;

final projects = await selectProjects(
'Select projects to list tokens for:',
organization,
logger: logger,
api: api,
scope: scope,
ids: projectUuidsFromArgs == null ? null : [projectUuidsFromArgs],
);

final listTokenProgress =
logger.progress('Listing Tokens for $projectName');
final projectNames = projects.map((e) => cyan.wrap(e.slug)).join(', ');
final listTokenProgress = logger.progress(
'Listing Tokens for $projectNames',
);

try {
final tokens = await api.listTokens(
orgId: validated.organization.id,
projectUuids: [validated.project.id],
orgId: organization.id,
projectUuids: projects.map((e) => e.id).toList(),
);
if (tokens.isEmpty) {
listTokenProgress.fail('No Tokens found for $projectName');
listTokenProgress.fail('No Tokens found for $projectNames');
return ExitCode.success.code;
}

Expand All @@ -39,7 +57,7 @@ class TokenListCommand extends BaseGlobeCommand {
Expiry: ${token.expiresAt.toLocal()}''';

listTokenProgress.complete(
'Tokens for $projectName\n${tokens.map(tokenLog).join('\n')}',
'Tokens for $projectNames\n${tokens.map(tokenLog).join('\n')}',
);

return ExitCode.success.code;
Expand Down
11 changes: 5 additions & 6 deletions packages/globe_cli/lib/src/utils/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,12 @@ class GlobeApi {
}) async {
requireAuth();

final listTokensPath =
'/orgs/$orgId/api-tokens?projects=${projectUuids.join(',')}';
logger.detail('API Request: GET $listTokensPath');
final fullUri = _buildUri('/orgs/$orgId/api-tokens')
.replace(queryParameters: {'projects': projectUuids});

final response = _handleResponse(
await http.get(_buildUri(listTokensPath), headers: headers),
)! as List<dynamic>;
logger.detail('API Request: GET /orgs/$orgId/api-tokens');
final response = _handleResponse(await http.get(fullUri, headers: headers))!
as List<dynamic>;

return response
.map((e) => Token.fromJson(e as Map<String, dynamic>))
Expand Down
3 changes: 2 additions & 1 deletion packages/globe_cli/lib/src/utils/prompts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ Future<Project> selectProject(
///
/// Optionally pass [ids] to only verify projects Ids actually exist
Future<List<Project>> selectProjects(
String question,
Organization organization, {
required Logger logger,
required GlobeApi api,
Expand Down Expand Up @@ -361,7 +362,7 @@ Future<List<Project>> selectProjects(

/// Ask user to choose zero or more options.
final selections = logger.chooseAny(
'❓ Select projects to associate token with:',
'❓ $question',
choices: projectsBySlug.keys.toList(),
);

Expand Down