@@ -5,7 +5,12 @@ import { Effect } from "effect"
55import { asObject , asString , type JsonValue } from "./api-json.js"
66import { defaultTemplateConfig } from "./frontend-lib/core/domain.js"
77import type { CreateCommand } from "./frontend-lib/core/domain.js"
8- import { resolvePathFromCwd } from "./frontend-lib/usecases/path-helpers.js"
8+ import {
9+ findAuthorizedKeysSource ,
10+ findExistingPath ,
11+ findSshPrivateKey ,
12+ resolvePathFromCwd
13+ } from "./frontend-lib/usecases/path-helpers.js"
914
1015export const readProjectOutput = ( payload : JsonValue ) : string => {
1116 const object = asObject ( payload )
@@ -37,6 +42,30 @@ const resolveClientCreatePath = (
3742
3843const missingAuthorizedKeysContents = ( ) : string | undefined => undefined
3944
45+ const normalizeAuthorizedKeysContents = ( value : string ) : string | undefined => {
46+ const trimmed = value . trim ( )
47+ return trimmed . length === 0 ? undefined : `${ trimmed } \n`
48+ }
49+
50+ const resolveManagedAuthorizedKeysContents = ( ) =>
51+ Effect . gen ( function * ( _ ) {
52+ const fs = yield * _ ( FileSystem . FileSystem )
53+ const path = yield * _ ( Path . Path )
54+ const cwd = process . cwd ( )
55+ const sshPrivateKey = yield * _ ( findSshPrivateKey ( fs , path , cwd ) )
56+ const matchingPublicKey = sshPrivateKey === null ? null : yield * _ ( findExistingPath ( fs , `${ sshPrivateKey } .pub` ) )
57+ const source = matchingPublicKey === null
58+ ? yield * _ ( findAuthorizedKeysSource ( fs , path , cwd ) )
59+ : matchingPublicKey
60+
61+ if ( source === null ) {
62+ return missingAuthorizedKeysContents ( )
63+ }
64+
65+ const contents = yield * _ ( fs . readFileString ( source ) )
66+ return normalizeAuthorizedKeysContents ( contents )
67+ } )
68+
4069export const resolveCreateRequestPaths = ( command : CreateCommand ) =>
4170 Effect . gen ( function * ( _ ) {
4271 const fs = yield * _ ( FileSystem . FileSystem )
@@ -46,11 +75,15 @@ export const resolveCreateRequestPaths = (command: CreateCommand) =>
4675 ? command . config . authorizedKeysPath
4776 : resolveClientCreatePath ( path , cwd , command . config . authorizedKeysPath )
4877 const authorizedKeysContents = authorizedKeysPath === defaultTemplateConfig . authorizedKeysPath
49- ? undefined
78+ ? yield * _ ( resolveManagedAuthorizedKeysContents ( ) )
5079 : yield * _ (
5180 fs . exists ( authorizedKeysPath ) . pipe (
5281 Effect . flatMap ( ( exists ) =>
53- exists ? fs . readFileString ( authorizedKeysPath ) : Effect . sync ( missingAuthorizedKeysContents )
82+ exists
83+ ? fs . readFileString ( authorizedKeysPath ) . pipe (
84+ Effect . map ( ( contents ) => normalizeAuthorizedKeysContents ( contents ) )
85+ )
86+ : Effect . sync ( missingAuthorizedKeysContents )
5487 )
5588 )
5689 )
0 commit comments