Skip to content

Commit

Permalink
feat: Add --project and --org options on commands that depend on …
Browse files Browse the repository at this point in the history
…"scope". (#100)

* Add `--project` and `--org` options on commands that depend on "scope".

* Lint

* Lint

* Sort
  • Loading branch information
rrousselGit authored Jul 21, 2024
1 parent 44f44e7 commit a11ec77
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 198 deletions.
31 changes: 16 additions & 15 deletions examples/flutter_web/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.1"
lints:
dependency: transitive
description:
Expand All @@ -119,18 +119,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.15.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -188,10 +188,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.7.2"
vector_math:
dependency: transitive
description:
Expand All @@ -204,9 +204,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev"
source: hosted
version: "13.0.0"
version: "14.2.4"
sdks:
dart: ">=3.2.0-0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
20 changes: 20 additions & 0 deletions packages/globe_cli/lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ typedef RunProcess = Future<ProcessResult> Function(
bool runInShell,
});

typedef ScopeValidator = Future<ScopeValidation> Function();

abstract class BaseGlobeCommand extends Command<int> {
GlobeAuth get auth => GetIt.I();
GlobeApi get api => GetIt.I();
Expand All @@ -42,4 +44,22 @@ abstract class BaseGlobeCommand extends Command<int> {

// TODO(ehesp): Check for JWT expiry and refresh if needed?
}

ScopeValidator declareScopeArguments() {
argParser
..addOption(
'org',
abbr: 'o',
help: 'The organization ID used by this command. '
'Defaults to what was previously linked using `globe link`.',
)
..addOption(
'project',
abbr: 'p',
help: 'The Project ID used by this command. '
'Defaults to what was previously linked using `globe link`.',
);

return () => scope.validate(argResults);
}
}
14 changes: 7 additions & 7 deletions packages/globe_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'commands/commands.dart';
import 'commands/create_project_command.dart';
import 'commands/project_command.dart';
import 'commands/update.dart';
import 'get_it.dart';
import 'package_info.dart' as package_info;
import 'utils/api.dart';
import 'utils/auth.dart';
Expand Down Expand Up @@ -93,19 +94,18 @@ class GlobeCliCommandRunner extends CompletionCommandRunner<int> {
await _checkForUpdates();
}

final auth = GlobeAuth(metadata);
final api = GlobeApi(
metadata: metadata,
auth: auth,
logger: _logger,
final auth = GetIt.instance.singletonPutIfAbsent<GlobeAuth>(
() => GlobeAuth(metadata),
);
final api = GetIt.instance.singletonPutIfAbsent<GlobeApi>(() {
return GlobeApi(metadata: metadata, auth: auth, logger: _logger);
});
final scope = GlobeScope(
api: api,
metadata: metadata,
logger: _logger,
);
GetIt.instance.registerSingleton<GlobeAuth>(auth);
GetIt.instance.registerSingleton<GlobeApi>(api);

GetIt.instance.registerSingleton<GlobeMetadata>(metadata);
GetIt.instance.registerSingleton<GlobeScope>(scope);

Expand Down
9 changes: 4 additions & 5 deletions packages/globe_cli/lib/src/commands/build_logs_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class BuildLogsCommand extends BaseGlobeCommand {
abbr: 'd',
help: 'Deployment ID.',
);
_validator = declareScopeArguments();
}

late final ScopeValidator _validator;

@override
String get description => 'View build logs for a given deployment ID.';

