-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathblueprint_list.go
More file actions
104 lines (79 loc) · 2.29 KB
/
blueprint_list.go
File metadata and controls
104 lines (79 loc) · 2.29 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
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
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
"github.com/sourcegraph/src-cli/internal/blueprint"
)
func init() {
usage := `
Examples:
List blueprints from the default community repository:
$ src blueprint list
List blueprints from a GitHub repository:
$ src blueprint list -repo https://github.com/org/blueprints
List blueprints from a specific branch or tag:
$ src blueprint list -repo https://github.com/org/blueprints -rev v1.0.0
List blueprints from a local directory:
$ src blueprint list -repo ./my-blueprints
Print JSON description of all blueprints:
$ src blueprint list -f '{{.|json}}'
List just blueprint names and subdirs:
$ src blueprint list -f '{{.Subdir}}: {{.Name}}'
`
flagSet := flag.NewFlagSet("list", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src blueprint %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
repoFlag = flagSet.String("repo", defaultBlueprintRepo, "Repository URL (HTTPS) or local path to blueprints")
revFlag = flagSet.String("rev", "", "Git revision, branch, or tag to checkout (ignored for local paths)")
formatFlag = flagSet.String("f", "{{.Title}}\t{{.Summary}}\t{{.Subdir}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.|json}}")`)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
tmpl, err := parseTemplate(*formatFlag)
if err != nil {
return err
}
src, err := blueprint.ResolveRootSource(*repoFlag, *revFlag)
if err != nil {
return err
}
rootDir, cleanup, err := src.Prepare(context.Background())
if cleanup != nil {
defer func() { _ = cleanup() }()
}
if err != nil {
return err
}
found, err := blueprint.FindBlueprints(rootDir)
if err != nil {
return err
}
for _, bp := range found {
subdir, _ := filepath.Rel(rootDir, bp.Dir)
if subdir == "." {
subdir = ""
}
data := struct {
*blueprint.Blueprint
Subdir string
}{bp, subdir}
if err := execTemplate(tmpl, data); err != nil {
return err
}
}
return nil
}
blueprintCommands = append(blueprintCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}