@@ -13,12 +13,15 @@ import {
1313 isPerfAction ,
1414 isPerfArea ,
1515 isPerfKind ,
16+ isPerfSubject ,
1617 PERF_ACTION_ERROR_MESSAGE ,
1718 PERF_AREA_ERROR_MESSAGE ,
1819 PERF_KIND_ERROR_MESSAGE ,
20+ PERF_SUBJECT_ERROR_MESSAGE ,
1921 type PerfAction ,
2022 type PerfArea ,
2123 type PerfKind ,
24+ type PerfSubject ,
2225} from '../perf-command-contract.ts' ;
2326import {
2427 commonInputFromFlags ,
@@ -33,9 +36,11 @@ import type { CliReader, DaemonWriter } from './types.ts';
3336export const observabilityCliReaders = {
3437 perf : ( positionals , flags ) => ( {
3538 ...commonInputFromFlags ( flags ) ,
36- ...readPerfPositionals ( positionals ) ,
37- kind : readPerfKind ( flags . kind ) ,
38- out : flags . out ,
39+ ...readPerfPositionals ( positionals , {
40+ kind : readPerfKindFlag ( flags . kind ) ,
41+ template : flags . perfTemplate ,
42+ out : flags . out ,
43+ } ) ,
3944 } ) ,
4045 logs : ( positionals , flags ) => ( {
4146 ...commonInputFromFlags ( flags ) ,
@@ -78,26 +83,76 @@ export const observabilityDaemonWriters = {
7883
7984function perfPositionals ( input : PerfOptions ) : string [ ] {
8085 const area = input . area ?? ( input . action ? 'metrics' : undefined ) ;
86+ if ( area === 'cpu' ) {
87+ return nativePerfPositionals (
88+ [
89+ ...optionalString ( area ) ,
90+ ...optionalString ( input . subject ) ,
91+ ...optionalString ( input . action ) ,
92+ ...optionalString ( input . kind ) ,
93+ ] ,
94+ input ,
95+ ) ;
96+ }
97+ if ( area === 'trace' ) {
98+ return nativePerfPositionals (
99+ [ ...optionalString ( area ) , ...optionalString ( input . action ) , ...optionalString ( input . kind ) ] ,
100+ input ,
101+ ) ;
102+ }
81103 return [ ...optionalString ( area ) , ...optionalString ( input . action ) ] ;
82104}
83105
84- function readPerfPositionals ( positionals : string [ ] ) : Pick < PerfOptions , 'area' | 'action' > {
106+ function nativePerfPositionals ( base : string [ ] , input : PerfOptions ) : string [ ] {
107+ const positionals = [ ...base ] ;
108+ if ( input . template || input . out || input . tracePath ) {
109+ positionals . push ( input . template ?? '' ) ;
110+ }
111+ if ( input . out || input . tracePath ) {
112+ positionals . push ( input . out ?? '' ) ;
113+ }
114+ if ( input . tracePath ) {
115+ positionals . push ( input . tracePath ) ;
116+ }
117+ return positionals ;
118+ }
119+
120+ function readPerfPositionals (
121+ positionals : string [ ] ,
122+ flags : Pick < PerfOptions , 'kind' | 'template' | 'out' > = { } ,
123+ ) : Pick < PerfOptions , 'area' | 'subject' | 'action' | 'kind' | 'template' | 'out' > {
85124 if ( positionals [ 0 ] !== undefined && positionals [ 1 ] === undefined ) {
86125 const action = readPerfAction ( positionals [ 0 ] , { allowUndefined : true } ) ;
87- if ( action ) return { action } ;
126+ if ( action ) return { action, kind : readPerfKind ( flags . kind ) , out : flags . out } ;
127+ }
128+ const area = readPerfArea ( positionals [ 0 ] ) ;
129+ if ( area === 'cpu' ) {
130+ return {
131+ area,
132+ subject : readPerfSubject ( positionals [ 1 ] ) ,
133+ action : readPerfAction ( positionals [ 2 ] ) ,
134+ kind : readPerfKind ( flags . kind ) ,
135+ template : flags . template ,
136+ out : flags . out ,
137+ } ;
138+ }
139+ if ( area === 'trace' ) {
140+ return {
141+ area,
142+ action : readPerfAction ( positionals [ 1 ] ) ,
143+ kind : readPerfKind ( flags . kind ) ,
144+ template : flags . template ,
145+ out : flags . out ,
146+ } ;
88147 }
89148 return {
90- area : readPerfArea ( positionals [ 0 ] ) ,
149+ area,
91150 action : readPerfAction ( positionals [ 1 ] ) ,
151+ kind : readPerfKind ( flags . kind ) ,
152+ out : flags . out ,
92153 } ;
93154}
94155
95- function readPerfKind ( value : string | undefined ) : PerfKind | undefined {
96- if ( value === undefined ) return undefined ;
97- if ( isPerfKind ( value ) ) return value ;
98- throw new AppError ( 'INVALID_ARGS' , PERF_KIND_ERROR_MESSAGE ) ;
99- }
100-
101156function logsPositionals ( input : { action ?: string ; message ?: string } ) : string [ ] {
102157 return [ input . action ?? 'path' , ...optionalString ( input . message ) ] ;
103158}
@@ -133,6 +188,23 @@ function readPerfAction(
133188 throw new AppError ( 'INVALID_ARGS' , PERF_ACTION_ERROR_MESSAGE ) ;
134189}
135190
191+ function readPerfSubject ( value : string | undefined ) : PerfSubject {
192+ const normalized = value ?. toLowerCase ( ) ;
193+ if ( normalized !== undefined && isPerfSubject ( normalized ) ) return normalized ;
194+ throw new AppError ( 'INVALID_ARGS' , PERF_SUBJECT_ERROR_MESSAGE ) ;
195+ }
196+
197+ function readPerfKind ( value : string | undefined ) : PerfKind | undefined {
198+ if ( value === undefined ) return undefined ;
199+ const normalized = value . toLowerCase ( ) ;
200+ if ( isPerfKind ( normalized ) ) return normalized ;
201+ throw new AppError ( 'INVALID_ARGS' , PERF_KIND_ERROR_MESSAGE ) ;
202+ }
203+
204+ function readPerfKindFlag ( value : unknown ) : PerfKind | undefined {
205+ return typeof value === 'string' ? readPerfKind ( value ) : undefined ;
206+ }
207+
136208function readLogsAction ( value : string | undefined ) : LogAction | undefined {
137209 if ( value === undefined ) return undefined ;
138210 return parseStringMember ( LOG_ACTION_VALUES , value , {
0 commit comments