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
A way to enforce required variables #1204
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
93f25e3
Adds new field to tasks for required vars
benc-uk 6ccd0b9
adds docs for `requires` keyword
benc-uk d254cd4
Better docs and checking logic
benc-uk 6d4e08b
Merge branch 'main' into add-required-vars
benc-uk 1777379
Add custom error and code
benc-uk 8a34dd1
More concise go style if
benc-uk ab9d10f
Fix quoting in error message
benc-uk 84b30ea
Fix comment on TaskMissingRequiredVars
benc-uk fc08a6b
No longer return bool, just error
benc-uk 2b44beb
Put vars under requires
benc-uk 11daf4e
Fix panic
benc-uk 4dac567
Docs update
benc-uk 2ddcc6c
Schema update for requires
benc-uk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package task | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-task/task/v3/errors" | ||
"github.com/go-task/task/v3/taskfile" | ||
) | ||
|
||
func (e *Executor) areTaskRequiredVarsSet(ctx context.Context, t *taskfile.Task, call taskfile.Call) error { | ||
if t.Requires == nil || len(t.Requires.Vars) == 0 { | ||
return nil | ||
} | ||
|
||
vars, err := e.Compiler.GetVariables(t, call) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var missingVars []string | ||
for _, requiredVar := range t.Requires.Vars { | ||
if !vars.Exists(requiredVar) { | ||
missingVars = append(missingVars, requiredVar) | ||
} | ||
} | ||
|
||
if len(missingVars) > 0 { | ||
return &errors.TaskMissingRequiredVars{ | ||
TaskName: t.Name(), | ||
MissingVars: missingVars, | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package taskfile | ||
|
||
import "github.com/go-task/task/v3/internal/deepcopy" | ||
|
||
// Requires represents a set of required variables necessary for a task to run | ||
type Requires struct { | ||
Vars []string | ||
} | ||
|
||
func (r *Requires) DeepCopy() *Requires { | ||
if r == nil { | ||
return nil | ||
} | ||
|
||
return &Requires{ | ||
Vars: deepcopy.Slice(r.Vars), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note some reformatting crept in when I saved the file due to my VSCode settings, but the changes looked pretty sensible So I kept them