Skip to content

Commit 99469c4

Browse files
konardclaude
andcommitted
refactor(shell): extract helpers to satisfy max-lines-per-function lint rule
Extract appendKeyIfMissing and resolveAuthorizedKeysSource into separate functions so that ensureAuthorizedKeys stays within the 50-line limit. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2ad27a4 commit 99469c4

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

packages/lib/src/usecases/actions/prepare-files.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,45 @@ const ensureFileReady = (
4646
return "exists"
4747
})
4848

49+
const appendKeyIfMissing = (
50+
fs: FileSystem.FileSystem,
51+
resolved: string,
52+
source: string,
53+
desiredContents: string
54+
): Effect.Effect<void, PlatformError> =>
55+
Effect.gen(function*(_) {
56+
const currentContents = yield* _(fs.readFileString(resolved))
57+
const currentLines = currentContents
58+
.split(/\r?\n/)
59+
.map((line) => line.trim())
60+
.filter((line) => line.length > 0)
61+
62+
if (currentLines.includes(desiredContents)) {
63+
return
64+
}
65+
66+
const normalizedCurrent = currentContents.trimEnd()
67+
const nextContents = normalizedCurrent.length === 0
68+
? `${desiredContents}\n`
69+
: `${normalizedCurrent}\n${desiredContents}\n`
70+
71+
yield* _(fs.writeFileString(resolved, nextContents))
72+
yield* _(Effect.log(`Authorized keys appended from ${source} to ${resolved}`))
73+
})
74+
75+
const resolveAuthorizedKeysSource = (
76+
fs: FileSystem.FileSystem,
77+
path: Path.Path,
78+
cwd: string
79+
): Effect.Effect<string | null, PlatformError, FileSystem.FileSystem | Path.Path> =>
80+
Effect.gen(function*(_) {
81+
const sshPrivateKey = yield* _(findSshPrivateKey(fs, path, cwd))
82+
const matchingPublicKey = sshPrivateKey === null ? null : yield* _(findExistingPath(fs, `${sshPrivateKey}.pub`))
83+
return matchingPublicKey === null
84+
? yield* _(findAuthorizedKeysSource(fs, path, cwd))
85+
: matchingPublicKey
86+
})
87+
4988
const ensureAuthorizedKeys = (
5089
baseDir: string,
5190
authorizedKeysPath: string
@@ -63,14 +102,7 @@ const ensureAuthorizedKeys = (
63102
)
64103
)
65104

66-
const sshPrivateKey = yield* _(findSshPrivateKey(fs, path, process.cwd()))
67-
const matchingPublicKey =
68-
sshPrivateKey === null ? null : yield* _(findExistingPath(fs, `${sshPrivateKey}.pub`))
69-
const source = yield* _(
70-
matchingPublicKey === null
71-
? findAuthorizedKeysSource(fs, path, process.cwd())
72-
: Effect.succeed(matchingPublicKey)
73-
)
105+
const source = yield* _(resolveAuthorizedKeysSource(fs, path, process.cwd()))
74106
if (source === null) {
75107
yield* _(
76108
Effect.logError(
@@ -87,28 +119,9 @@ const ensureAuthorizedKeys = (
87119
}
88120

89121
if (state === "exists") {
90-
if (resolved !== managedDefaultAuthorizedKeys) {
91-
return
122+
if (resolved === managedDefaultAuthorizedKeys) {
123+
yield* _(appendKeyIfMissing(fs, resolved, source, desiredContents))
92124
}
93-
94-
const currentContents = yield* _(fs.readFileString(resolved))
95-
const currentLines = currentContents
96-
.split(/\r?\n/)
97-
.map((line) => line.trim())
98-
.filter((line) => line.length > 0)
99-
100-
if (currentLines.includes(desiredContents)) {
101-
return
102-
}
103-
104-
const normalizedCurrent = currentContents.trimEnd()
105-
const nextContents =
106-
normalizedCurrent.length === 0
107-
? `${desiredContents}\n`
108-
: `${normalizedCurrent}\n${desiredContents}\n`
109-
110-
yield* _(fs.writeFileString(resolved, nextContents))
111-
yield* _(Effect.log(`Authorized keys appended from ${source} to ${resolved}`))
112125
return
113126
}
114127

0 commit comments

Comments
 (0)