Skip to content

Commit

Permalink
Added "register" task component
Browse files Browse the repository at this point in the history
- uses command component, save it under teh string used in register
- updated user docks
- closes kcmerrill#27
  • Loading branch information
kcmerrill committed Mar 4, 2017
1 parent 2ad3faa commit 7071c3d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
26 changes: 26 additions & 0 deletions RTFM.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ User Guide
* [Tasks](#tasks-1)
* [Modules](#modules)
* [Summary](#summary)
* [Register](#register)
* [Test](#test)
* [Retry](#retry)
* [Command](#command)
Expand Down Expand Up @@ -347,6 +348,31 @@ my.simple.task:
summary: I will echo out everytime I'm run!
```

## Register
A string representation of a string variable to be stored for later use by other tasks. The variable that is stored is the stderr/stdout from `command` component.

```yaml
fourty.one:
summary: Register a variable
register: my.new.var
command: |
echo "5678"
fourty.two:
setup: fourty.one
summary: Retrieve a registered variable
command: |
echo The variable is {{ index .Vars "my.new.var"}}
```
Example use cases:
- Use to store changing information per alfred run, but needed throughout all tasks
A few things to note:
- Either stderr or stdout will be saved
- If you define `alfred.vars`, be careful the names you choose, as you _can_ overwrite a previously defined variable.


## Test
A shell command that should return a proper exit code before continuing. Should it fail, the task will fail and will continue on to the `fail` tasks, then to `exit` or `skip` depending on whatever was set.

Expand Down
7 changes: 7 additions & 0 deletions alfred/alfred.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ func (a *Alfred) runTask(task string, args []string, formatted bool) bool {
say(task, copyOfTask.Summary)
}

// Register task output
if copyOfTask.Register != "" && copyOfTask.Command != "" {
a.Vars[copyOfTask.Register] = copyOfTask.Exec(copyOfTask.Command)
// No need to continue on ... return
return true
}

// Test ...
if copyOfTask.TestF(copyOfTask.Test) {
// Lets execute the command if it has one, and add retry logic
Expand Down
13 changes: 12 additions & 1 deletion alfred/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ A brief explination:
- Watch: A regular expression of changed files
- Setup: Similiar to tasks but these get run _before_ the command/task group gets called
- Serve: A string, denoting port number to serve a static webserver
- Register: A string, runs "command" component and stores it in a string
*/

// Task contains a task definition and all of it's components
Expand Down Expand Up @@ -76,6 +77,7 @@ type Task struct {
Retry int
Watch string
Serve string
Register string
}

// TaskDefinition defines the name and params of a task when originally in string format
Expand Down Expand Up @@ -268,13 +270,22 @@ func (t *Task) TestF(tst string) bool {

// Eval runs a string to see if it's a command or not(depending on it's exit code)
func (t *Task) Eval(cmd string) string {
out, err := exec.Command(cmd).Output()
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
return cmd
}
return string(out)
}

// Exec runs the command, and sends the output back, either stdin or stdout
func (t *Task) Exec(cmd string) string {
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
return string(err.Error())
}
return string(out)
}

// Prepare will setup a bunch of things, including templates and argument defeaults
func (t *Task) Prepare(args []string, vars map[string]string) bool {
t.Args = t.Defaults
Expand Down
9 changes: 9 additions & 0 deletions alfred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ func TestTaskMultiArguments(t *testing.T) {
}
}

func TestRegisteringVariables(t *testing.T) {
sut, _ := run("alfred fourty.two", t)

if !strings.Contains(sut, "The variable is 5678") {
t.Logf("Expecting 5678 to be returned")
t.FailNow()
}
}

func TestExample(t *testing.T) {
sut, _ := run("alfred", t)
if len(sut) <= 0 {
Expand Down
12 changes: 12 additions & 0 deletions examples/demo-everything/.alfred/part-two.alfred.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ fourty:
summary: Takes two args, tests the () functionality
command: |
echo -{{ index .Args 0 }}-{{ index .Args 1 }}-
fourty.one:
summary: Register a variable
register: my.new.var
command: |
echo "5678"
fourty.two:
setup: fourty.one
summary: Retrieve a registered variable
command: |
echo The variable is {{ index .Vars "my.new.var"}}

0 comments on commit 7071c3d

Please sign in to comment.