From 17dd861014ec12b5cdf2da5393b810a90d251acb Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Fri, 23 Feb 2024 10:50:57 +0000 Subject: [PATCH 1/3] remove need for scope validation --- .../commands/token/token_create_command.dart | 8 ++--- .../commands/token/token_delete_command.dart | 5 +-- .../commands/token/token_list_command.dart | 34 ++++++++++++++----- packages/globe_cli/lib/src/utils/api.dart | 11 +++--- packages/globe_cli/lib/src/utils/prompts.dart | 3 +- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/globe_cli/lib/src/commands/token/token_create_command.dart b/packages/globe_cli/lib/src/commands/token/token_create_command.dart index 1ad8bf0c..94663f56 100644 --- a/packages/globe_cli/lib/src/commands/token/token_create_command.dart +++ b/packages/globe_cli/lib/src/commands/token/token_create_command.dart @@ -36,8 +36,7 @@ class TokenCreateCommand extends BaseGlobeCommand { FutureOr 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() ?? @@ -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, @@ -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, diff --git a/packages/globe_cli/lib/src/commands/token/token_delete_command.dart b/packages/globe_cli/lib/src/commands/token/token_delete_command.dart index 425e6e8d..ccfde2d0 100644 --- a/packages/globe_cli/lib/src/commands/token/token_delete_command.dart +++ b/packages/globe_cli/lib/src/commands/token/token_delete_command.dart @@ -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() { @@ -23,7 +24,7 @@ class TokenDeleteCommand extends BaseGlobeCommand { FutureOr 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:'); @@ -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'); diff --git a/packages/globe_cli/lib/src/commands/token/token_list_command.dart b/packages/globe_cli/lib/src/commands/token/token_list_command.dart index 39051d6f..d2ac366e 100644 --- a/packages/globe_cli/lib/src/commands/token/token_list_command.dart +++ b/packages/globe_cli/lib/src/commands/token/token_list_command.dart @@ -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'; @@ -16,19 +23,30 @@ class TokenListCommand extends BaseGlobeCommand { FutureOr? 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; } @@ -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; diff --git a/packages/globe_cli/lib/src/utils/api.dart b/packages/globe_cli/lib/src/utils/api.dart index 8e0dd87c..2d3897b2 100644 --- a/packages/globe_cli/lib/src/utils/api.dart +++ b/packages/globe_cli/lib/src/utils/api.dart @@ -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; + logger.detail('API Request: GET /orgs/$orgId/api-tokens'); + final response = _handleResponse(await http.get(fullUri, headers: headers))! + as List; return response .map((e) => Token.fromJson(e as Map)) diff --git a/packages/globe_cli/lib/src/utils/prompts.dart b/packages/globe_cli/lib/src/utils/prompts.dart index abb0bb4c..a42d0eb9 100644 --- a/packages/globe_cli/lib/src/utils/prompts.dart +++ b/packages/globe_cli/lib/src/utils/prompts.dart @@ -319,6 +319,7 @@ Future selectProject( /// /// Optionally pass [ids] to only verify projects Ids actually exist Future> selectProjects( + String question, Organization organization, { required Logger logger, required GlobeApi api, @@ -361,7 +362,7 @@ Future> selectProjects( /// Ask user to choose zero or more options. final selections = logger.chooseAny( - '❓ Select projects to associate token with:', + '❓ $question', choices: projectsBySlug.keys.toList(), ); From e2a2fb0d4999269133da8cdb761d4568c1b1fb07 Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Fri, 23 Feb 2024 10:56:27 +0000 Subject: [PATCH 2/3] - --- .../globe_cli/lib/src/commands/token/token_list_command.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/globe_cli/lib/src/commands/token/token_list_command.dart b/packages/globe_cli/lib/src/commands/token/token_list_command.dart index d2ac366e..f82da4fe 100644 --- a/packages/globe_cli/lib/src/commands/token/token_list_command.dart +++ b/packages/globe_cli/lib/src/commands/token/token_list_command.dart @@ -10,11 +10,11 @@ class TokenListCommand extends BaseGlobeCommand { TokenListCommand() { argParser.addOption( 'project', - help: 'Specify project to list token for.', + help: 'Specify projects(s) to list token for.', ); } @override - String get description => 'List globe auth tokens for current project'; + String get description => 'List globe auth tokens for project(s)'; @override String get name => 'list'; From 624c1e960139c4b51265d5b589fe8076afeebbd6 Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Fri, 23 Feb 2024 10:57:32 +0000 Subject: [PATCH 3/3] - --- .../lib/src/commands/token/token_list_command.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/globe_cli/lib/src/commands/token/token_list_command.dart b/packages/globe_cli/lib/src/commands/token/token_list_command.dart index f82da4fe..ff7bc7e0 100644 --- a/packages/globe_cli/lib/src/commands/token/token_list_command.dart +++ b/packages/globe_cli/lib/src/commands/token/token_list_command.dart @@ -8,7 +8,7 @@ import '../../utils/prompts.dart'; class TokenListCommand extends BaseGlobeCommand { TokenListCommand() { - argParser.addOption( + argParser.addMultiOption( 'project', help: 'Specify projects(s) to list token for.', ); @@ -24,15 +24,13 @@ class TokenListCommand extends BaseGlobeCommand { requireAuth(); 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], + ids: argResults?['project'] as List?, ); final projectNames = projects.map((e) => cyan.wrap(e.slug)).join(', ');