From b571b8a4b6b2f5190e18d4dcf58cb25c5476665e Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Fri, 8 Nov 2024 16:13:27 +0200 Subject: [PATCH] Allow sudofn injection into connection (#227) - allow connection objects to have a sudofn injected - if sudofn is set, then Connect won't try to detect it Signed-off-by: James Nesbitt --- connection.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/connection.go b/connection.go index b0f2aa50..f444428c 100644 --- a/connection.go +++ b/connection.go @@ -241,11 +241,19 @@ func (c *Connection) Connect() error { c.OSVersion = &o } - c.configureSudo() + if c.sudofunc == nil { + c.discoverSudo() + } return nil } +// SetSudofn inject a sudofn into the connection before Connect() +// @NOTE this will avoid sudo detection if done before Connect() +func (c *Connection) SetSudofn(f func(string) string) { + c.sudofunc = f +} + func sudoNoop(cmd string) string { return cmd } @@ -287,21 +295,21 @@ func sudoWindows(cmd string) string { return cmd } -func (c *Connection) configureSudo() { +func (c *Connection) discoverSudo() { if !c.IsWindows() { if c.Exec(`[ "$(id -u)" = 0 ]`) == nil { // user is already root - c.sudofunc = sudoNoop + c.SetSudofn(sudoNoop) return } if c.Exec(`sudo -n -l`) == nil { // user has passwordless sudo - c.sudofunc = sudoSudo + c.SetSudofn(sudoSudo) return } if c.Exec(`doas -n -- "${SHELL-sh}" -c true`) == nil { // user has passwordless doas - c.sudofunc = sudoDoas + c.SetSudofn(sudoDoas) } return }