Expand All @@ -22,11 +25,7 @@ class BuildLogsCommand extends BaseGlobeCommand {
Future<int> run() async {
requireAuth();

if (!scope.hasScope()) {
logger.err('Not a Globe project.');
}

final validated = await scope.validate();
final validated = await _validator();
final deploymentId = argResults!['deployment'] as String?;

if (deploymentId == null) {
Expand Down
12 changes: 5 additions & 7 deletions packages/globe_cli/lib/src/commands/deploy_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ class DeployCommand extends BaseGlobeCommand {
'token',
abbr: 't',
help: 'Set the API token for deployment. Also needs --project',
)
..addOption(
'project',
abbr: 'p',
help:
'Set the project for deployment with API token. Used with --token',
);

_validator = declareScopeArguments();
}

@override
Expand All @@ -50,6 +46,8 @@ class DeployCommand extends BaseGlobeCommand {
? DeploymentEnvironment.production
: DeploymentEnvironment.preview;

late final ScopeValidator _validator;

@override
Future<int> run() async {
if (argResults?['token'] != null && argResults?['project'] != null) {
Expand Down Expand Up @@ -77,7 +75,7 @@ class DeployCommand extends BaseGlobeCommand {
await linkProject(logger: logger, api: api);
}

final validated = await scope.validate();
final validated = await _validator();
if (validated.project.paused) {
logger
..err('No new deployments can be created for this project.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import '../../command.dart';
import '../../utils/api.dart';

class ProjectPauseCommand extends BaseGlobeCommand {
ProjectPauseCommand() {
_validator = declareScopeArguments();
}

@override
String get description => 'Pause the current globe project';

@override
String get name => 'pause';

late final ScopeValidator _validator;

@override
FutureOr<int> run() async {
requireAuth();

if (!scope.hasScope()) {
logger.err('Not a Globe project.');
}

final validated = await scope.validate();
final validated = await _validator();
final projectSlug = validated.project.slug;
final pauseProjectProgress =
logger.progress('Pausing project: ${cyan.wrap(projectSlug)}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import '../../command.dart';
import '../../utils/api.dart';

class ProjectResumeCommand extends BaseGlobeCommand {
ProjectResumeCommand() {
_validator = declareScopeArguments();
}

@override
String get description => 'Resume the current globe project';

@override
String get name => 'resume';

late final ScopeValidator _validator;

@override
FutureOr<int> run() async {
requireAuth();

if (!scope.hasScope()) {
logger.err('Not a Globe project.');
}

final validated = await scope.validate();
final validated = await _validator();
final projectSlug = validated.project.slug;
final pauseProjectProgress =
logger.progress('Resuming project: ${cyan.wrap(projectSlug)}');
Expand Down
15 changes: 15 additions & 0 deletions packages/globe_cli/lib/src/get_it.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:get_it/get_it.dart';

extension GetItX on GetIt {
/// Initialize a singleton if it is not already registered.
///
/// This is mainly useful for tests, where tests register overrides
/// for singletons.
T singletonPutIfAbsent<T extends Object>(T Function() value) {
if (!isRegistered<T>()) {
registerSingleton<T>(value());
}

return get<T>();
}
}
10 changes: 5 additions & 5 deletions packages/globe_cli/lib/src/utils/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class SettingsConnection {
}

class Organization {
Organization._({
Organization({
required this.id,
required this.name,
required this.slug,
Expand All @@ -429,7 +429,7 @@ class Organization {
'createdAt': final String createdAt,
'updatedAt': final String updatedAt,
} =>
Organization._(
Organization(
id: id,
name: name,
slug: slug,
Expand All @@ -452,7 +452,7 @@ class Organization {
}

class Project {
Project._({
Project({
required this.id,
required this.orgId,
required this.slug,
Expand All @@ -471,7 +471,7 @@ class Project {
'createdAt': final String createdAt,
'updatedAt': final String updatedAt,
} =>
Project._(
Project(
id: id,
orgId: organizationId,
slug: slug,
Expand All @@ -486,7 +486,7 @@ class Project {
'createdAt': final String createdAt,
'updatedAt': final String updatedAt,
} =>
Project._(
Project(
id: id,
orgId: organizationId,
slug: slug,
Expand Down
2 changes: 0 additions & 2 deletions packages/globe_cli/lib/src/utils/prompts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Future<ScopeMetadata> linkProject({
organization,
logger: logger,
api: api,
scope: scope,
);

final result = scope.setScope(
Expand Down Expand Up @@ -119,7 +118,6 @@ Future<Project> selectProject(
Organization organization, {
required Logger logger,
required GlobeApi api,
required GlobeScope scope,
}) async {
logger.detail('Fetching organization projects');
final projects = await api.getProjects(org: organization.id);
Expand Down
Loading

0 comments on commit a11ec77

Please sign in to comment.