diff options
| author | Shulhan <ms@kilabit.info> | 2021-08-01 21:23:04 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-08-01 21:23:04 +0700 |
| commit | c3b239bbeb087dd454f548b1e4aaf9b3a4c8e324 (patch) | |
| tree | 1be92ed2065663babe848adb0d0324547162c3d4 /command.go | |
| parent | adf810fae6254bcfc5507025bb6c0d378ba66564 (diff) | |
| download | awwan-c3b239bbeb087dd454f548b1e4aaf9b3a4c8e324.tar.xz | |
all: refactoring the command
This changes introduce new type Awwan that will run the "local" and
"play" command.
This is the first changes to refactoring the code to separate between
script, host, and command to make it reusable and can be run multiple
times.
Diffstat (limited to 'command.go')
| -rw-r--r-- | command.go | 33 |
1 files changed, 2 insertions, 31 deletions
@@ -10,7 +10,6 @@ import ( "log" "os" "path/filepath" - "strings" "github.com/shuLhan/share/lib/io" "github.com/shuLhan/share/lib/os/exec" @@ -20,11 +19,10 @@ import ( ) // -// Command contains the mode, environment, script, and SSH client per one +// Command contains the environment, script, and SSH client per one // execution. // type Command struct { - mode string // One of the mode to execute the script. scriptPath string // Location of the script in file system. scriptStart int scriptEnd int @@ -42,25 +40,12 @@ type Command struct { func NewCommand(mode, scriptPath string, startAt, endAt int) (cmd *Command, err error) { logp := "NewCommand" - mode = strings.ToLower(mode) - switch mode { - case modeLocal: - case modePlay: - default: - return nil, fmt.Errorf("%s: unknown command %s\n", logp, mode) - } - scriptPath, err = filepath.Abs(scriptPath) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } - if endAt < startAt { - endAt = startAt - } - cmd = &Command{ - mode: mode, scriptPath: scriptPath, scriptStart: startAt, scriptEnd: endAt, @@ -73,7 +58,7 @@ func NewCommand(mode, scriptPath string, startAt, endAt int) (cmd *Command, err cmd.tmpDir = filepath.Join("/tmp", cmd.env.randomString) - if mode == modePlay { + if mode == CommandModePlay { err = cmd.initSSHClient() if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) @@ -342,20 +327,6 @@ func (cmd *Command) sudoPut(stmt []byte) (err error) { return cmd.sshClient.Execute(moveStmt) } -// -// Run the script. -// -func (cmd *Command) Run() (err error) { - switch cmd.mode { - case modeLocal: - err = cmd.doLocal() - case modePlay: - err = cmd.doPlay() - } - - return err -} - func (cmd *Command) executeLocalScript() { for x := cmd.scriptStart; x <= cmd.scriptEnd; x++ { stmt := cmd.script.Statements[x] |
