feat: add -dryrun mode
#533
Open
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.
Details
What
This PR adds a
-dryrunflag to Mage. When dryrun mode is active, Mage will print the commands it would have executed (if not for dryrun mode).Why
While
-vand-debugalready exist, they both still result in the actual commands being executed; in some situations, one may want to verify the correctness of one's magefile by just seeing what would be running.How
The main "wrinkle" as far as implementation goes is the fact that the same lines of code in mage/main.go are used to compile the magefile into a binary as are used within that binary to execute other commands. (While there are other lines used to execute commands in Mage - notably, in the
shpackage - those don't have this "dual issue" property.)As a result, an indiscriminate dryrun mode would simply echo the magefile-compilation command lines, never compile the magefile into an actual binary, and thus never print the "actual" command lines (from the user's perspective) that the magefile should execute - nullifying any usefulness of the feature.
To resolve this problem, the implementation uses the conjunction (i.e., logical
AND) of two elements to determine dryrun. One is, well, whether-dryrunhas been specified; but the other is whether the environment variableMAGEFILE_DRYRUN_POSSIBLEhas been set. In the "outer" call to Mage, the latter variable will be unset, so that even if dryrun mode was requested, the result of the logicalANDwill be false. But, as part of this outer run, Mage sets this environment variable; meaning in any "inner" runs, the result of the logicalANDwill depend purely on whether the-dryrunflag or its correspondingMAGEFILE_DRYRUNenvironment variable (set inRunCompiled(...), just like the other Mage-related environment variables) are in effect.The net result is that
-dryrunhas no effect on the magefile compilation, but works as desired on the command-lines issued by the compiled magefile.GitHub Issue
Merging this PR would close issue #532