Skip to content

Commit

Permalink
Add Timeout to SmbClientGetter to go-getter/v2 (#369)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nywilken committed Jul 7, 2022
1 parent d10f069 commit 4dc8b7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions get_smbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4dc8b7d

Please sign in to comment.