split main1 with a func returning an error

pull/22/head
Daniel Martí 5 years ago
parent 2ad7593b06
commit 0cc6e36414

@ -98,14 +98,20 @@ func main1() int {
if len(args) < 1 { if len(args) < 1 {
flagSet.Usage() flagSet.Usage()
} }
if err := mainErr(args); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
func mainErr(args []string) error {
// If we recognise an argument, we're not running within -toolexec. // If we recognise an argument, we're not running within -toolexec.
switch args[0] { switch args[0] {
case "build": case "build":
execPath, err := os.Executable() execPath, err := os.Executable()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) return err
return 1
} }
goArgs := []string{ goArgs := []string{
"build", "build",
@ -118,11 +124,7 @@ func main1() int {
cmd := exec.Command("go", goArgs...) cmd := exec.Command("go", goArgs...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { return cmd.Run()
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
} }
_, tool := filepath.Split(args[0]) _, tool := filepath.Split(args[0])
@ -131,16 +133,14 @@ func main1() int {
} }
transform, ok := transformFuncs[tool] transform, ok := transformFuncs[tool]
if !ok { if !ok {
fmt.Fprintf(os.Stderr, "unknown tool: %q", tool) return fmt.Errorf("unknown tool: %q", tool)
return 1
} }
transformed := args[1:] transformed := args[1:]
// log.Println(tool, transformed) // log.Println(tool, transformed)
if transform != nil { if transform != nil {
var err error var err error
if transformed, err = transform(transformed); err != nil { if transformed, err = transform(transformed); err != nil {
fmt.Fprintln(os.Stderr, err) return err
return 1
} }
} }
defer func() { defer func() {
@ -154,10 +154,9 @@ func main1() int {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err) return err
return 1
} }
return 0 return nil
} }
var transformFuncs = map[string]func([]string) ([]string, error){ var transformFuncs = map[string]func([]string) ([]string, error){

Loading…
Cancel
Save