Skip to content

Commit 0e3f28f

Browse files
Copilotnitrocode
andcommitted
refactor: replace CutPrefix dscl parsing with compiled regex dsclNFSHomeDirRe
Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com> Agent-Logs-Url: https://github.com/cloudposse/atmos/sessions/5fe42198-8bf2-42d9-bfa3-dd6295923b14
1 parent 7f2b233 commit 0e3f28f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pkg/config/homedir/homedir.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ var (
7474
// preventing shell injection when the username is interpolated into sh -c.
7575
usernameRe = regexp.MustCompile(`^[A-Za-z0-9._][A-Za-z0-9._-]*$`)
7676

77+
// dsclNFSHomeDirRe matches a dscl NFSHomeDirectory output line tolerating
78+
// any amount of leading whitespace before the key and any whitespace
79+
// after the colon. It captures the first non-whitespace token which
80+
// is always the home directory path in well-formed dscl output.
81+
dsclNFSHomeDirRe = regexp.MustCompile(`^\s*NFSHomeDirectory:\s*(\S+)`)
82+
7783
// externalCmdTimeout is the maximum time allowed for external subprocess
7884
// calls (id, dscl, sh). A conservative default prevents Dir()/Expand()
7985
// from hanging indefinitely on slow NSS/LDAP/directory backends.
@@ -460,18 +466,17 @@ func getDarwinHomeDir(cachedUsername string) (string, error) {
460466
}
461467

462468
// parseDsclNFSHomeDir extracts the home directory from dscl output.
463-
// It accepts leading/trailing whitespace around the key and multiple spaces
464-
// after the colon (defensive against dscl output variants). The returned path
465-
// is filepath.Clean'd. Returns ErrBlankOutput when the key is absent or the
469+
// It uses a compiled regex to tolerate any amount of leading whitespace before
470+
// the key and multiple spaces/tabs after the colon. The returned path is
471+
// filepath.Clean'd. Returns ErrBlankOutput when the key is absent or the
466472
// parsed value is empty, ".", or a non-absolute path (malformed record).
467473
func parseDsclNFSHomeDir(output string) (string, error) {
468474
for _, line := range strings.Split(output, "\n") {
469-
trimmed := strings.TrimSpace(line)
470-
after, ok := strings.CutPrefix(trimmed, "NFSHomeDirectory:")
471-
if !ok {
475+
m := dsclNFSHomeDirRe.FindStringSubmatch(line)
476+
if m == nil {
472477
continue
473478
}
474-
home := filepath.Clean(strings.TrimSpace(after))
479+
home := filepath.Clean(m[1])
475480
if home == "" || home == "." {
476481
continue
477482
}

0 commit comments

Comments
 (0)