From 4dc8b7dda9e633d3b6957cf810adf5c96825f640 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 7 Jul 2022 18:37:18 -0400 Subject: [PATCH] Add Timeout to SmbClientGetter to go-getter/v2 (#369) * Add Timeout to SmbClientGetter Allow caller to configure a command execution context with a default timeout for all smbclient CLI operations. * Make smbclient timeout configurable --- get_git.go | 2 +- get_smbclient.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/get_git.go b/get_git.go index b8afecc94..6f0c2a7a4 100644 --- a/get_git.go +++ b/get_git.go @@ -26,7 +26,7 @@ import ( type GitGetter struct { Detectors []Detector - // Timeout sets a deadline which all hg CLI operations should + // Timeout sets a deadline which all git CLI operations should // complete within. Defaults to zero which means no timeout. Timeout time.Duration } diff --git a/get_smbclient.go b/get_smbclient.go index 802cb9f2d..7d89fe573 100644 --- a/get_smbclient.go +++ b/get_smbclient.go @@ -9,13 +9,19 @@ import ( "os/exec" "path/filepath" "regexp" + "strconv" "strings" "syscall" ) // SmbClientGetter is a Getter implementation that will download a module from // a shared folder using smbclient cli. -type SmbClientGetter struct{} +type SmbClientGetter struct { + + // Timeout in seconds sets a deadline which all smb client CLI operations should + // complete within. Defaults to zero which means to use the default client timeout of 20 seconds. + Timeout int +} func (g *SmbClientGetter) Mode(ctx context.Context, u *url.URL) (Mode, error) { if u.Host == "" || u.Path == "" { @@ -216,6 +222,10 @@ func (g *SmbClientGetter) smbclientCmdArgs(used *url.Userinfo, hostPath string, baseCmd = append(baseCmd, hostPath) baseCmd = append(baseCmd, "--directory") baseCmd = append(baseCmd, fileDir) + if g.Timeout > 0 { + baseCmd = append(baseCmd, "-t") + baseCmd = append(baseCmd, strconv.Itoa(g.Timeout)) + } return baseCmd } @@ -254,7 +264,8 @@ func (g *SmbClientGetter) isDirectory(args []string, object string) (bool, error } func (g *SmbClientGetter) runSmbClientCommand(dst string, args []string) (string, error) { - cmd := exec.Command("smbclient", args...) + ctx := context.Background() + cmd := exec.CommandContext(ctx, "smbclient", args...) if dst != "" { cmd.Dir = dst