-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (116 loc) 路 4.42 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"github.com/peterbourgon/ff/v3/ffcli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/srand"
"moul.io/zapconfig"
)
func main() {
if err := run(os.Args[1:]); err != nil {
if err != flag.ErrHelp {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}
os.Exit(1)
}
}
type projectOpts struct {
CheckoutMainBranch bool
Fetch bool
ShowDiff bool
OpenPR bool
Reset bool
}
type Opts struct {
Verbose bool
Path string
Maintenance struct {
Project projectOpts
BumpDeps bool
Standard bool
}
TemplatePostClone struct {
Project projectOpts
RemoveGoBinary bool
TemplateName string
TemplateOwner string
}
Doctor struct{}
Info struct{}
Version struct{}
}
var (
rootFs = flag.NewFlagSet("<root>", flag.ExitOnError)
doctorFs = flag.NewFlagSet("doctor", flag.ExitOnError)
infoFs = flag.NewFlagSet("doctor", flag.ExitOnError)
maintenanceFs = flag.NewFlagSet("maintenance", flag.ExitOnError)
versionFs = flag.NewFlagSet("version", flag.ExitOnError)
templatePostCloneFs = flag.NewFlagSet("template-post-clone", flag.ExitOnError)
assetsConfigFs = flag.NewFlagSet("assets-config", flag.ExitOnError)
opts Opts
logger *zap.Logger
)
func run(args []string) error {
rand.Seed(srand.Fast())
// setup flags
{
setupProjectFlags := func(fs *flag.FlagSet, opts *projectOpts) {
fs.BoolVar(&opts.CheckoutMainBranch, "checkout-main-branch", true, "switch to the main branch before applying the changes")
fs.BoolVar(&opts.Fetch, "fetch", true, "fetch origin before applying the changes")
fs.BoolVar(&opts.ShowDiff, "show-diff", true, "display git diff of the changes")
fs.BoolVar(&opts.OpenPR, "open-pr", true, "open a new pull-request with the changes")
fs.BoolVar(&opts.Reset, "reset", false, "reset dirty worktree before applying the changes")
}
rootFs.BoolVar(&opts.Verbose, "v", false, "verbose mode")
setupProjectFlags(templatePostCloneFs, &opts.TemplatePostClone.Project)
templatePostCloneFs.StringVar(&opts.TemplatePostClone.TemplateName, "template-name", "golang-repo-template", "template's name (to change with the new project's name)")
templatePostCloneFs.StringVar(&opts.TemplatePostClone.TemplateOwner, "template-owner", "moul", "template owner's name (to change with the new owner)")
templatePostCloneFs.BoolVar(&opts.TemplatePostClone.RemoveGoBinary, "rm-go-binary", false, "whether to delete everything related to go binary and only keep a library")
setupProjectFlags(maintenanceFs, &opts.Maintenance.Project)
maintenanceFs.BoolVar(&opts.Maintenance.BumpDeps, "bump-deps", false, "bump dependencies")
maintenanceFs.BoolVar(&opts.Maintenance.Standard, "std", true, "standard maintenance tasks")
}
root := &ffcli.Command{
Name: "repoman",
FlagSet: rootFs,
ShortUsage: "repoman <subcommand>",
Subcommands: []*ffcli.Command{
{Name: "info", Exec: doInfo, FlagSet: infoFs, ShortHelp: "get project info", ShortUsage: "info [opts] <path...>"},
{Name: "doctor", Exec: doDoctor, FlagSet: doctorFs, ShortHelp: "perform various checks (read-only)", ShortUsage: "doctor [opts] <path...>"},
{Name: "maintenance", Exec: doMaintenance, FlagSet: maintenanceFs, ShortHelp: "perform various maintenance tasks (write)", ShortUsage: "maintenance [opts] <path...>"},
{Name: "version", Exec: doVersion, FlagSet: versionFs, ShortHelp: "show version and build info", ShortUsage: "version"},
{Name: "template-post-clone", Exec: doTemplatePostClone, FlagSet: templatePostCloneFs, ShortHelp: "replace template", ShortUsage: "template-post-clone [opts] <path...>"},
{Name: "assets-config", Exec: doAssetsConfig, FlagSet: assetsConfigFs, ShortHelp: "generate a configuration for assets", ShortUsage: "assets-config [opts] <path...>"},
},
Exec: func(ctx context.Context, args []string) error {
return flag.ErrHelp
},
}
if err := root.Parse(args); err != nil {
return fmt.Errorf("parse error: %w", err)
}
// init logger
{
var err error
config := zapconfig.New().
SetPreset("light-console")
if opts.Verbose {
config = config.SetLevel(zapcore.DebugLevel)
} else {
config = config.SetLevel(zapcore.InfoLevel)
}
logger, err = config.Build()
if err != nil {
return err
}
}
if err := root.Run(context.Background()); err != nil {
return fmt.Errorf("run error: %w", err)
}
return nil
}