-
Notifications
You must be signed in to change notification settings - Fork 256
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
Feature/context for jobs #32
Conversation
This commit adds helper functions that should be commonly needed by many magefiles. It includes command execution handlers as well as error handlers. This commit also removes the only two non-stdlib dependencies in the codebase. Since mage will likely be vendored into a repo, it would be nice to avoid cluttering the vendor directory with dependencies that are only for the build script.
Moved magefile template to its own file
gah, sorry for the conflicts. |
Ya, I saw it coming from that other PR. Still working out handling timeouts locally. |
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.
Ran out of time to do a proper review, will do more tomorow
example/magefile.go
Outdated
|
||
func Timeout(ctx context.Context) { | ||
fmt.Printf("timeout start\n") | ||
time.Sleep(1000000000000) |
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.
time.sleep takes a time.Duration, you could make this easier to read by just doing time.Sleep(time.Hour) or something :)
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 does and "oops!" did not intend to commit that... I'll remove it. I use that for my manual test cases.
mage/main.go
Outdated
@@ -35,8 +35,8 @@ const initFile = "magefile.go" | |||
|
|||
var ( | |||
force, verbose, list, help, mageInit, keep, showVersion bool | |||
|
|||
timestamp, commitHash, gitTag string | |||
timeout int |
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 can put spaces between the lines os the types aren't so crazily right justified :)
bde64d9
to
8ca6529
Compare
Removed template defered recover in preference to the runTarget func that does the same. Fixed many, but not all, gofmt errors in the template
This goes as far as I can take it right now.
|
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.
Looks great, a few minor nitpicks
example/magefile.go
Outdated
// Manage your deps, or running package managers. | ||
func InstallDeps(ctx context.Context) error { | ||
fmt.Println("Installing Deps...") | ||
cmd := exec.Command("go", "get", "github.com/stretchr/piglatin") |
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.
there's a version of exec.Command that takes a context, that would be a perfect example for using this new feature: https://golang.org/pkg/os/exec/#CommandContext
mage/main.go
Outdated
@@ -46,6 +46,7 @@ func init() { | |||
flag.BoolVar(&help, "h", false, "show this help") | |||
flag.BoolVar(&mageInit, "init", false, "create a starting template if no mage files exist") | |||
flag.BoolVar(&keep, "keep", false, "keep intermediate mage files around after running") | |||
flag.IntVar(&timeout, "t", 0, "timeout in seconds, Default (0) no timeout") |
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.
if you make this a flag.DurationVar, it'll convert a string right to a duration, and then you can do -t 1m5s
for example
.vscode/settings.json
Outdated
@@ -0,0 +1,3 @@ | |||
// Place your settings in this file to overwrite default and user settings. | |||
{ |
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 probably be .gitignored
mage/main.go
Outdated
@@ -40,6 +40,8 @@ const mainfile = "mage_output_file.go" | |||
const initFile = "magefile.go" | |||
|
|||
var ( | |||
force, verbose, list, help, mageInit, keep, showVersion bool |
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 can get removed.... with switch to a standalone parse function, we don't have global flag variables anymore.
mage/main_test.go
Outdated
@@ -31,7 +31,11 @@ func testmain(m *testing.M) int { | |||
log.Fatal(err) | |||
} | |||
if err := os.Mkdir(dir, 0700); err != nil { | |||
log.Fatal(err) | |||
if os.IsExist(err) { | |||
os.RemoveAll(fmt.Sprintf("%s/*", dir)) |
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.
RemoveAll removes a directory. globbing (*) doesn't generally work in go functions anyway. if you want to just delete everything inside the directory, you'll either have to loop through, or better, just delete the whole thing and recreate it.
mg/deps.go
Outdated
fn = f | ||
default: | ||
panic("attempted to add a dep that did not match required type") |
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.
can you slap a
// should be impossible, since we already checked this
above this?
mg/runtime.go
Outdated
var ctx context.Context | ||
var ctxCancel func() | ||
|
||
func GetContext() (context.Context, func()) { |
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.
Can you call this just Context()? GetFoo is sorta non-idiomatic.
mg/runtime.go
Outdated
if os.Getenv("MAGEFILE_TIMEOUT") != "" { | ||
timeout, err := strconv.Atoi(os.Getenv("MAGEFILE_TIMEOUT")) | ||
if err != nil { | ||
fmt.Println("timeout must be a number >= 0") |
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.
speaking of which, check for negative numbers here
parse/parse.go
Outdated
Comment string | ||
} | ||
|
||
func (f Function) TemplateString() 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.
doc comment here please
parse/parse_test.go
Outdated
} | ||
|
||
// DefaultName | ||
if strings.Compare(info.DefaultName, "ReturnsError") != 0 { |
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.
if info.DefaultName != "ReturnsError" {
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.
two tiny tweaks and we're home free!
mage/main.go
Outdated
@@ -368,7 +367,7 @@ func RunCompiled(inv Invocation, exePath string) int { | |||
c.Env = append(c.Env, "MAGEFILE_HELP=1") | |||
} | |||
if inv.Timeout > 0 { | |||
c.Env = append(c.Env, fmt.Sprintf("MAGEFILE_TIMEOUT=%d", inv.Timeout)) | |||
c.Env = append(c.Env, fmt.Sprintf("MAGEFILE_TIMEOUT=%dns", inv.Timeout)) |
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 can probably just do inv.Timeout.String()
mage/main_test.go
Outdated
@@ -32,7 +33,7 @@ func testmain(m *testing.M) int { | |||
} | |||
if err := os.Mkdir(dir, 0700); err != nil { | |||
if os.IsExist(err) { | |||
os.RemoveAll(fmt.Sprintf("%s/*", dir)) | |||
os.RemoveAll(fmt.Sprintf("%s", dir)) |
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.
can remove the sprintf now :)
Allows for jobs that take and call with a context as described in #25
Todo:
closes #25