Skip to content

Commit a39cb38

Browse files
authored
feat(cli): Add celest auth token command (#345)
Adds a command for retrieving the token of the current user. Re-organizes CLI commands into a better folder structure.
1 parent 959c3c9 commit a39cb38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+123
-62
lines changed

apps/cli/bin/celest.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'package:celest_cli/celest_cli.dart';
2-
import 'package:celest_cli/src/commands/auth_command.dart';
3-
import 'package:celest_cli/src/commands/deploy_command.dart';
4-
import 'package:celest_cli/src/commands/organizations/organizations_command.dart';
5-
import 'package:celest_cli/src/commands/project_environments/project_environments_command.dart';
6-
import 'package:celest_cli/src/commands/projects/projects_command.dart';
7-
import 'package:celest_cli/src/commands/status_command.dart';
2+
import 'package:celest_cli/src/commands/auth/auth_command.dart';
3+
import 'package:celest_cli/src/commands/cloud/organizations/organizations_command.dart';
4+
import 'package:celest_cli/src/commands/cloud/project_environments/project_environments_command.dart';
5+
import 'package:celest_cli/src/commands/cloud/projects/projects_command.dart';
6+
import 'package:celest_cli/src/commands/project/deploy_command.dart';
7+
import 'package:celest_cli/src/commands/project/status_command.dart';
88
import 'package:celest_cli/src/performance/sentry_perf.dart';
99

1010
void main(List<String> args) async {

apps/cli/lib/celest_cli.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export 'src/cli/cli.dart';
2-
export 'src/commands/analysis_server_command.dart';
3-
export 'src/commands/build_command.dart';
4-
export 'src/commands/init_command.dart';
5-
export 'src/commands/precache_command.dart';
6-
export 'src/commands/start_command.dart';
7-
export 'src/commands/uninstall_command.dart';
8-
export 'src/commands/upgrade_command.dart';
2+
export 'src/commands/project/analysis_server_command.dart';
3+
export 'src/commands/project/build_command.dart';
4+
export 'src/commands/project/init_command.dart';
5+
export 'src/commands/project/precache_command.dart';
6+
export 'src/commands/project/start_command.dart';
7+
export 'src/commands/uninstall/uninstall_command.dart';
8+
export 'src/commands/upgrade/upgrade_command.dart';
99
export 'src/version.dart';

apps/cli/lib/src/cli/cli.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,19 @@ final class Cli {
145145
}
146146

147147
Future<void> run(List<String> args) async {
148-
var verbose = ctx.platform.environment.containsKey('CELEST_VERBOSE');
149-
var jsonOutput = false;
150148
_runner.argParser
151149
..addFlag(
152150
'verbose',
153151
abbr: 'v',
154152
help: 'Enable verbose logging',
155153
negatable: false,
156154
defaultsTo: false,
157-
callback: (v) => verbose = v,
158155
)
159156
..addFlag(
160157
'json',
161158
negatable: false,
162159
help: 'Run CLI with JSON input/output',
163160
hide: true,
164-
callback: (j) => jsonOutput = j,
165161
)
166162
..addFlag(
167163
'version',
@@ -170,6 +166,10 @@ final class Cli {
170166
);
171167
final argResults = _runner.parse(args);
172168

169+
final verbose = ctx.platform.environment.containsKey('CELEST_VERBOSE') ||
170+
argResults.flag('verbose');
171+
final jsonOutput = argResults.flag('json');
172+
173173
await configure(
174174
verbose: verbose,
175175
jsonOutput: jsonOutput,

apps/cli/lib/src/commands/auth_command.dart renamed to apps/cli/lib/src/commands/auth/auth_command.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import 'package:celest_cli/src/commands/auth/login_command.dart';
22
import 'package:celest_cli/src/commands/auth/logout_command.dart';
3+
import 'package:celest_cli/src/commands/auth/token_command.dart';
34
import 'package:celest_cli/src/commands/celest_command.dart';
45

56
final class AuthCommand extends CelestCommand {
67
AuthCommand() {
78
addSubcommand(LoginCommand());
89
addSubcommand(LogoutCommand());
10+
addSubcommand(TokenCommand());
911
}
1012

1113
@override
File renamed without changes.

apps/cli/lib/src/commands/auth/cli_auth.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'package:celest_auth/celest_auth.dart';
21
import 'package:celest_auth/src/auth_impl.dart';
32
import 'package:celest_cli/src/context.dart' as ctx;
43
import 'package:celest_cli/src/context.dart';
@@ -29,7 +28,7 @@ final class _CliClient with CelestBase {
2928
NativeSecureStorage get nativeStorage => ctx.secureStorage;
3029
}
3130

32-
extension type CliAuth._(AuthImpl _hub) implements Auth {
31+
extension type CliAuth._(AuthImpl _hub) implements AuthImpl {
3332
CliAuth()
3433
: _hub = AuthImpl(
3534
_CliClient(),

apps/cli/lib/src/commands/auth/login_command.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:celest_auth/celest_auth.dart';
2+
import 'package:celest_cli/src/commands/auth/authenticate.dart';
23
import 'package:celest_cli/src/commands/auth/cli_auth.dart';
3-
import 'package:celest_cli/src/commands/authenticate.dart';
44
import 'package:celest_cli/src/commands/celest_command.dart';
55
import 'package:celest_cli/src/context.dart';
66

apps/cli/lib/src/commands/auth/logout_command.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import 'package:celest_cli/src/commands/auth/authenticate.dart';
12
import 'package:celest_cli/src/commands/auth/cli_auth.dart';
2-
import 'package:celest_cli/src/commands/authenticate.dart';
33
import 'package:celest_cli/src/commands/celest_command.dart';
44
import 'package:celest_cli/src/config/celest_config.dart';
55
import 'package:celest_cli/src/context.dart';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:celest_cli/src/commands/auth/authenticate.dart';
5+
import 'package:celest_cli/src/commands/auth/cli_auth.dart';
6+
import 'package:celest_cli/src/commands/celest_command.dart';
7+
import 'package:celest_cli/src/utils/error.dart';
8+
import 'package:celest_cli/src/utils/typeid.dart';
9+
import 'package:celest_core/_internal.dart';
10+
import 'package:corks_cedar/corks_cedar.dart';
11+
12+
final class TokenCommand extends CelestCommand with Authenticate {
13+
@override
14+
String get name => 'token';
15+
16+
@override
17+
String get description => 'Prints the access token for the current user.';
18+
19+
@override
20+
Future<int> run() async {
21+
await super.run();
22+
23+
await assertAuthenticated();
24+
final cork = await auth.secureStorage.read('cork');
25+
if (cork == null) {
26+
unreachable('No access token found for user.');
27+
}
28+
29+
if (jsonOutput) {
30+
final corkData = CedarCork.parse(cork);
31+
stdout.write(jsonEncode(corkData.toJson()));
32+
} else {
33+
stdout.write(cork);
34+
}
35+
36+
return 0;
37+
}
38+
}
39+
40+
extension on CedarCork {
41+
Map<String, Object?> toJson() => {
42+
'id': TypeId.fromUuid(Uuid(id), 'cork'),
43+
'issuer': issuer.toString(),
44+
'bearer': bearer.toString(),
45+
'audience': audience?.toString(),
46+
'claims': claims?.toJson(),
47+
'caveats': caveats.map((c) => c.toJson()).toList(),
48+
};
49+
}

apps/cli/lib/src/commands/celest_command.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ abstract base class CelestCommand extends Command<int> {
2424
late final String version;
2525

2626
/// Whether verbose logging is enabled.
27-
bool get verbose => globalResults?['verbose'] as bool? ?? false;
27+
bool get verbose => globalResults?.flag('verbose') ?? false;
28+
29+
/// Whether JSON output is enabled.
30+
bool get jsonOutput => globalResults?.flag('json') ?? false;
2831

2932
/// Resolves the latest version information from `pub.dev`.
3033
Future<PubVersionInfo?> resolveVersionInfo(String package) async {

0 commit comments

Comments
 (0)