Skip to content

Commit

Permalink
feat(fetch): add flag '-t/--tmp-dir' (#19)
Browse files Browse the repository at this point in the history
Fixes #19.
  • Loading branch information
windvalley committed Jan 11, 2022
1 parent a6a21c3 commit 8d3d6f6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.4.1]

### Added

- Add flag `-t/--tmp-dir` for subcommand `fetch`. For details at [#19](https://github.com/windvalley/gossh/issues/19).

## [1.4.0]

### Added
Expand Down
13 changes: 11 additions & 2 deletions internal/cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
var (
srcFiles []string
localDstDir string
tmpDir string
)

// fetchCmd represents the fetch command
Expand All @@ -53,7 +54,11 @@ Copy files/dirs from target hosts to local.`,
#
$ gossh fetch host1 host2 -f /path1/foo.txt -f /path2/bar/ -d /tmp/backup
or
$ gossh fetch host1 host2 -f /path1/foo.txt,/path2/bar/ -d /tmp/backup`,
$ gossh fetch host1 host2 -f /path1/foo.txt,/path2/bar/ -d /tmp/backup
# Specify tmp dir on target host instead of default /tmp by flag '-t'.
# NOTE: If the tmp dir not exist, it will auto create it.
$ gossh fetch host1 -f /path/foo.txt -d ./backup/ -t /home/user/tmp/`,
PreRun: func(cmd *cobra.Command, args []string) {
if errs := config.Validate(); len(errs) != 0 {
util.CheckErr(errs)
Expand All @@ -64,7 +69,7 @@ Copy files/dirs from target hosts to local.`,

task.SetHosts(args)
task.SetFetchFiles(srcFiles)
task.SetFileOptions(localDstDir, true)
task.SetFetchOptions(localDstDir, tmpDir)

task.Start()
},
Expand All @@ -80,4 +85,8 @@ func init() {
fetchCmd.Flags().StringVarP(&localDstDir, "dest-path", "d", "",
"local directory that files/dirs from target hosts will be copied to",
)

fetchCmd.Flags().StringVarP(&tmpDir, "tmp-dir", "t", "/tmp",
"directory of target hosts for storing temporary zip file",
)
}
2 changes: 1 addition & 1 deletion internal/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Copy local files/dirs to target hosts.`,

task.SetHosts(args)
task.SetPushfiles(files, zipFiles)
task.SetFileOptions(fileDstPath, allowOverwrite)
task.SetPushOptions(fileDstPath, allowOverwrite)

task.Start()
},
Expand Down
18 changes: 13 additions & 5 deletions internal/pkg/sshtask/sshtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ type Task struct {
// hostnames or ips from command line arguments.
hosts []string

command string
scriptFile string
command string
scriptFile string

pushFiles *pushFiles
fetchFiles []string
dstDir string
tmpDir string
remove bool
allowOverwrite bool

Expand Down Expand Up @@ -184,12 +186,18 @@ func (t *Task) SetScriptOptions(destPath string, remove, allowOverwrite bool) {
t.allowOverwrite = allowOverwrite
}

// SetFileOptions ...
func (t *Task) SetFileOptions(destPath string, allowOverwrite bool) {
// SetPushOptions ...
func (t *Task) SetPushOptions(destPath string, allowOverwrite bool) {
t.dstDir = destPath
t.allowOverwrite = allowOverwrite
}

// SetFetchOptions ...
func (t *Task) SetFetchOptions(destPath, tmpDir string) {
t.dstDir = destPath
t.tmpDir = tmpDir
}

// RunSSH implements batchssh.Task
func (t *Task) RunSSH(addr string) (string, error) {
lang := t.configFlags.Run.Lang
Expand All @@ -204,7 +212,7 @@ func (t *Task) RunSSH(addr string) (string, error) {
case PushTask:
return t.sshClient.PushFiles(addr, t.pushFiles.files, t.pushFiles.zipFiles, t.dstDir, t.allowOverwrite)
case FetchTask:
return t.sshClient.FetchFiles(addr, t.fetchFiles, t.dstDir)
return t.sshClient.FetchFiles(addr, t.fetchFiles, t.dstDir, t.tmpDir)
default:
return "", fmt.Errorf("unknown task type: %v", t.taskType)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/batchssh/batchssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (c *Client) PushFiles(
func (c *Client) FetchFiles(
addr string,
srcFiles []string,
dstDir string,
dstDir, tmpDir string,
) (string, error) {
client, err := c.getClient(addr)
if err != nil {
Expand Down Expand Up @@ -337,7 +337,7 @@ func (c *Client) FetchFiles(
}
defer session.Close()

zippedFileTmpDir := fmt.Sprintf("/tmp/gossh-%s", addr)
zippedFileTmpDir := path.Join(tmpDir, "gossh-"+addr)
tmpZipFile := fmt.Sprintf("%s.%d", addr, time.Now().UnixMicro())
zippedFileFullpath := path.Join(zippedFileTmpDir, tmpZipFile)
_, err = c.executeCmd(
Expand Down

0 comments on commit 8d3d6f6

Please sign in to comment.