| 1 |
package godo |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/mgutz/minimist" |
| 5 |
"gopkg.in/godo.v2/util" |
| 6 |
"gopkg.in/godo.v2/watcher" |
| 7 |
) |
| 8 |
|
| 9 |
func logVerbose(msg string, format string, args ...interface{}) { |
| 10 |
if !verbose { |
| 11 |
return |
| 12 |
} |
| 13 |
util.Debug(msg, format, args...) |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
type Context struct { |
| 18 |
|
| 19 |
Task *Task |
| 20 |
|
| 21 |
|
| 22 |
FileEvent *watcher.FileEvent |
| 23 |
|
| 24 |
|
| 25 |
Args minimist.ArgMap |
| 26 |
|
| 27 |
Error error |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func (context *Context) AnyFile() []string { |
| 33 |
if context.FileEvent != nil && context.FileEvent.Event != watcher.DELETED { |
| 34 |
return []string{context.FileEvent.Path} |
| 35 |
} |
| 36 |
return context.Task.SrcGlobs |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func (context *Context) Run(cmd string, options ...map[string]interface{}) { |
| 41 |
if context.Error != nil { |
| 42 |
logVerbose(context.Task.Name, "Context is in error. Skipping: %s\n", cmd) |
| 43 |
return |
| 44 |
} |
| 45 |
_, err := Run(cmd, options...) |
| 46 |
if err != nil { |
| 47 |
context.Error = err |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
func (context *Context) Bash(cmd string, options ...map[string]interface{}) { |
| 53 |
if context.Error != nil { |
| 54 |
logVerbose(context.Task.Name, "Context is in error. Skipping: %s\n", cmd) |
| 55 |
return |
| 56 |
} |
| 57 |
_, err := Bash(cmd, options...) |
| 58 |
if err != nil { |
| 59 |
context.Error = err |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (context *Context) Start(cmd string, options ...map[string]interface{}) { |
| 65 |
if context.Error != nil { |
| 66 |
logVerbose(context.Task.Name, "Context is in error. Skipping: %s\n", cmd) |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
err := startEx(context, cmd, options) |
| 71 |
if err != nil { |
| 72 |
context.Error = err |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (context *Context) BashOutput(script string, options ...map[string]interface{}) string { |
| 78 |
if len(options) == 0 { |
| 79 |
options = append(options, M{"$out": CaptureBoth}) |
| 80 |
} else { |
| 81 |
options[0]["$out"] = CaptureBoth |
| 82 |
} |
| 83 |
s, err := Bash(script, options...) |
| 84 |
if err != nil { |
| 85 |
context.Error = err |
| 86 |
return "" |
| 87 |
} |
| 88 |
return s |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (context *Context) RunOutput(commandstr string, options ...map[string]interface{}) string { |
| 93 |
if len(options) == 0 { |
| 94 |
options = append(options, M{"$out": CaptureBoth}) |
| 95 |
} else { |
| 96 |
options[0]["$out"] = CaptureBoth |
| 97 |
} |
| 98 |
s, err := Run(commandstr, options...) |
| 99 |
if err != nil { |
| 100 |
context.Error = err |
| 101 |
return "" |
| 102 |
} |
| 103 |
return s |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
func (context *Context) Check(err error, msg string) { |
| 117 |
if err != nil { |
| 118 |
if msg == "" { |
| 119 |
Halt(err) |
| 120 |
return |
| 121 |
} |
| 122 |
Halt(msg + ": " + err.Error()) |
| 123 |
} |
| 124 |
} |