From 5489a70805d7dc6e1c166cb01ae0d32db3514cff Mon Sep 17 00:00:00 2001 From: Gerard Casas Saez Date: Tue, 16 Apr 2024 13:28:56 -0600 Subject: [PATCH 1/4] read from shell environment variable if exists --- shell/shell.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shell/shell.go b/shell/shell.go index c40ea2ab..7d3413f9 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -94,7 +94,13 @@ func Resolve(name string) (Shell, error) { // Detect the user's shell. func Detect() (Shell, error) { - // First look for shell in parent processes. + // Check for SHELL environment variable. + shell, ok := shells[filepath.Base(os.Getenv("SHELL"))] + if ok { + return shell, nil + } + + // Then, look for shell in parent processes. pid := os.Getppid() for { process, err := ps.FindProcess(pid) From a46018c432e1810d4cf7fecf109351604749fc00 Mon Sep 17 00:00:00 2001 From: Gerard Casas Saez Date: Tue, 16 Apr 2024 18:39:28 -0600 Subject: [PATCH 2/4] fix re-used vars --- shell/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/shell.go b/shell/shell.go index 7d3413f9..bd5238e0 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -134,7 +134,7 @@ func Detect() (Shell, error) { if entry.Shell == "" { return nil, errors.Errorf("/etc/passwd file entry for %q does not contain a shell field", u.Username) } - shell, ok := shells[filepath.Base(entry.Shell)] + shell, ok = shells[filepath.Base(entry.Shell)] if ok { return shell, nil } From 6f10b023d1ed535ed9ba802bfae0a4f603c00e88 Mon Sep 17 00:00:00 2001 From: Gerard Casas Saez Date: Tue, 16 Apr 2024 19:29:12 -0600 Subject: [PATCH 3/4] avoid reusing names --- shell/shell.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shell/shell.go b/shell/shell.go index bd5238e0..5471600b 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -95,8 +95,8 @@ func Resolve(name string) (Shell, error) { // Detect the user's shell. func Detect() (Shell, error) { // Check for SHELL environment variable. - shell, ok := shells[filepath.Base(os.Getenv("SHELL"))] - if ok { + envShell := filepath.Base(os.Getenv("SHELL")) + if shell, ok := shells[envShell]; ok { return shell, nil } @@ -134,7 +134,7 @@ func Detect() (Shell, error) { if entry.Shell == "" { return nil, errors.Errorf("/etc/passwd file entry for %q does not contain a shell field", u.Username) } - shell, ok = shells[filepath.Base(entry.Shell)] + shell, ok := shells[filepath.Base(entry.Shell)] if ok { return shell, nil } From bd7efdb87f07968eedb8aff28294814f93aea95b Mon Sep 17 00:00:00 2001 From: Gerard Casas Saez Date: Wed, 17 Apr 2024 10:01:33 -0600 Subject: [PATCH 4/4] move down --- shell/shell.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/shell.go b/shell/shell.go index 5471600b..87e8a891 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -94,13 +94,7 @@ func Resolve(name string) (Shell, error) { // Detect the user's shell. func Detect() (Shell, error) { - // Check for SHELL environment variable. - envShell := filepath.Base(os.Getenv("SHELL")) - if shell, ok := shells[envShell]; ok { - return shell, nil - } - - // Then, look for shell in parent processes. + // First look for shell in parent processes. pid := os.Getppid() for { process, err := ps.FindProcess(pid) @@ -118,6 +112,12 @@ func Detect() (Shell, error) { } } + // Next, check for SHELL environment variable. + envShell := filepath.Base(os.Getenv("SHELL")) + if shell, ok := shells[envShell]; ok { + return shell, nil + } + // Next, try to pull the shell from the user's password entry. u, err := user.Current() if err != nil {