Skip to content

Conversation

sascha-andres
Copy link
Contributor

Use of variables ( task local, variable file and environment ) in Taskfiles

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Better than I'd made.

Made a few suggestions.

For future PRs, try having a dedicated branch instead of using master, to prevent all these merge commits.

task.go Outdated
if output, err = runCommand(ReplaceVariables(c, vars), ReplaceVariables(t.Dir, vars)); err != nil {
return &taskRunError{name, err}
}
fmt.Println(output)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build error by fmt not being imported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe should not print output if command set a variable? Not sure, just an idea.

if t.Set != "" {
  os.Setenv(t.Set, output)
} else {
  fmt.Println(output)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about a quiet switch, but that would imply more, I will change code accoring to your suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have already...

@@ -0,0 +1,82 @@
package task
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convention for filenames in Go is using underscores.

variableHandling.go -> variable_handling.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

"strings"

"github.com/BurntSushi/toml"
yaml "gopkg.in/yaml.v2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to alias package name.

task.go Outdated
}
}

if !Force && isTaskUpToDate(t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should run after running deps. See c655f23

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreynering Why running dependencies for a task when the task itself would not run? That was at least my assumption, as you would create side-effects that possible could change what the task would generate itself. I create an issue for discussion and commit a change running deps first.

func ReplaceVariables(initial string, variables map[string]string) string {
replaced := initial
for name, val := range variables {
replaced = strings.Replace(replaced, fmt.Sprintf("{{%s}}", name), val, -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll replace this with Go's template engine after merge.

So we can have some variables assigned by default (like OS, Architecture, etc). And will allow users to program if's and range's. Example:

acmd:
  cmds:
    - {{if eq .GOOS "windows"}}windows-command{{else}}unix-command{{end}}

But don't change that in this PR, otherwise it'll become too big.


var (
// VariableFilePath file containing additional variables
VariableFilePath = "Variables"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Taskvars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

build:
deps: [setvar]
cmds:
- echo "{{prefix}} '{{THEVAR}}'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think prefix should be PREFIX. Uppercase should be a convention for variables, just like environment variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a choice of the user instead of the tool?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, user can call it the name they want. But I changed to uppercase as a documentation convention.

setvar:
cmds:
- echo "{{PATH}}"
set: THEVAR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setvar instead of set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set is the property with the variable name. I searched for a valid name of the most simplistic task to set a variable.

@andreynering andreynering merged commit 9bffad0 into go-task:master Mar 5, 2017
Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged with few changes, like change to Go's template engine.

Thanks a lot!

build:
deps: [setvar]
cmds:
- echo "{{prefix}} '{{THEVAR}}'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, user can call it the name they want. But I changed to uppercase as a documentation convention.

Sources []string
Generates []string
Dir string
Variables map[string]string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to Vars for brevity.

@andreynering andreynering mentioned this pull request Mar 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants