cmd/wire: add a --header_file flag to the "gen" and "diff" commands (#175)

This commit is contained in:
Robert van Gent
2019-05-16 09:56:42 -07:00
committed by GitHub
parent d76a979091
commit fe01654174
10 changed files with 133 additions and 11 deletions

View File

@@ -83,7 +83,23 @@ func packages(f *flag.FlagSet) []string {
return pkgs
}
type genCmd struct{}
// newGenerateOptions returns an initialized wire.GenerateOptions, possibly
// with the Header option set.
func newGenerateOptions(headerFile string) (*wire.GenerateOptions, error) {
opts := new(wire.GenerateOptions)
if headerFile != "" {
var err error
opts.Header, err = ioutil.ReadFile(headerFile)
if err != nil {
return nil, fmt.Errorf("failed to read header file %q: %v", headerFile, err)
}
}
return opts, nil
}
type genCmd struct {
headerFile string
}
func (*genCmd) Name() string { return "gen" }
func (*genCmd) Synopsis() string {
@@ -97,14 +113,22 @@ func (*genCmd) Usage() string {
If no packages are listed, it defaults to ".".
`
}
func (*genCmd) SetFlags(_ *flag.FlagSet) {}
func (*genCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
func (cmd *genCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.headerFile, "header_file", "", "path to file to insert as a header in wire_gen.go")
}
func (cmd *genCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
wd, err := os.Getwd()
if err != nil {
log.Println("failed to get working directory: ", err)
return subcommands.ExitFailure
}
outs, errs := wire.Generate(ctx, wd, os.Environ(), packages(f))
opts, err := newGenerateOptions(cmd.headerFile)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
outs, errs := wire.Generate(ctx, wd, os.Environ(), packages(f), opts)
if len(errs) > 0 {
logErrors(errs)
log.Println("generate failed")
@@ -138,7 +162,9 @@ func (*genCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}
return subcommands.ExitSuccess
}
type diffCmd struct{}
type diffCmd struct {
headerFile string
}
func (*diffCmd) Name() string { return "diff" }
func (*diffCmd) Synopsis() string {
@@ -156,8 +182,10 @@ func (*diffCmd) Usage() string {
plus an error if trouble.
`
}
func (*diffCmd) SetFlags(_ *flag.FlagSet) {}
func (*diffCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
func (cmd *diffCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.headerFile, "header_file", "", "path to file to insert as a header in wire_gen.go")
}
func (cmd *diffCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
const (
errReturn = subcommands.ExitStatus(2)
diffReturn = subcommands.ExitStatus(1)
@@ -167,7 +195,12 @@ func (*diffCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{
log.Println("failed to get working directory: ", err)
return errReturn
}
outs, errs := wire.Generate(ctx, wd, os.Environ(), packages(f))
opts, err := newGenerateOptions(cmd.headerFile)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
outs, errs := wire.Generate(ctx, wd, os.Environ(), packages(f), opts)
if len(errs) > 0 {
logErrors(errs)
log.Println("generate failed")