Skip to content

Commit 4dc8b7d

Browse files
authored
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
1 parent d10f069 commit 4dc8b7d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

get_git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
type GitGetter struct {
2727
Detectors []Detector
2828

29-
// Timeout sets a deadline which all hg CLI operations should
29+
// Timeout sets a deadline which all git CLI operations should
3030
// complete within. Defaults to zero which means no timeout.
3131
Timeout time.Duration
3232
}

get_smbclient.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"regexp"
12+
"strconv"
1213
"strings"
1314
"syscall"
1415
)
1516

1617
// SmbClientGetter is a Getter implementation that will download a module from
1718
// a shared folder using smbclient cli.
18-
type SmbClientGetter struct{}
19+
type SmbClientGetter struct {
20+
21+
// Timeout in seconds sets a deadline which all smb client CLI operations should
22+
// complete within. Defaults to zero which means to use the default client timeout of 20 seconds.
23+
Timeout int
24+
}
1925

2026
func (g *SmbClientGetter) Mode(ctx context.Context, u *url.URL) (Mode, error) {
2127
if u.Host == "" || u.Path == "" {
@@ -216,6 +222,10 @@ func (g *SmbClientGetter) smbclientCmdArgs(used *url.Userinfo, hostPath string,
216222
baseCmd = append(baseCmd, hostPath)
217223
baseCmd = append(baseCmd, "--directory")
218224
baseCmd = append(baseCmd, fileDir)
225+
if g.Timeout > 0 {
226+
baseCmd = append(baseCmd, "-t")
227+
baseCmd = append(baseCmd, strconv.Itoa(g.Timeout))
228+
}
219229
return baseCmd
220230
}
221231

@@ -254,7 +264,8 @@ func (g *SmbClientGetter) isDirectory(args []string, object string) (bool, error
254264
}
255265

256266
func (g *SmbClientGetter) runSmbClientCommand(dst string, args []string) (string, error) {
257-
cmd := exec.Command("smbclient", args...)
267+
ctx := context.Background()
268+
cmd := exec.CommandContext(ctx, "smbclient", args...)
258269

259270
if dst != "" {
260271
cmd.Dir = dst

0 commit comments

Comments
 (0)