-
Notifications
You must be signed in to change notification settings - Fork 200
/
runner.go
81 lines (65 loc) · 1.81 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package plugins
import (
"fmt"
"strings"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/context"
"github.com/pkg/errors"
)
type CommandOptions struct {
Command string
}
type PluginRunner struct {
*context.Context
pluginName string
}
func NewRunner(pluginName string) *PluginRunner {
return &PluginRunner{
Context: context.New(),
pluginName: pluginName,
}
}
func (r *PluginRunner) Validate() error {
if r.pluginName == "" {
return errors.New("Plugin not specified")
}
pluginPath, err := config.New().GetPluginPath(r.pluginName)
if err != nil {
return errors.Wrapf(err, "Failed to get plugin path for %s", r.pluginName)
}
exists, err := r.FileSystem.Exists(pluginPath)
if err != nil {
return errors.Wrapf(err, "Failed to stat path %s", pluginPath)
}
if !exists {
return errors.Errorf("Plugin %s doesn't exist in filesystem with path %s", r.pluginName, pluginPath)
}
return nil
}
func (r *PluginRunner) Run(commandOpts CommandOptions) error {
if r.Debug {
fmt.Fprintln(r.Err, "DEBUG Plugin Name: ", r.pluginName)
fmt.Fprintln(r.Err, "DEBUG Plugin Command: ", commandOpts.Command)
}
pluginPath, err := config.New().GetPluginPath(r.pluginName)
if r.Debug {
fmt.Fprintln(r.Err, "DEBUG Plugin Path: ", pluginPath)
}
if err != nil {
return errors.Wrapf(err, "Failed to get plugin path for %s", r.pluginName)
}
cmdArgs := strings.Split(commandOpts.Command, " ")
cmd := r.NewCommand(pluginPath, cmdArgs...)
// Pipe the output from the plugin to porter
cmd.Stdout = r.Out
cmd.Stderr = r.Err
prettyCmd := fmt.Sprintf("%s%s", cmd.Dir, strings.Join(cmd.Args, " "))
if r.Debug {
fmt.Fprintln(r.Err, "DEBUG Plugin Full Command: ", prettyCmd)
}
err = cmd.Start()
if err != nil {
return errors.Wrapf(err, "could not run plugin command %s", prettyCmd)
}
return cmd.Wait()
}