refactor: move all subprocess-related code to new file
This commit is contained in:
committed by
Jose Diaz-Gonzalez
parent
74f27480fc
commit
c7becc6cad
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -13,75 +12,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
sh "github.com/codeskyblue/go-sh"
|
|
||||||
columnize "github.com/ryanuber/columnize"
|
columnize "github.com/ryanuber/columnize"
|
||||||
)
|
)
|
||||||
|
|
||||||
type errfunc func() error
|
type errfunc func() error
|
||||||
|
|
||||||
// ShellCmd represents a shell command to be run for dokku
|
|
||||||
type ShellCmd struct {
|
|
||||||
Env map[string]string
|
|
||||||
Command *exec.Cmd
|
|
||||||
CommandString string
|
|
||||||
Args []string
|
|
||||||
ShowOutput bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewShellCmd returns a new ShellCmd struct
|
|
||||||
func NewShellCmd(command string) *ShellCmd {
|
|
||||||
items := strings.Split(command, " ")
|
|
||||||
cmd := items[0]
|
|
||||||
args := items[1:]
|
|
||||||
return NewShellCmdWithArgs(cmd, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewShellCmdWithArgs returns a new ShellCmd struct
|
|
||||||
func NewShellCmdWithArgs(cmd string, args ...string) *ShellCmd {
|
|
||||||
commandString := strings.Join(append([]string{cmd}, args...), " ")
|
|
||||||
|
|
||||||
return &ShellCmd{
|
|
||||||
Command: exec.Command(cmd, args...),
|
|
||||||
CommandString: commandString,
|
|
||||||
Args: args,
|
|
||||||
ShowOutput: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sc *ShellCmd) setup() {
|
|
||||||
env := os.Environ()
|
|
||||||
for k, v := range sc.Env {
|
|
||||||
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
||||||
}
|
|
||||||
sc.Command.Env = env
|
|
||||||
if sc.ShowOutput {
|
|
||||||
sc.Command.Stdout = os.Stdout
|
|
||||||
sc.Command.Stderr = os.Stderr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute is a lightweight wrapper around exec.Command
|
|
||||||
func (sc *ShellCmd) Execute() bool {
|
|
||||||
sc.setup()
|
|
||||||
|
|
||||||
if err := sc.Command.Run(); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output is a lightweight wrapper around exec.Command.Output()
|
|
||||||
func (sc *ShellCmd) Output() ([]byte, error) {
|
|
||||||
sc.setup()
|
|
||||||
return sc.Command.Output()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CombinedOutput is a lightweight wrapper around exec.Command.CombinedOutput()
|
|
||||||
func (sc *ShellCmd) CombinedOutput() ([]byte, error) {
|
|
||||||
sc.setup()
|
|
||||||
return sc.Command.CombinedOutput()
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppRoot returns the app root path
|
// AppRoot returns the app root path
|
||||||
func AppRoot(appName string) string {
|
func AppRoot(appName string) string {
|
||||||
dokkuRoot := MustGetEnv("DOKKU_ROOT")
|
dokkuRoot := MustGetEnv("DOKKU_ROOT")
|
||||||
@@ -485,27 +420,6 @@ func VerifyAppName(appName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlugnTrigger fire the given plugn trigger with the given args
|
|
||||||
func PlugnTrigger(triggerName string, args ...string) error {
|
|
||||||
return PlugnTriggerSetup(triggerName, args...).Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlugnTriggerOutput fire the given plugn trigger with the given args
|
|
||||||
func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) {
|
|
||||||
return PlugnTriggerSetup(triggerName, args...).Output()
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlugnTriggerSetup sets up a plugn trigger call
|
|
||||||
func PlugnTriggerSetup(triggerName string, args ...string) *sh.Session {
|
|
||||||
shellArgs := make([]interface{}, len(args)+2)
|
|
||||||
shellArgs[0] = "trigger"
|
|
||||||
shellArgs[1] = triggerName
|
|
||||||
for i, arg := range args {
|
|
||||||
shellArgs[i+2] = arg
|
|
||||||
}
|
|
||||||
return sh.Command("plugn", shellArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func times(str string, n int) (out string) {
|
func times(str string, n int) (out string) {
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
out += str
|
out += str
|
||||||
|
|||||||
94
plugins/common/subprocess.go
Normal file
94
plugins/common/subprocess.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/codeskyblue/go-sh"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ShellCmd represents a shell command to be run for dokku
|
||||||
|
type ShellCmd struct {
|
||||||
|
Env map[string]string
|
||||||
|
Command *exec.Cmd
|
||||||
|
CommandString string
|
||||||
|
Args []string
|
||||||
|
ShowOutput bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewShellCmd returns a new ShellCmd struct
|
||||||
|
func NewShellCmd(command string) *ShellCmd {
|
||||||
|
items := strings.Split(command, " ")
|
||||||
|
cmd := items[0]
|
||||||
|
args := items[1:]
|
||||||
|
return NewShellCmdWithArgs(cmd, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewShellCmdWithArgs returns a new ShellCmd struct
|
||||||
|
func NewShellCmdWithArgs(cmd string, args ...string) *ShellCmd {
|
||||||
|
commandString := strings.Join(append([]string{cmd}, args...), " ")
|
||||||
|
|
||||||
|
return &ShellCmd{
|
||||||
|
Command: exec.Command(cmd, args...),
|
||||||
|
CommandString: commandString,
|
||||||
|
Args: args,
|
||||||
|
ShowOutput: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *ShellCmd) setup() {
|
||||||
|
env := os.Environ()
|
||||||
|
for k, v := range sc.Env {
|
||||||
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
||||||
|
}
|
||||||
|
sc.Command.Env = env
|
||||||
|
if sc.ShowOutput {
|
||||||
|
sc.Command.Stdout = os.Stdout
|
||||||
|
sc.Command.Stderr = os.Stderr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute is a lightweight wrapper around exec.Command
|
||||||
|
func (sc *ShellCmd) Execute() bool {
|
||||||
|
sc.setup()
|
||||||
|
|
||||||
|
if err := sc.Command.Run(); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output is a lightweight wrapper around exec.Command.Output()
|
||||||
|
func (sc *ShellCmd) Output() ([]byte, error) {
|
||||||
|
sc.setup()
|
||||||
|
return sc.Command.Output()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CombinedOutput is a lightweight wrapper around exec.Command.CombinedOutput()
|
||||||
|
func (sc *ShellCmd) CombinedOutput() ([]byte, error) {
|
||||||
|
sc.setup()
|
||||||
|
return sc.Command.CombinedOutput()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlugnTrigger fire the given plugn trigger with the given args
|
||||||
|
func PlugnTrigger(triggerName string, args ...string) error {
|
||||||
|
return PlugnTriggerSetup(triggerName, args...).Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlugnTriggerOutput fire the given plugn trigger with the given args
|
||||||
|
func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) {
|
||||||
|
return PlugnTriggerSetup(triggerName, args...).Output()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlugnTriggerSetup sets up a plugn trigger call
|
||||||
|
func PlugnTriggerSetup(triggerName string, args ...string) *sh.Session {
|
||||||
|
shellArgs := make([]interface{}, len(args)+2)
|
||||||
|
shellArgs[0] = "trigger"
|
||||||
|
shellArgs[1] = triggerName
|
||||||
|
for i, arg := range args {
|
||||||
|
shellArgs[i+2] = arg
|
||||||
|
}
|
||||||
|
return sh.Command("plugn", shellArgs...)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user