Skip to content

Commit

Permalink
Decouple ParseAndRun to allow customization of Invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 18, 2022
1 parent 300bbc8 commit e864580
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@ func (i Invocation) UsesMagefiles() bool {
// files in the given directory with the given args (do not include the command
// name in the args).
func ParseAndRun(stdout, stderr io.Writer, stdin io.Reader, args []string) int {
errlog := log.New(stderr, "", 0)
out := log.New(stdout, "", 0)
inv, cmd, err := Parse(stderr, stdout, args)
inv.Stderr = stderr
inv.Stdin = stdin
if err == flag.ErrHelp {
return 0
}
if err != nil {
errlog.Println("Error:", err)
return 2
if retcode, doExit := HandleParseError(err, stderr); doExit {
return retcode
}
inv.Stdin = stdin
return Run(inv, cmd)
}

// Run compiles and runs the mage files in invocation parsed by Parse function.
// The parameters to this function are the same as the return values of Parse.
func Run(inv Invocation, cmd Command) int {
errlog := log.New(inv.Stderr, "", 0)
out := log.New(inv.Stdout, "", 0)

switch cmd {
case Version:
Expand Down Expand Up @@ -179,6 +180,7 @@ func ParseAndRun(stdout, stderr io.Writer, stdin io.Reader, args []string) int {
// flag.ErrHelp, the calling process should exit with code 0.
func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command, err error) {
inv.Stdout = stdout
inv.Stderr = stderr
fs := flag.FlagSet{}
fs.SetOutput(stdout)

Expand Down Expand Up @@ -306,6 +308,20 @@ Options:
return inv, cmd, err
}

// HandleParseError handles errors returned by Parse function. It returns the
// return code and a boolean indicating whether the program should exit.
func HandleParseError(err error, stderr io.Writer) (int, bool) {
errlog := log.New(stderr, "", 0)
if err == flag.ErrHelp {
return 0, true
}
if err != nil {
errlog.Println("Error:", err)
return 2, true
}
return 0, false
}

const dotDirectory = "."

// Invoke runs Mage with the given arguments.
Expand Down

0 comments on commit e864580

Please sign in to comment.