forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
117 lines (105 loc) · 2.93 KB
/
list.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"text/template"
"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
)
var (
projectroot string
format string
formatStdin bool
jsonOutput bool
)
func init() {
registerCommand(&cmd.Command{
Name: "list",
UsageLine: `list [-s] [-f format] [-json] [packages]`,
Short: "list the packages named by the importpaths",
Long: `list lists packages.
The default output shows the package import path:
% gb list github.com/constabulary/...
github.com/constabulary/gb
github.com/constabulary/gb/cmd
github.com/constabulary/gb/cmd/gb
github.com/constabulary/gb/cmd/gb-env
github.com/constabulary/gb/cmd/gb-list
Flags:
-f
alternate format for the list, using the syntax of package template.
The default output is equivalent to -f '{{.ImportPath}}'. The struct
being passed to the template is currently an instance of gb.Package.
This structure is under active development and it'As contents are not
guarenteed to be stable.
`,
Run: list,
AddFlags: func(fs *flag.FlagSet) {
fs.StringVar(&format, "f", "{{.ImportPath}}", "format template")
fs.BoolVar(&formatStdin, "s", false, "read format from stdin")
fs.BoolVar(&jsonOutput, "json", false, "outputs json. WARNING: gb.Package structure is not stable and will change in future")
},
})
}
func list(ctx *gb.Context, args []string) error {
if formatStdin {
var formatBuffer bytes.Buffer
io.Copy(&formatBuffer, os.Stdin)
format = formatBuffer.String()
}
pkgs, err := cmd.ResolvePackages(ctx, args...)
if err != nil {
gb.Fatalf("unable to resolve: %v", err)
}
if jsonOutput {
views := make([]*PackageView, 0, len(pkgs))
for _, pkg := range pkgs {
views = append(views, NewPackageView(pkg))
}
encoder := json.NewEncoder(os.Stdout)
if err := encoder.Encode(views); err != nil {
return fmt.Errorf("Error occurred during json encoding: %v", err)
}
} else {
tmpl, err := template.New("list").Parse(format)
if err != nil {
return fmt.Errorf("unable to parse template %q: %v", format, err)
}
for _, pkg := range pkgs {
if err := tmpl.Execute(os.Stdout, pkg); err != nil {
return fmt.Errorf("unable to execute template: %v", err)
}
fmt.Fprintln(os.Stdout)
}
}
return nil
}
// PackageView represents a package shown by list command in JSON format.
// It is not stable and may be subject to change.
type PackageView struct {
Dir string
ImportPath string
Name string
Root string
GoFiles []string
Imports []string
TestGoFiles []string
TestImports []string
}
// NewPackageView creates a *PackageView from gb Package.
func NewPackageView(pkg *gb.Package) *PackageView {
return &PackageView{
Dir: pkg.Dir,
ImportPath: pkg.ImportPath,
Name: pkg.Name,
Root: pkg.Root,
GoFiles: pkg.GoFiles,
Imports: pkg.Package.Imports,
TestGoFiles: pkg.TestGoFiles,
TestImports: pkg.TestImports,
}
}