This repository was archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmagefile.go
More file actions
59 lines (47 loc) · 1.41 KB
/
magefile.go
File metadata and controls
59 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:build mage
// +build mage
package main
import (
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"os"
"text/tabwriter"
"github.com/cloudfoundry/go-cf-api/internal/helpers"
)
//#######################//
// General Mage Commands //
//#######################//
// Prints this helptext for available commands
func Help() error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug|tabwriter.DiscardEmptyColumns)
pack := getPackage("./magefile.go")
fmt.Fprintf(w, "----- General Commands, defined in %v -----\n", pack.Filenames[0])
printHelpText(pack, w)
fmt.Fprintln(w)
pack = getPackage("./magefile-ops.go")
fmt.Fprintf(w, "----- Commands for Operators, defined in %v -----\n", pack.Filenames[0])
printHelpText(pack, w)
fmt.Fprintln(w)
pack = getPackage("./magefile-dev.go")
fmt.Fprintf(w, "----- Commands for Developers, defined in %v -----\n", pack.Filenames[0])
printHelpText(pack, w)
w.Flush()
return nil
}
func getPackage(path string) *doc.Package {
fset := token.NewFileSet() // positions are relative to fset
obj, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
helpers.CheckErrFatal(err)
objFile := []*ast.File{obj}
pack, err := doc.NewFromFiles(fset, objFile, path, doc.AllMethods)
helpers.CheckErrFatal(err)
return pack
}
func printHelpText(pack *doc.Package, w *tabwriter.Writer) {
for _, f := range pack.Funcs {
fmt.Fprintf(w, "%v \t %v", f.Name, f.Doc)
}
}