Files
coder/agent/usershell/usershell_other.go
Cian Johnston 172e52317c feat(agent): wire up agentssh server to allow exec into container (#16638)
Builds on top of https://github.com/coder/coder/pull/16623/ and wires up
the ReconnectingPTY server. This does nothing to wire up the web
terminal yet but the added test demonstrates the functionality working.

Other changes:
* Refactors and moves the `SystemEnvInfo` interface to the
`agent/usershell` package to address follow-up from
https://github.com/coder/coder/pull/16623#discussion_r1967580249
* Marks `usershellinfo.Get` as deprecated. Consumers should use the
`EnvInfoer` interface instead.

---------

Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
Co-authored-by: Danny Kopping <danny@coder.com>
2025-02-26 09:03:27 +00:00

36 lines
858 B
Go

//go:build !windows && !darwin
// +build !windows,!darwin
package usershell
import (
"os"
"strings"
"golang.org/x/xerrors"
)
// Get returns the /etc/passwd entry for the username provided.
// Deprecated: use SystemEnvInfo.UserShell instead.
func Get(username string) (string, error) {
contents, err := os.ReadFile("/etc/passwd")
if err != nil {
return "", xerrors.Errorf("read /etc/passwd: %w", err)
}
lines := strings.Split(string(contents), "\n")
for _, line := range lines {
if !strings.HasPrefix(line, username+":") {
continue
}
parts := strings.Split(line, ":")
if len(parts) < 7 {
return "", xerrors.Errorf("malformed user entry: %q", line)
}
return parts[6], nil
}
if s := os.Getenv("SHELL"); s != "" {
return s, nil
}
return "", xerrors.Errorf("shell for user %q not found in /etc/passwd or $SHELL", username)
}