| 1 | **Documentation is WIP** |
| 2 | |
| 3 | # godo |
| 4 | |
| 5 | [godoc](https://godoc.org/github.com/mgutz/godo/v2) |
| 6 | |
| 7 | godo is a task runner and file watcher for golang in the spirit of |
| 8 | rake, gulp. |
| 9 | |
| 10 | To install |
| 11 | |
| 12 | go get -u gopkg.in/godo.v2/cmd/godo |
| 13 | |
| 14 | ## Godofile |
| 15 | |
| 16 | Godo runs `Gododir/main.go`. |
| 17 | |
| 18 | As an example, create a file **Gododir/main.go** with this content |
| 19 | |
| 20 | ```go |
| 21 | package main |
| 22 | |
| 23 | import ( |
| 24 | "fmt" |
| 25 | do "gopkg.in/godo.v2" |
| 26 | ) |
| 27 | |
| 28 | func tasks(p *do.Project) { |
| 29 | do.Env = `GOPATH=.vendor::$GOPATH` |
| 30 | |
| 31 | p.Task("default", do.S{"hello", "build"}, nil) |
| 32 | |
| 33 | p.Task("hello", nil, func(c *do.Context) { |
| 34 | name := c.Args.AsString("name", "n") |
| 35 | if name == "" { |
| 36 | c.Bash("echo Hello $USER!") |
| 37 | } else { |
| 38 | fmt.Println("Hello", name) |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | p.Task("assets?", nil, func(c *do.Context) { |
| 43 | // The "?" tells Godo to run this task ONLY ONCE regardless of |
| 44 | // how many tasks depend on it. In this case watchify watches |
| 45 | // on its own. |
| 46 | c.Run("watchify public/js/index.js d -o dist/js/app.bundle.js") |
| 47 | }).Src("public/**/*.{css,js,html}") |
| 48 | |
| 49 | p.Task("build", do.S{"views", "assets"}, func(c *do.Context) { |
| 50 | c.Run("GOOS=linux GOARCH=amd64 go build", do.M{"$in": "cmd/server"}) |
| 51 | }).Src("**/*.go") |
| 52 | |
| 53 | p.Task("server", do.S{"views", "assets"}, func(c *do.Context) { |
| 54 | // rebuilds and restarts when a watched file changes |
| 55 | c.Start("main.go", do.M{"$in": "cmd/server"}) |
| 56 | }).Src("server/**/*.go", "cmd/server/*.{go,json}"). |
| 57 | Debounce(3000) |
| 58 | |
| 59 | p.Task("views", nil, func(c *do.Context) { |
| 60 | c.Run("razor templates") |
| 61 | }).Src("templates/**/*.go.html") |
| 62 | } |
| 63 | |
| 64 | func main() { |
| 65 | do.Godo(tasks) |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | To run "server" task from parent dir of `Gododir/` |
| 70 | |
| 71 | godo server |
| 72 | |
| 73 | To rerun "server" and its dependencies whenever any of their watched files change |
| 74 | |
| 75 | godo server --watch |
| 76 | |
| 77 | To run the "default" task which runs "hello" and "build" |
| 78 | |
| 79 | godo |
| 80 | |
| 81 | Task names may add a "?" suffix to execute only once even when watching |
| 82 | |
| 83 | ```go |
| 84 | // build once regardless of number of dependents |
| 85 | p.Task("assets?", nil, func(*do.Context) { }) |
| 86 | ``` |
| 87 | |
| 88 | Task dependencies |
| 89 | |
| 90 | do.S{} or do.Series{} - dependent tasks to run in series |
| 91 | do.P{} or do.Parallel{} - dependent tasks to run in parallel |
| 92 | |
| 93 | For example, do.S{"clean", do.P{"stylesheets", "templates"}, "build"} |
| 94 | |
| 95 | |
| 96 | ### Task Option Funcs |
| 97 | |
| 98 | * Task#Src() - specify watch paths or the src files for Task#Dest() |
| 99 | |
| 100 | Glob patterns |
| 101 | |
| 102 | /**/ - match zero or more directories |
| 103 | {a,b} - match a or b, no spaces |
| 104 | * - match any non-separator char |
| 105 | ? - match a single non-separator char |
| 106 | **/ - match any directory, start of pattern only |
| 107 | /** - match any in this directory, end of pattern only |
| 108 | ! - removes files from result set, start of pattern only |
| 109 | |
| 110 | * Task#Dest(globs ...string) - If globs in Src are newer than Dest, then |
| 111 | the task is run |
| 112 | |
| 113 | * Task#Desc(description string) - Set task's description in usage. |
| 114 | |
| 115 | * Task#Debounce(duration time.Duration) - Disallow a task from running until duration |
| 116 | has elapsed. |
| 117 | |
| 118 | * Task#Deps(names ...interface{}) - Can be `S, Series, P, Parallel, string` |
| 119 | |
| 120 | |
| 121 | ### Task CLI Arguments |
| 122 | |
| 123 | Task CLI arguments follow POSIX style flag convention |
| 124 | (unlike go's built-in flag package). Any command line arguments |
| 125 | succeeding `--` are passed to each task. Note, arguments before `--` |
| 126 | are reserved for `godo`. |
| 127 | |
| 128 | As an example, |
| 129 | |
| 130 | ```go |
| 131 | p.Task("hello", nil, func(c *do.Context) { |
| 132 | // "(none)" is the default value |
| 133 | msg := c.Args.MayString("(none)", "message", "msg", "m") |
| 134 | var name string |
| 135 | if len(c.Args.NonFlags()) == 1 { |
| 136 | name = c.Args.NonFlags()[0] |
| 137 | } |
| 138 | fmt.Println(msg, name) |
| 139 | }) |
| 140 | ``` |
| 141 | |
| 142 | running |
| 143 | |
| 144 | ```sh |
| 145 | # prints "(none)" |
| 146 | godo hello |
| 147 | |
| 148 | # prints "Hello dude" using POSIX style flags |
| 149 | godo hello -- dude --message Hello |
| 150 | godo hello -- dude --msg Hello |
| 151 | godo hello -- -m Hello dude |
| 152 | ``` |
| 153 | |
| 154 | Args functions are categorized as |
| 155 | |
| 156 | * `Must*` - Argument must be set by user or panic. |
| 157 | |
| 158 | ```go |
| 159 | c.Args.MustInt("number", "n") |
| 160 | ``` |
| 161 | |
| 162 | * `May*` - If argument is not set, default to first value. |
| 163 | |
| 164 | ```go |
| 165 | // defaults to 100 |
| 166 | c.Args.MayInt(100, "number", "n") |
| 167 | ``` |
| 168 | |
| 169 | * `As*` - If argument is not set, default to zero value. |
| 170 | |
| 171 | ```go |
| 172 | // defaults to 0 |
| 173 | c.Args.AsInt("number", "n") |
| 174 | ``` |
| 175 | |
| 176 | |
| 177 | ## Modularity and Namespaces |
| 178 | |
| 179 | A project may include other tasks functions with `Project#Use`. `Use` requires a namespace to |
| 180 | prevent task name conflicts with existing tasks. |
| 181 | |
| 182 | ```go |
| 183 | func buildTasks(p *do.Project) { |
| 184 | p.Task("default", S{"clean"}, nil) |
| 185 | |
| 186 | p.Task("clean", nil, func(*do.Context) { |
| 187 | fmt.Println("build clean") |
| 188 | }) |
| 189 | } |
| 190 | |
| 191 | func tasks(p *do.Project) { |
| 192 | p.Use("build", buildTasks) |
| 193 | |
| 194 | p.Task("clean", nil, func(*do.Context) { |
| 195 | fmt.Println("root clean") |
| 196 | }) |
| 197 | |
| 198 | p.Task("build", do.S{"build:default"}, func(*do.Context) { |
| 199 | fmt.Println("root clean") |
| 200 | }) |
| 201 | } |
| 202 | ``` |
| 203 | |
| 204 | Running `godo build:.` or `godo build` results in output of `build clean`. Note that |
| 205 | it uses the `clean` task in its namespace not the `clean` in the parent project. |
| 206 | |
| 207 | The special name `build:.` is alias for `build:default`. |
| 208 | |
| 209 | Task dependencies that start with `"/"` are relative to the parent project and |
| 210 | may be called referenced from sub projects. |
| 211 | |
| 212 | ## godobin |
| 213 | |
| 214 | `godo` compiles `Godofile.go` to `godobin-VERSION` (`godobin-VERSION.exe` on Windows) whenever |
| 215 | `Godofile.go` changes. The binary file is built into the same directory as |
| 216 | `Godofile.go` and should be ignored by adding the path `godobin*` to `.gitignore`. |
| 217 | |
| 218 | ## Exec functions |
| 219 | |
| 220 | All of these functions accept a `map[string]interface{}` or `M` for |
| 221 | options. Option keys that start with `"$"` are reserved for `godo`. |
| 222 | Other fields can be used as context for template. |
| 223 | |
| 224 | ### Bash |
| 225 | |
| 226 | Bash functions uses the bash executable and may not run on all OS. |
| 227 | |
| 228 | Run a bash script string. The script can be multiline line with continutation. |
| 229 | |
| 230 | ```go |
| 231 | c.Bash(` |
| 232 | echo -n $USER |
| 233 | echo some really long \ |
| 234 | command |
| 235 | `) |
| 236 | ``` |
| 237 | |
| 238 | Bash can use Go templates |
| 239 | |
| 240 | ```go |
| 241 | c.Bash(`echo -n {{.name}}`, do.M{"name": "mario", "$in": "cmd/bar"}) |
| 242 | ``` |
| 243 | |
| 244 | Run a bash script and capture STDOUT and STDERR. |
| 245 | |
| 246 | ```go |
| 247 | output, err := c.BashOutput(`echo -n $USER`) |
| 248 | ``` |
| 249 | |
| 250 | ### Run |
| 251 | |
| 252 | Run `go build` inside of cmd/app and set environment variables. |
| 253 | |
| 254 | ```go |
| 255 | c.Run(`GOOS=linux GOARCH=amd64 go build`, do.M{"$in": "cmd/app"}) |
| 256 | ``` |
| 257 | |
| 258 | Run can use Go templates |
| 259 | |
| 260 | ```go |
| 261 | c.Run(`echo -n {{.name}}`, do.M{"name": "mario", "$in": "cmd/app"}) |
| 262 | ``` |
| 263 | |
| 264 | Run and capture STDOUT and STDERR |
| 265 | |
| 266 | ```go |
| 267 | output, err := c.RunOutput("whoami") |
| 268 | ``` |
| 269 | |
| 270 | ### Start |
| 271 | |
| 272 | Start an async command. If the executable has suffix ".go" then it will be "go install"ed then executed. |
| 273 | Use this for watching a server task. |
| 274 | |
| 275 | ```go |
| 276 | c.Start("main.go", do.M{"$in": "cmd/app"}) |
| 277 | ``` |
| 278 | |
| 279 | Godo tracks the process ID of started processes to restart the app gracefully. |
| 280 | |
| 281 | ### Inside |
| 282 | |
| 283 | To run many commands inside a directory, use `Inside` instead of the `$in` option. |
| 284 | `Inside` changes the working directory. |
| 285 | |
| 286 | ```go |
| 287 | do.Inside("somedir", func() { |
| 288 | do.Run("...") |
| 289 | do.Bash("...") |
| 290 | }) |
| 291 | ``` |
| 292 | |
| 293 | ## User Input |
| 294 | |
| 295 | To get plain string |
| 296 | |
| 297 | ```go |
| 298 | user := do.Prompt("user: ") |
| 299 | ``` |
| 300 | |
| 301 | To get password |
| 302 | |
| 303 | ```go |
| 304 | password := do.PromptPassword("password: ") |
| 305 | ``` |
| 306 | |
| 307 | ## Godofile Run-Time Environment |
| 308 | |
| 309 | ### From command-line |
| 310 | |
| 311 | Environment variables may be set via key-value pairs as arguments to |
| 312 | godo. This feature was added to facilitate users on Windows. |
| 313 | |
| 314 | ```sh |
| 315 | godo NAME=mario GOPATH=./vendor hello |
| 316 | ``` |
| 317 | |
| 318 | ### From source code |
| 319 | |
| 320 | To specify whether to inherit from parent's process environment, |
| 321 | set `InheritParentEnv`. This setting defaults to true |
| 322 | |
| 323 | ```go |
| 324 | do.InheritParentEnv = false |
| 325 | ``` |
| 326 | |
| 327 | To specify the base environment for your tasks, set `Env`. |
| 328 | Separate with whitespace or newlines. |
| 329 | |
| 330 | ```go |
| 331 | do.Env = ` |
| 332 | GOPATH=.vendor::$GOPATH |
| 333 | PG_USER=mario |
| 334 | ` |
| 335 | ``` |
| 336 | |
| 337 | Functions can add or override environment variables as part of the command string. |
| 338 | Note that environment variables are set before the executable similar to a shell; |
| 339 | however, the `Run` and `Start` functions do not use a shell. |
| 340 | |
| 341 | ```go |
| 342 | p.Task("build", nil, func(c *do.Context) { |
| 343 | c.Run("GOOS=linux GOARCH=amd64 go build" ) |
| 344 | }) |
| 345 | ``` |
| 346 | |
| 347 | The effective environment for exec functions is: `parent (if inherited) <- do.Env <- func parsed env` |
| 348 | |
| 349 | Paths should use `::` as a cross-platform path list separator. On Windows `::` is replaced with `;`. |
| 350 | On Mac and linux `::` is replaced with `:`. |
| 351 | |
| 352 | ### From godoenv file |
| 353 | |
| 354 | For special circumstances where the GOPATH needs to be set before building the Gododir, |
| 355 | use `Gododir/godoenv` file. |
| 356 | |
| 357 | TIP: Create `Gododir/godoenv` when using a dependency manager like `godep` that necessitates |
| 358 | changing `$GOPATH` |
| 359 | |
| 360 | ``` |
| 361 | # Gododir/godoenv |
| 362 | GOPATH=$PWD/cmd/app/Godeps/_workspace::$GOPATH |
| 363 | ``` |