@@ -2,10 +2,9 @@ import chalk from 'chalk';
22import { Command } from 'commander' ;
33import { CanvasListOptions , CanvasReadOptions } from '../types/commands' ;
44import { CanvasFile , CanvasSection , CanvasSectionElement } from '../types/slack' ;
5- import { createSlackClient } from '../utils/client-factory ' ;
5+ import { renderByFormat , withSlackClient } from '../utils/command-support ' ;
66import { wrapCommand } from '../utils/command-wrapper' ;
7- import { parseFormat , parseProfile } from '../utils/option-parsers' ;
8- import { sanitizeTerminalData , sanitizeTerminalText } from '../utils/terminal-sanitizer' ;
7+ import { sanitizeTerminalText } from '../utils/terminal-sanitizer' ;
98import { createValidationHook , optionValidators } from '../utils/validators' ;
109
1110function extractText ( elements : CanvasSectionElement [ ] ) : string {
@@ -18,21 +17,7 @@ function extractText(elements: CanvasSectionElement[]): string {
1817 . join ( '' ) ;
1918}
2019
21- function formatSections ( sections : CanvasSection [ ] , format : string ) : void {
22- if ( format === 'json' ) {
23- console . log ( JSON . stringify ( sanitizeTerminalData ( sections ) ) ) ;
24- return ;
25- }
26-
27- if ( format === 'simple' ) {
28- sections . forEach ( ( section ) => {
29- const text = section . elements ? extractText ( section . elements ) : '' ;
30- console . log ( `${ sanitizeTerminalText ( section . id || '(no id)' ) } \t${ text || '(no content)' } ` ) ;
31- } ) ;
32- return ;
33- }
34-
35- // table format (default)
20+ function formatSectionsTable ( sections : CanvasSection [ ] ) : void {
3621 sections . forEach ( ( section ) => {
3722 const text = section . elements ? extractText ( section . elements ) : '' ;
3823 console . log (
@@ -42,22 +27,14 @@ function formatSections(sections: CanvasSection[], format: string): void {
4227 } ) ;
4328}
4429
45- function formatCanvases ( canvases : CanvasFile [ ] , format : string ) : void {
46- if ( format === 'json' ) {
47- console . log ( JSON . stringify ( sanitizeTerminalData ( canvases ) ) ) ;
48- return ;
49- }
50-
51- if ( format === 'simple' ) {
52- canvases . forEach ( ( canvas ) => {
53- console . log (
54- `${ sanitizeTerminalText ( canvas . id || '(no id)' ) } \t${ sanitizeTerminalText ( canvas . name || '(no name)' ) } `
55- ) ;
56- } ) ;
57- return ;
58- }
30+ function formatSectionsSimple ( sections : CanvasSection [ ] ) : void {
31+ sections . forEach ( ( section ) => {
32+ const text = section . elements ? extractText ( section . elements ) : '' ;
33+ console . log ( `${ sanitizeTerminalText ( section . id || '(no id)' ) } \t${ text || '(no content)' } ` ) ;
34+ } ) ;
35+ }
5936
60- // table format (default)
37+ function formatCanvasesTable ( canvases : CanvasFile [ ] ) : void {
6138 canvases . forEach ( ( canvas ) => {
6239 console . log (
6340 chalk . cyan ( `ID: ${ sanitizeTerminalText ( canvas . id || '(no id)' ) } ` ) +
@@ -66,6 +43,14 @@ function formatCanvases(canvases: CanvasFile[], format: string): void {
6643 } ) ;
6744}
6845
46+ function formatCanvasesSimple ( canvases : CanvasFile [ ] ) : void {
47+ canvases . forEach ( ( canvas ) => {
48+ console . log (
49+ `${ sanitizeTerminalText ( canvas . id || '(no id)' ) } \t${ sanitizeTerminalText ( canvas . name || '(no name)' ) } `
50+ ) ;
51+ } ) ;
52+ }
53+
6954export function setupCanvasCommand ( ) : Command {
7055 const canvasCommand = new Command ( 'canvas' ) . description ( 'Manage Slack Canvases' ) ;
7156
@@ -77,18 +62,19 @@ export function setupCanvasCommand(): Command {
7762 . hook ( 'preAction' , createValidationHook ( [ optionValidators . format ] ) )
7863 . action (
7964 wrapCommand ( async ( options : CanvasReadOptions ) => {
80- const profile = parseProfile ( options . profile ) ;
81- const client = await createSlackClient ( profile ) ;
82-
83- const sections = await client . readCanvas ( options . id ) ;
84-
85- if ( sections . length === 0 ) {
86- console . log ( 'No sections found in canvas' ) ;
87- return ;
88- }
89-
90- const format = parseFormat ( options . format ) ;
91- formatSections ( sections , format ) ;
65+ await withSlackClient ( options , async ( client ) => {
66+ const sections = await client . readCanvas ( options . id ) ;
67+
68+ if ( sections . length === 0 ) {
69+ console . log ( 'No sections found in canvas' ) ;
70+ return ;
71+ }
72+
73+ renderByFormat ( options , sections , {
74+ table : formatSectionsTable ,
75+ simple : formatSectionsSimple ,
76+ } ) ;
77+ } ) ;
9278 } )
9379 ) ;
9480
@@ -100,18 +86,19 @@ export function setupCanvasCommand(): Command {
10086 . hook ( 'preAction' , createValidationHook ( [ optionValidators . format ] ) )
10187 . action (
10288 wrapCommand ( async ( options : CanvasListOptions ) => {
103- const profile = parseProfile ( options . profile ) ;
104- const client = await createSlackClient ( profile ) ;
105-
106- const canvases = await client . listCanvases ( options . channel ) ;
107-
108- if ( canvases . length === 0 ) {
109- console . log ( 'No canvases found in channel' ) ;
110- return ;
111- }
112-
113- const format = parseFormat ( options . format ) ;
114- formatCanvases ( canvases , format ) ;
89+ await withSlackClient ( options , async ( client ) => {
90+ const canvases = await client . listCanvases ( options . channel ) ;
91+
92+ if ( canvases . length === 0 ) {
93+ console . log ( 'No canvases found in channel' ) ;
94+ return ;
95+ }
96+
97+ renderByFormat ( options , canvases , {
98+ table : formatCanvasesTable ,
99+ simple : formatCanvasesSimple ,
100+ } ) ;
101+ } ) ;
115102 } )
116103 ) ;
117104
0 commit comments