-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
153 lines (128 loc) · 3.12 KB
/
plugin.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package bluffalo
import (
"context"
"flag"
"fmt"
"sort"
"github.com/markbates/bluffalo/fauxplugs/cmdx"
)
// Plugin is the most basic interface a plugin can implement.
type Plugin interface {
// Name is the name of the plugin.
// This will also be used for the cli sub-command
// "pop" | "heroku" | "auth" | etc...
Name() string
}
type Plugins []Plugin
// Fixer is an optional interface a plugin can implement
// to be run with `bluffalo fix`. This should update the application
// to the current version of the plugin.
// The expectation is fixing of only one major revision.
type Fixer interface {
Fix(ctx context.Context, args []string) error
}
// Fixer is an optional interface a plugin can implement
// to be run with `bluffalo fix`
type Generator interface {
Generate(ctx context.Context, args []string) error
}
// Fix runs any Fixers that are in the Plugins.
// If no arguments are provided it will run all fixers in the Plugins.
// Otherwise Fix will run the fixers for the arguments provided.
// bluffalo fix
// bluffalo fix plush pop
// bluffalo fix -h
func (plugs Plugins) Fix(ctx context.Context, args []string) error {
opts := struct {
help bool
}{}
flags := flag.NewFlagSet("bluffalo fix", flag.ContinueOnError)
flags.BoolVar(&opts.help, "h", false, "print this help")
if err := flags.Parse(args); err != nil {
return err
}
args = flags.Args()
stderr := cmdx.Stderr(ctx)
if opts.help {
sort.Slice(plugs, func(i, j int) bool {
return plugs[i].Name() < plugs[j].Name()
})
for _, p := range plugs {
if _, ok := p.(Fixer); ok {
fmt.Fprintf(stderr, "%s %s - [%s]\n", flags.Name(), p.Name(), p)
}
}
return nil
}
if len(args) > 0 {
fixers := map[string]Fixer{}
for _, p := range plugs {
f, ok := p.(Fixer)
if !ok {
continue
}
fixers[p.Name()] = f
}
for _, a := range args {
f, ok := fixers[a]
if !ok {
return fmt.Errorf("unknown fixer %s", a)
}
if err := f.Fix(ctx, []string{}); err != nil {
return err
}
}
return nil
}
for _, p := range plugs {
f, ok := p.(Fixer)
if !ok {
continue
}
if err := f.Fix(ctx, args); err != nil {
return err
}
}
return nil
}
// Generate will run the specified generator.
// bluffalo generate -h
// bluffalo generate pop ...
func (plugs Plugins) Generate(ctx context.Context, args []string) error {
opts := struct {
help bool
}{}
flags := flag.NewFlagSet("bluffalo generate", flag.ContinueOnError)
flags.BoolVar(&opts.help, "h", false, "print this help")
if err := flags.Parse(args); err != nil {
return err
}
args = flags.Args()
if opts.help || len(args) == 0 {
sort.Slice(plugs, func(i, j int) bool {
return plugs[i].Name() < plugs[j].Name()
})
stderr := cmdx.Stderr(ctx)
for _, p := range plugs {
if _, ok := p.(Generator); ok {
fmt.Fprintf(stderr, "%s %s - [%s]\n", flags.Name(), p.Name(), p)
}
}
return nil
}
arg := args[0]
if len(args) > 0 {
args = args[1:]
}
for _, p := range plugs {
f, ok := p.(Generator)
if !ok {
continue
}
if p.Name() != arg {
continue
}
return f.Generate(ctx, args)
}
return fmt.Errorf("unknown generator %s", arg)
}