Skip to content

Commit 67b7e54

Browse files
authored
refactor: Use build tags and optimize Darwin implementation
2 parents 85d2ad3 + c28ef2b commit 67b7e54

4 files changed

Lines changed: 74 additions & 66 deletions

File tree

internal/ptysession/shell.go

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build darwin
2+
3+
package ptysession
4+
5+
import (
6+
"fmt"
7+
"os/exec"
8+
"strings"
9+
)
10+
11+
func getUserShell(username string) (string, error) {
12+
cmd := exec.Command("dscl", ".", "-read", fmt.Sprintf("/Users/%s", username), "UserShell")
13+
output, err := cmd.Output()
14+
if err != nil {
15+
return "", fmt.Errorf("failed to get user shell on macOS: %v", err)
16+
}
17+
18+
// Parse the output
19+
for line := range strings.SplitSeq(string(output), "\n") {
20+
if strings.HasPrefix(line, "UserShell:") {
21+
fields := strings.Fields(line)
22+
if len(fields) >= 2 {
23+
return fields[1], nil
24+
}
25+
}
26+
}
27+
return "", fmt.Errorf("shell not found in dscl output")
28+
}

internal/ptysession/shell_linux.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build linux
2+
3+
package ptysession
4+
5+
import (
6+
"bufio"
7+
"fmt"
8+
"os"
9+
"strings"
10+
)
11+
12+
func getUserShell(username string) (string, error) {
13+
file, err := os.Open("/etc/passwd")
14+
if err == nil {
15+
defer file.Close()
16+
scanner := bufio.NewScanner(file)
17+
for scanner.Scan() {
18+
line := scanner.Text()
19+
fields := strings.Split(line, ":")
20+
if len(fields) < 7 {
21+
continue
22+
}
23+
if fields[0] == username {
24+
shell := fields[6]
25+
return shell, nil
26+
}
27+
}
28+
if err := scanner.Err(); err != nil {
29+
return "", err
30+
}
31+
}
32+
33+
return "", fmt.Errorf("user %s not found in /etc/passwd: %w", username, err)
34+
}

internal/ptysession/shell_other.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux && !darwin
2+
3+
package ptysession
4+
5+
import (
6+
"fmt"
7+
"runtime"
8+
)
9+
10+
func getUserShell(username string) (string, error) {
11+
return "", fmt.Errorf("operating system %s not supported", runtime.GOOS)
12+
}

0 commit comments

Comments
 (0)