@@ -149,17 +149,9 @@ function resolveRemoteConnectFlags(flags: CliFlags): {
149149}
150150
151151export const disconnectCommand : ClientCommandHandler = async ( { flags, client } ) => {
152- const session = flags . session ?? 'default' ;
153- const stateDir = resolveDaemonPaths ( flags . stateDir ) . baseDir ;
154- const state =
155- readRemoteConnectionState ( { stateDir, session } ) ??
156- ( flags . session ? null : readActiveConnectionState ( { stateDir } ) ) ;
152+ const { session, stateDir, state } = readRequestedConnectionState ( flags ) ;
157153 if ( ! state ) {
158- writeCommandOutput (
159- flags ,
160- { connected : false , session } ,
161- ( ) => `No remote connection for "${ session } ".` ,
162- ) ;
154+ writeNoRemoteConnectionOutput ( flags , session ) ;
163155 return true ;
164156 }
165157 const connectedSession = state . session ;
@@ -197,17 +189,9 @@ export const connectionCommand: ClientCommandHandler = async ({ positionals, fla
197189 if ( positionals [ 0 ] !== 'status' ) {
198190 throw new AppError ( 'INVALID_ARGS' , 'connection accepts only: status' ) ;
199191 }
200- const session = flags . session ?? 'default' ;
201- const stateDir = resolveDaemonPaths ( flags . stateDir ) . baseDir ;
202- const state =
203- readRemoteConnectionState ( { stateDir, session } ) ??
204- ( flags . session ? null : readActiveConnectionState ( { stateDir } ) ) ;
192+ const { session, state } = readRequestedConnectionState ( flags ) ;
205193 if ( ! state ) {
206- writeCommandOutput (
207- flags ,
208- { connected : false , session } ,
209- ( ) => `No remote connection for "${ session } ".` ,
210- ) ;
194+ writeNoRemoteConnectionOutput ( flags , session ) ;
211195 return true ;
212196 }
213197 const leasePreparation = buildLeasePreparationNotice ( state ) ;
@@ -237,6 +221,30 @@ function createRemoteSessionName(stateDir: string): string {
237221 return `adc-${ Date . now ( ) . toString ( 36 ) } -${ crypto . randomBytes ( 2 ) . toString ( 'hex' ) } ` ;
238222}
239223
224+ function readRequestedConnectionState ( flags : CliFlags ) : {
225+ session : string ;
226+ stateDir : string ;
227+ state : RemoteConnectionState | null ;
228+ } {
229+ const session = flags . session ?? 'default' ;
230+ const stateDir = resolveDaemonPaths ( flags . stateDir ) . baseDir ;
231+ return {
232+ session,
233+ stateDir,
234+ state :
235+ readRemoteConnectionState ( { stateDir, session } ) ??
236+ ( flags . session ? null : readActiveConnectionState ( { stateDir } ) ) ,
237+ } ;
238+ }
239+
240+ function writeNoRemoteConnectionOutput ( flags : CliFlags , session : string ) : void {
241+ writeCommandOutput (
242+ flags ,
243+ { connected : false , session } ,
244+ ( ) => `No remote connection for "${ session } ".` ,
245+ ) ;
246+ }
247+
240248function isCompatibleConnection (
241249 state : RemoteConnectionState ,
242250 options : {
0 commit comments