-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.go
118 lines (101 loc) · 2.66 KB
/
cli.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
package maltmill
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"github.com/pkg/errors"
)
const envGitHubToken = "GITHUB_TOKEN"
var errOpt = errors.New("special option requested")
type cli struct {
outStream, errStream io.Writer
}
func (cl *cli) run(ctx context.Context, args []string) error {
log.SetOutput(cl.errStream)
log.SetPrefix("[maltmill] ")
log.SetFlags(0)
mm, err := cl.parseArgs(ctx, args)
if err != nil {
return err
}
return mm.run(ctx)
}
func (cl *cli) parseArgs(ctx context.Context, args []string) (runner, error) {
mm := &cmdMaltmill{writer: cl.outStream}
fs := flag.NewFlagSet("maltmill", flag.ContinueOnError)
fs.SetOutput(cl.errStream)
fs.Usage = func() {
fs.SetOutput(cl.outStream)
defer fs.SetOutput(cl.errStream)
fmt.Fprintf(cl.outStream, `maltmill - Update or create homebrew third party formulae
Version: %s (rev: %s/%s)
Synopsis:
%% maltmill -w [formula-files.rb]
Options:
`, version, revision, runtime.Version())
fs.PrintDefaults()
fmt.Fprintf(cl.outStream, `
Commands:
new create new formula
`)
}
var token string
fs.StringVar(&token, "token", os.Getenv(envGitHubToken), "github `token")
fs.BoolVar(&mm.overwrite, "w", false, "write result to (source) file instead of stdout")
err := fs.Parse(args)
if err != nil {
return nil, err
}
restArgs := fs.Args()
if len(restArgs) < 1 {
return nil, errors.New("no formula files or sub command are specified")
}
switch restArgs[0] {
case "new":
newArgs := []string{}
if token != "" {
newArgs = append(newArgs, "-token", token)
}
if mm.overwrite {
newArgs = append(newArgs, "-w")
}
return cl.parseCmdNewArgs(ctx, append(newArgs, restArgs[1:]...))
default:
mm.files = restArgs
mm.ghcli = newGithubClient(ctx, token)
return mm, nil
}
}
func (cl *cli) parseCmdNewArgs(ctx context.Context, args []string) (runner, error) {
cr := &cmdNew{writer: cl.outStream}
fs := flag.NewFlagSet("maltmill", flag.ContinueOnError)
fs.SetOutput(cl.errStream)
fs.Usage = func() {
fs.SetOutput(cl.outStream)
defer fs.SetOutput(cl.errStream)
fmt.Fprintf(cl.outStream, `maltmill new - create new formula
Synopsis:
%% maltmill new -w Songmu/ghg
Options:
`)
fs.PrintDefaults()
}
var token string
fs.StringVar(&token, "token", os.Getenv(envGitHubToken), "github `token`")
fs.BoolVar(&cr.overwrite, "w", false, "write result to (source) file instead of stdout")
fs.StringVar(&cr.outFile, "o", "", "`file` to output")
err := fs.Parse(args)
if err != nil {
return nil, err
}
if len(fs.Args()) < 1 {
return nil, errors.New("githut repository isn't specified")
}
cr.slug = fs.Arg(0)
cr.ghcli = newGithubClient(ctx, token)
return cr, nil
}