@chalksurf/cli 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,174 +0,0 @@
1
- import { createApiClient } from '../lib/api-client.js';
2
- import { CliCommandError } from '../lib/cli-error.js';
3
- import { resolveOrganization } from '../lib/config-store.js';
4
- import { formatUserIdentity, mapApiErrorToCliError, requireResolvedBaseUrl, requireResolvedToken, } from '../lib/session.js';
5
- const isInteractiveStdin = (stdin) => {
6
- return stdin.isTTY === true;
7
- };
8
- const readTokenFromStdin = async (stdin) => {
9
- let token = '';
10
- for await (const chunk of stdin) {
11
- token += typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8');
12
- }
13
- return token.trim();
14
- };
15
- const serializeOrganization = (organization) => {
16
- if (!organization) {
17
- return null;
18
- }
19
- return {
20
- id: organization.organizationId,
21
- name: organization.organizationName,
22
- role: organization.role,
23
- type: organization.organizationType,
24
- };
25
- };
26
- const formatOrganizationSummary = (organization) => {
27
- if (!organization) {
28
- return 'No default organization selected.';
29
- }
30
- return `Default organization: ${organization.name} (${organization.id}).`;
31
- };
32
- export const registerAuthCommands = (authYargs, context) => {
33
- return authYargs
34
- .command('login', 'Read a CLI token from stdin or prompt for it interactively', (loginYargs) => {
35
- return loginYargs
36
- .option('with-token', {
37
- type: 'boolean',
38
- default: false,
39
- describe: 'Read the token from stdin when piping input',
40
- })
41
- .example('chalksurf auth login --profile prod-cztamas --base-url https://chalksurf-api.fly.dev', 'Prompt for a CLI token and store it in the prod-cztamas profile')
42
- .example(`printf '%s' "$CHALKSURF_TOKEN" | chalksurf auth login --profile prod-codex --base-url https://chalksurf-api.fly.dev`, 'Read a CLI token from stdin into an explicit profile');
43
- }, async (argv) => {
44
- const config = await context.configStore.loadProfile({ profileName: argv.profile });
45
- const resolvedBaseUrl = requireResolvedBaseUrl({
46
- flagValue: argv.baseUrl,
47
- env: context.env,
48
- config,
49
- });
50
- const token = (isInteractiveStdin(context.stdin)
51
- ? await context.promptSecret('CLI token: ')
52
- : await readTokenFromStdin(context.stdin)).trim();
53
- if (!token) {
54
- throw new CliCommandError('No token was provided.', 2);
55
- }
56
- const apiClient = createApiClient({
57
- baseUrl: resolvedBaseUrl.value,
58
- token,
59
- });
60
- let profile;
61
- try {
62
- profile = await apiClient.getUserProfile();
63
- }
64
- catch (error) {
65
- throw mapApiErrorToCliError(error);
66
- }
67
- const resolvedOrganization = resolveOrganization({
68
- flagValue: argv.organization,
69
- env: context.env,
70
- config,
71
- profile,
72
- });
73
- await context.configStore.update({ profileName: argv.profile }, (currentConfig) => ({
74
- ...currentConfig,
75
- token,
76
- baseUrl: resolvedBaseUrl.value,
77
- organizationId: resolvedOrganization.organization?.organizationId,
78
- }));
79
- resolvedOrganization.warnings.forEach((warning) => {
80
- context.output.info(warning);
81
- });
82
- context.output.print({
83
- authenticated: true,
84
- baseUrl: resolvedBaseUrl.value,
85
- baseUrlSource: resolvedBaseUrl.source,
86
- organization: serializeOrganization(resolvedOrganization.organization),
87
- profile: config.profileName,
88
- profileSource: config.profileSource,
89
- user: {
90
- email: profile.email,
91
- id: profile.id,
92
- name: profile.name,
93
- },
94
- }, (result) => `Logged in as ${formatUserIdentity(result.user)}.\n${formatOrganizationSummary(result.organization)}`, {
95
- command: 'auth login',
96
- });
97
- })
98
- .command('status', 'Show the current authentication and organization state', (statusYargs) => statusYargs
99
- .example('chalksurf auth status', 'Show the current authenticated user and default organization')
100
- .example('chalksurf --profile prod-codex auth status --json', 'Inspect a specific profile in machine-readable mode')
101
- .epilogue([
102
- 'Resolution order:',
103
- ' profile: --profile, then CHALKSURF_PROFILE, then stored default profile',
104
- ' base URL: --base-url, then CHALKSURF_BASE_URL, then active profile',
105
- ' token: CHALKSURF_TOKEN, then active profile',
106
- ' organization: --organization, then CHALKSURF_ORGANIZATION_ID, then active profile',
107
- ].join('\n')), async (argv) => {
108
- const config = await context.configStore.loadProfile({ profileName: argv.profile });
109
- const resolvedBaseUrl = requireResolvedBaseUrl({
110
- flagValue: argv.baseUrl,
111
- env: context.env,
112
- config,
113
- });
114
- const resolvedToken = requireResolvedToken({
115
- env: context.env,
116
- config,
117
- });
118
- const apiClient = createApiClient({
119
- baseUrl: resolvedBaseUrl.value,
120
- token: resolvedToken.value,
121
- });
122
- let profile;
123
- try {
124
- profile = await apiClient.getUserProfile();
125
- }
126
- catch (error) {
127
- throw mapApiErrorToCliError(error);
128
- }
129
- const resolvedOrganization = resolveOrganization({
130
- flagValue: argv.organization,
131
- env: context.env,
132
- config,
133
- profile,
134
- });
135
- resolvedOrganization.warnings.forEach((warning) => {
136
- context.output.info(warning);
137
- });
138
- context.output.print({
139
- authenticated: true,
140
- baseUrl: resolvedBaseUrl.value,
141
- baseUrlSource: resolvedBaseUrl.source,
142
- organization: serializeOrganization(resolvedOrganization.organization),
143
- organizationSource: resolvedOrganization.source,
144
- profile: config.profileName,
145
- profileSource: config.profileSource,
146
- tokenSource: resolvedToken.source,
147
- user: {
148
- email: profile.email,
149
- id: profile.id,
150
- name: profile.name,
151
- },
152
- }, (result) => `Authenticated as ${formatUserIdentity(result.user)}.\n${formatOrganizationSummary(result.organization)}`, {
153
- command: 'auth status',
154
- });
155
- })
156
- .command('logout', 'Remove the locally stored CLI token', (logoutYargs) => logoutYargs.example('chalksurf auth logout', 'Remove the stored CLI token from local config'), async (argv) => {
157
- const config = await context.configStore.loadProfile({ profileName: argv.profile });
158
- const hadStoredToken = Boolean(config.token);
159
- await context.configStore.update({ profileName: argv.profile }, (currentConfig) => ({
160
- ...currentConfig,
161
- token: undefined,
162
- }));
163
- context.output.print({
164
- clearedStoredToken: hadStoredToken,
165
- loggedOut: true,
166
- profile: config.profileName,
167
- profileSource: config.profileSource,
168
- }, hadStoredToken ? 'Stored CLI token removed.' : 'No stored CLI token was present.', {
169
- command: 'auth logout',
170
- });
171
- })
172
- .demandCommand(1)
173
- .strict();
174
- };