1
1
import { Command } from 'commander' ;
2
- import { getSlackAuth } from '../auth ' ;
2
+ import { findWorkspaceToken } from '../slack-api ' ;
3
3
import { CommandContext } from '../context' ;
4
+ import { getStoredAuth } from '../keychain.js' ;
5
+ import { getCookie } from '../cookies.js' ;
6
+ import { getTokens } from '../tokens.js' ;
4
7
5
8
export function registerPrintCommand ( program : Command , context : CommandContext ) : void {
6
9
program
@@ -13,45 +16,80 @@ export function registerPrintCommand(program: Command, context: CommandContext):
13
16
console . log ( 'Getting Slack credentials...' ) ;
14
17
}
15
18
16
- // For print command, we want to work even without a workspace selected
17
- // But if a workspace is set, we'll use it to filter results
18
- const auth = await getSlackAuth ( {
19
- workspace : context . hasWorkspace ? context . workspace : undefined ,
20
- quiet : cmdOptions . quiet ,
21
- } ) ;
22
-
23
- if ( Object . keys ( auth . tokens ) . length === 0 ) {
24
- console . error ( 'Error: No tokens found.' ) ;
25
- if ( context . hasWorkspace ) {
26
- console . error ( `No workspace matching "${ context . workspace } " was found.` ) ;
27
- }
28
- process . exit ( 1 ) ;
29
- }
19
+ // If no workspace is specified, we'll default to one to get credentials
20
+ const defaultWorkspace = context . hasWorkspace ? context . workspace : 'default' ;
30
21
31
- if ( ! cmdOptions . quiet ) {
32
- console . log ( '\nFound tokens for workspaces:\n' ) ;
22
+ // Get the auth object first
23
+ const storedAuth = await getStoredAuth ( ) ;
24
+ if ( ! cmdOptions . quiet && ! storedAuth ) {
25
+ console . log ( 'No stored auth found, fetching fresh credentials...' ) ;
33
26
}
34
27
35
- for ( const [ url , details ] of Object . entries ( auth . tokens ) ) {
36
- if ( cmdOptions . quiet ) {
37
- console . log ( `${ details . token } ` ) ;
28
+ const auth = storedAuth || {
29
+ cookie : await getCookie ( ) ,
30
+ tokens : await getTokens ( context ) ,
31
+ } ;
32
+
33
+ try {
34
+ const { token, cookie, workspaceUrl } = findWorkspaceToken (
35
+ auth ,
36
+ defaultWorkspace ,
37
+ context ,
38
+ ) ;
39
+
40
+ if ( ! cmdOptions . quiet ) {
41
+ console . log ( '\nFound token for workspace:\n' ) ;
42
+ console . log ( `Workspace URL: ${ workspaceUrl } ` ) ;
43
+ console . log ( `Token: ${ token } \n` ) ;
44
+
45
+ console . log ( 'Found cookie:' ) ;
46
+ console . log ( `${ cookie . name } : ${ cookie . value } \n` ) ;
38
47
} else {
39
- console . log ( ` ${ details . name } ( ${ url } )` ) ;
40
- console . log ( `Token: ${ details . token } \n` ) ;
48
+ console . log ( token ) ;
49
+ console . log ( cookie . value ) ;
41
50
}
42
- }
51
+ } catch ( error ) {
52
+ // If the specified workspace isn't found and we're using a default,
53
+ // we'll just use any available token
54
+ if ( ! context . hasWorkspace ) {
55
+ try {
56
+ // Try with an empty string to get any available workspace
57
+ const firstWorkspaceKey = Object . keys ( auth . tokens ) [ 0 ] ;
58
+
59
+ if ( ! firstWorkspaceKey ) {
60
+ throw new Error ( 'No workspaces available' ) ;
61
+ }
62
+
63
+ const { token, cookie, workspaceUrl } = findWorkspaceToken (
64
+ auth ,
65
+ firstWorkspaceKey , // Use the first workspace key instead of empty string
66
+ context ,
67
+ ) ;
43
68
44
- if ( cmdOptions . quiet ) {
45
- console . log ( `${ auth . cookie . value } ` ) ;
46
- } else {
47
- console . log ( 'Found cookie:' ) ;
48
- console . log ( `${ auth . cookie . name } : ${ auth . cookie . value } \n` ) ;
49
-
50
- // Print guidance about workspace selection if we showed multiple workspaces
51
- if ( Object . keys ( auth . tokens ) . length > 1 && ! context . hasWorkspace ) {
52
- console . log ( '\nTip: To filter results for a specific workspace, use one of:' ) ;
53
- console . log ( ' - Use -w, --workspace <workspace> to specify a workspace directly' ) ;
54
- console . log ( ' - Use -l, --last-workspace to use your most recently used workspace' ) ;
69
+ if ( ! cmdOptions . quiet ) {
70
+ console . log ( '\nFound token for workspace:\n' ) ;
71
+ console . log ( `Workspace URL: ${ workspaceUrl } ` ) ;
72
+ console . log ( `Token: ${ token } \n` ) ;
73
+
74
+ console . log ( 'Found cookie:' ) ;
75
+ console . log ( `${ cookie . name } : ${ cookie . value } \n` ) ;
76
+
77
+ console . log ( '\nTip: To specify a workspace directly, use:' ) ;
78
+ console . log ( ' - Use -w, --workspace <workspace> to specify a workspace' ) ;
79
+ console . log (
80
+ ' - Use -l, --last-workspace to use your most recently used workspace' ,
81
+ ) ;
82
+ } else {
83
+ console . log ( token ) ;
84
+ console . log ( cookie . value ) ;
85
+ }
86
+ } catch ( innerError ) {
87
+ console . error ( 'Error getting any workspace token:' , innerError ) ;
88
+ process . exit ( 1 ) ;
89
+ }
90
+ } else {
91
+ console . error ( `Error getting workspace "${ context . workspace } ":` , error ) ;
92
+ process . exit ( 1 ) ;
55
93
}
56
94
}
57
95
} catch ( error ) {
0 commit comments