-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluffalo.go
83 lines (71 loc) · 1.36 KB
/
bluffalo.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
package bluffalo
import (
"context"
"flag"
"fmt"
"io"
"github.com/markbates/bluffalo/fauxplugs/cmdx"
)
// Bluffalo represents the `bluffalo` cli.
type Bluffalo struct {
context.Context
flags *flag.FlagSet
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Plugins Plugins
version bool
help bool
}
func New(ctx context.Context) (*Bluffalo, error) {
b := &Bluffalo{
Context: ctx,
Stdin: cmdx.Stdin(ctx),
Stdout: cmdx.Stdout(ctx),
Stderr: cmdx.Stderr(ctx),
}
b.setFlags()
return b, nil
}
func (b *Bluffalo) Flags() *flag.FlagSet {
if b.flags == nil {
b.setFlags()
}
return b.flags
}
func (b *Bluffalo) setFlags() {
b.flags = flag.NewFlagSet("bluffalo", flag.ContinueOnError)
b.flags.BoolVar(&b.version, "v", false, "display version")
b.flags.BoolVar(&b.help, "h", false, "display help")
cmdx.Usage(b.Context, b.flags)
}
func (b *Bluffalo) Main(ctx context.Context, args []string) error {
flags := b.Flags()
if err := flags.Parse(args); err != nil {
return err
}
args = flags.Args()
if len(args) == 0 {
flags.Usage()
return nil
}
arg := args[0]
if len(args) > 0 {
args = args[1:]
}
switch arg {
case "fix":
return b.Plugins.Fix(ctx, args)
case "generate":
return b.Plugins.Generate(ctx, args)
}
if b.version {
fmt.Fprintln(b.Stdout, "ssh")
return nil
}
if b.help {
flags.Usage()
return nil
}
return nil
}