Debian Codegraph @

context.go

raw symbols SCIP
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 // Context is the data passed to a task.
17 type Context struct {
18 // Task is the currently running task.
19 Task *Task
20
21 // FileEvent is an event from the watcher with change details.
22 FileEvent *watcher.FileEvent
23
24 // Task command line arguments
25 Args minimist.ArgMap
26
27 Error error
28 }
29
30 // AnyFile returns either a non-DELETe FileEvent file or the WatchGlob patterns which
31 // can be used by goa.Load()
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 // Run runs a command
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 // Bash runs a bash shell.
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 // Start run aysnchronously.
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 // BashOutput executes a bash script and returns the output
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 // RunOutput runs a command and returns output.
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 // Check halts the task if err is not nil.
107 //
108 // Do this
109 // Check(err, "Some error occured")
110 //
111 // Instead of
112 //
113 // if err != nil {
114 // Halt(err)
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 }