Skip to content

Commit d674cfa

Browse files
authored
minor cleanup (#22)
* some minor cleanup
1 parent 84e8285 commit d674cfa

File tree

6 files changed

+47
-29
lines changed

6 files changed

+47
-29
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ run mage.go <target>` and it'll work just as if you ran `mage <target>`
211211
package main
212212

213213
import (
214+
"os"
214215
"github.com/magefile/mage/mage"
215216
)
216217

217-
func main() { mage.Main() }
218+
func main() { os.Exit(mage.Main()) }
218219
```
219220

220221
Note that because of the peculiarities of `go run`, if you run this way, go run

mage/main.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const mageVer = "v0.3"
2828
var output = template.Must(template.New("").Funcs(map[string]interface{}{
2929
"lower": strings.ToLower,
3030
}).Parse(tpl))
31-
var mageTemplateOutput = template.Must(template.New("").Parse(mageTpl))
31+
var initOutput = template.Must(template.New("").Parse(mageTpl))
3232

3333
const mainfile = "mage_output_file.go"
34-
const mageTemplate = "magefile.go"
34+
const initFile = "magefile.go"
3535

3636
var (
3737
force, verbose, list, help, mageInit bool
@@ -54,39 +54,48 @@ func init() {
5454
// Main is the entrypoint for running mage. It exists external to mage's main
5555
// function to allow it to be used from other programs, specifically so you can
5656
// go run a simple file that run's mage's Main.
57-
func Main() {
57+
func Main() int {
5858
log.SetFlags(0)
5959
flag.Parse()
6060
if help && len(flag.Args()) == 0 {
6161
flag.Usage()
62-
return
62+
return 0
6363
}
6464

65-
files := magefiles()
65+
files, err := magefiles()
66+
if err != nil {
67+
fmt.Println(err)
68+
return 1
69+
}
6670
if len(files) == 0 && mageInit {
6771
if err := generateInit(); err != nil {
68-
log.Fatalf("%+v", err)
72+
log.Printf("%+v", err)
73+
return 1
6974
}
70-
files = magefiles()
75+
log.Println(initFile, "created")
76+
return 0
7177
} else if len(files) == 0 {
72-
log.Fatal("No files marked with the mage build tag in this directory.")
78+
log.Print("No .go files marked with the mage build tag in this directory.")
79+
return 1
7380
}
7481

7582
out, err := ExeName(files)
7683

7784
if err != nil {
78-
log.Fatalf("%+v", err)
85+
log.Printf("%+v", err)
86+
return 1
7987
}
8088

8189
if !force {
8290
if _, err := os.Stat(out); err == nil {
83-
os.Exit(run(out, flag.Args()...))
91+
return run(out, flag.Args()...)
8492
}
8593
}
8694

8795
info, err := parse.Package(".", files)
8896
if err != nil {
89-
log.Fatalf("%v", err)
97+
log.Printf("%v", err)
98+
return 1
9099
}
91100

92101
names := map[string][]string{}
@@ -111,9 +120,10 @@ func Main() {
111120
}
112121

113122
if err := compile(out, info, files); err != nil {
114-
log.Fatal(err)
123+
log.Print(err)
124+
return 1
115125
}
116-
os.Exit(run(out, flag.Args()...))
126+
return run(out, flag.Args()...)
117127
}
118128

119129
type data struct {
@@ -122,18 +132,18 @@ type data struct {
122132
Default string
123133
}
124134

125-
func magefiles() []string {
135+
func magefiles() ([]string, error) {
126136
ctx := build.Default
127137
ctx.RequiredTags = []string{"mage"}
128138
ctx.BuildTags = []string{"mage"}
129139
p, err := ctx.ImportDir(".", 0)
130140
if err != nil {
131141
if _, ok := err.(*build.NoGoError); ok {
132-
return p.GoFiles
142+
return []string{}, nil
133143
}
134-
log.Fatalf("%+v", err)
144+
return nil, err
135145
}
136-
return p.GoFiles
146+
return p.GoFiles, nil
137147
}
138148

139149
func compile(out string, info *parse.PkgInfo, gofiles []string) error {
@@ -226,13 +236,13 @@ func confDir() string {
226236
}
227237

228238
func generateInit() error {
229-
f, err := os.Create(mageTemplate)
239+
f, err := os.Create(initFile)
230240
if err != nil {
231241
return errors.WithMessage(err, "could not create mage template")
232242
}
233243
defer f.Close()
234244

235-
if err := mageTemplateOutput.Execute(f, nil); err != nil {
245+
if err := initOutput.Execute(f, nil); err != nil {
236246
return errors.WithMessage(err, "can't execute magefile template")
237247
}
238248

mage/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ package main
116116
117117
import (
118118
"fmt"
119-
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
120119
"os"
121120
"os/exec"
121+
122+
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
122123
)
123124
124125
// Default target to run when none is specified
@@ -152,5 +153,4 @@ func Clean() {
152153
fmt.Println("Cleaning...")
153154
os.RemoveAll("MyApp")
154155
}
155-
156156
`

mage/testdata/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
package main
44

5-
import "github.com/magefile/mage/mage"
5+
import (
6+
"os"
7+
8+
"github.com/magefile/mage/mage"
9+
)
610

711
func main() {
8-
mage.Main()
12+
os.Exit(mage.Main())
913
}

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package main
22

3-
import "github.com/magefile/mage/mage"
3+
import (
4+
"os"
5+
6+
"github.com/magefile/mage/mage"
7+
)
48

59
func main() {
6-
mage.Main()
10+
os.Exit(mage.Main())
711
}

parse/parse.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func Package(path string, files []string) (*PkgInfo, error) {
6363
}
6464
}
6565

66-
getDefault(p, pi, info)
66+
setDefault(p, pi, info)
6767

6868
return pi, nil
6969
}
7070

71-
func getDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
71+
func setDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
7272
for _, v := range p.Vars {
7373
for x, name := range v.Names {
7474
if name != "Default" {
@@ -92,7 +92,6 @@ func getDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
9292
log.Println("warning: default declaration does not reference a mage target")
9393
}
9494
}
95-
log.Println("no default target found")
9695
}
9796

9897
// getPackage returns the non-test package at the given path.

0 commit comments

Comments
 (0)