-
-
Notifications
You must be signed in to change notification settings - Fork 745
Variable handling #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
variableHandling.go
Outdated
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
yaml "gopkg.in/yaml.v2" |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
variableHandling.go
Outdated
|
||
var ( | ||
// VariableFilePath file containing additional variables | ||
VariableFilePath = "Variables" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Taskvars
?
There was a problem hiding this comment.
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}}'" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setvar
instead of set
?
There was a problem hiding this comment.
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.
There was a problem hiding this 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}}'" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Use of variables ( task local, variable file and environment ) in Taskfiles