Skip to content

Commit

Permalink
feat: remote move,copy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Dec 13, 2022
1 parent 32a4351 commit 9301e74
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
39 changes: 39 additions & 0 deletions backend/runner/builtin/filesystem/remotecopy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package filesystem

import (
"fmt"
"strings"

"github.com/o8x/acorn/backend/runner/base"
"github.com/o8x/acorn/backend/runner/constant"
)

type RemoteCopy struct {
base.Plugin[constant.RemoteCopyParams]
}

func (s *RemoteCopy) Run() (string, error) {
cmd := []string{
"cp",
}

if s.Params.IsDir {
cmd = append(cmd, "-r")
}

source := strings.TrimSpace(s.Params.Source)
target := strings.TrimSpace(s.Params.Target)
command := strings.Join(append(cmd, fmt.Sprintf(`'%s' '%s'`, source, target)), " ")

s.Logger.Write("source: %s", source)
s.Logger.Write("target: %s", target)
s.Logger.Write("command: %s", command)

res, err := s.SSH.ExecShellCode(command)
if err != nil {
return "", err
}

s.Logger.Write("output: %s", res)
return res.String(), err
}
35 changes: 35 additions & 0 deletions backend/runner/builtin/filesystem/remotemove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package filesystem

import (
"fmt"
"strings"

"github.com/o8x/acorn/backend/runner/base"
"github.com/o8x/acorn/backend/runner/constant"
)

type RemoteMove struct {
base.Plugin[constant.RemoteMoveParams]
}

func (s *RemoteMove) Run() (string, error) {
cmd := []string{
"mv",
}

source := strings.TrimSpace(s.Params.Source)
target := strings.TrimSpace(s.Params.Target)
command := strings.Join(append(cmd, fmt.Sprintf(`'%s' '%s'`, source, target)), " ")

s.Logger.Write("source: %s", source)
s.Logger.Write("target: %s", target)
s.Logger.Write("command: %s", command)

res, err := s.SSH.ExecShellCode(command)
if err != nil {
return "", err
}

s.Logger.Write("output: %s", res)
return res.String(), err
}
13 changes: 12 additions & 1 deletion backend/runner/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ type RemoteDeleteParams struct {
CheckExist bool `json:"check_exist"`
}

type RemoteCopyParams struct {
Source string `json:"source"`
Target string `json:"target"`
IsDir bool `json:"is_dir"`
}

type RemoteMoveParams struct {
Source string `json:"source"`
Target string `json:"target"`
}

type PluginTypes interface {
RemoteDeleteParams | FileTransferParams | ShellParams
RemoteDeleteParams | FileTransferParams | ShellParams | RemoteCopyParams | RemoteMoveParams
}
10 changes: 10 additions & 0 deletions backend/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ func (p *Runner) run(pb Playbook, log *logger.Logger) error {
v = v1
}

if v1, ok := task["builtin.remote.fs.copy"]; ok {
plugin = &filesystem.RemoteCopy{}
v = v1
}

if v1, ok := task["builtin.remote.fs.move"]; ok {
plugin = &filesystem.RemoteMove{}
v = v1
}

if plugin == nil {
return fmt.Errorf("plugin not found")
}
Expand Down

0 comments on commit 9301e74

Please sign in to comment.