mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-05-09 18:23:21 +08:00
chore: support split from string text
This commit is contained in:
parent
9ef0a47bc0
commit
390cd4dc80
18
plugin.go
18
plugin.go
@ -182,19 +182,23 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p Plugin) scriptCommands() []string {
|
func (p Plugin) scriptCommands() []string {
|
||||||
numCommands := len(p.Config.Script)
|
scripts := []string{}
|
||||||
if p.Config.ScriptStop {
|
|
||||||
numCommands *= 2
|
|
||||||
}
|
|
||||||
|
|
||||||
commands := make([]string, numCommands)
|
|
||||||
|
|
||||||
for _, cmd := range p.Config.Script {
|
for _, cmd := range p.Config.Script {
|
||||||
if p.Config.ScriptStop {
|
if p.Config.ScriptStop {
|
||||||
commands = append(commands, "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;")
|
scripts = append(scripts, strings.Split(cmd, "\n")...)
|
||||||
|
} else {
|
||||||
|
scripts = append(scripts, cmd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commands := make([]string, 0)
|
||||||
|
|
||||||
|
for _, cmd := range scripts {
|
||||||
commands = append(commands, cmd)
|
commands = append(commands, cmd)
|
||||||
|
if p.Config.ScriptStop {
|
||||||
|
commands = append(commands, "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -527,3 +529,56 @@ func TestEnvOutput(t *testing.T) {
|
|||||||
func unindent(text string) string {
|
func unindent(text string) string {
|
||||||
return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
|
return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlugin_scriptCommands(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
Config Config
|
||||||
|
Writer io.Writer
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal testing",
|
||||||
|
fields: fields{
|
||||||
|
Config: Config{
|
||||||
|
Script: []string{"mkdir a", "mkdir b"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{"mkdir a", "mkdir b"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "script stop",
|
||||||
|
fields: fields{
|
||||||
|
Config: Config{
|
||||||
|
Script: []string{"mkdir a", "mkdir b"},
|
||||||
|
ScriptStop: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{"mkdir a", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir b", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "normal testing 2",
|
||||||
|
fields: fields{
|
||||||
|
Config: Config{
|
||||||
|
Script: []string{"mkdir a\nmkdir c", "mkdir b"},
|
||||||
|
ScriptStop: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []string{"mkdir a", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir c", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir b", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
p := Plugin{
|
||||||
|
Config: tt.fields.Config,
|
||||||
|
Writer: tt.fields.Writer,
|
||||||
|
}
|
||||||
|
if got := p.scriptCommands(); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("Plugin.scriptCommands() = %#v, want %#v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user