Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timeout to SmbClientGetter to go-getter/v2 #369

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 8 additions & 11 deletions get_smbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)

// SmbClientGetter is a Getter implementation that will download a module from
// a shared folder using smbclient cli.
type SmbClientGetter struct {

// Timeout sets a deadline which all smb client CLI operations should
// complete within. Defaults to zero which means no timeout.
Timeout time.Duration
// 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) {
Expand Down Expand Up @@ -222,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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

baseCmd = append(baseCmd, strconv.Itoa(g.Timeout))
}
return baseCmd
}

Expand Down Expand Up @@ -261,13 +265,6 @@ func (g *SmbClientGetter) isDirectory(args []string, object string) (bool, error

func (g *SmbClientGetter) runSmbClientCommand(dst string, args []string) (string, error) {
ctx := context.Background()

if g.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), g.Timeout)
defer cancel()
}

cmd := exec.CommandContext(ctx, "smbclient", args...)

if dst != "" {
Expand Down