Skip to content

Commit

Permalink
Add goimports flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Jun 14, 2021
1 parent 1d3b255 commit 0f4ed82
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ Depending on how you prefer to structure your code, you can either

The following flags are defined by the binary.

| Name | Short Flag | Description |
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------- |
| package | p | The name of the generated package. Is the name of target directory if dirname or filename is supplied by default. |
| prefix | | A prefix used in the name of each mock struct. Should be TitleCase by convention. |
| interfaces | i | A whitelist of interfaces to generate given the import paths. |
| filename | o | The target output file. All mocks are written to this file. |
| dirname | d | The target output directory. Each mock will be written to a unique file. |
| force | f | Do not abort if a write to disk would overwrite an existing file. |
| disable-formatting | | Do not run goimports over the rendered files. Formatting is enabled by default and requires `goimports` to be on the PATH. |
| Name | Short Flag | Description |
| ------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------- |
| package | p | The name of the generated package. Is the name of target directory if dirname or filename is supplied by default. |
| prefix | | A prefix used in the name of each mock struct. Should be TitleCase by convention. |
| interfaces | i | A whitelist of interfaces to generate given the import paths. |
| filename | o | The target output file. All mocks are written to this file. |
| dirname | d | The target output directory. Each mock will be written to a unique file. |
| force | f | Do not abort if a write to disk would overwrite an existing file. |
| disable-formatting | | Do not run goimports over the rendered files (enabled by default). |
| goimports | | Path to the goimports binary (uses goimports on your PATH by default). |

If neither dirname nor filename are supplied, then the generated code is written to a unique file in the current directory.

Expand Down
1 change: 1 addition & 0 deletions cmd/go-mockgen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func parseArgs() (*generation.Options, error) {
app.Flag("prefix", "A prefix used in the name of each mock struct. Should be TitleCase by convention.").StringVar(&opts.Prefix)
app.Flag("force", "Do not abort if a write to disk would overwrite an existing file.").Short('f').BoolVar(&opts.Force)
app.Flag("disable-formatting", "Do not run goimports over the rendered files.").BoolVar(&opts.DisableFormatting)
app.Flag("goimports", "Path to the goimports binary.").Default("goimports").StringVar(&opts.GoImportsBinary)

if _, err := app.Parse(os.Args[1:]); err != nil {
return nil, err
Expand Down
18 changes: 17 additions & 1 deletion cmd/go-mockgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,26 @@ func init() {

func main() {
if err := mainErr(); err != nil {
log.Fatalf("error: %s\n", err.Error())
message := fmt.Sprintf("error: %s\n", err.Error())

if solvableError, ok := err.(solvableError); ok {
message += "\nPossible solutions:\n"

for _, hint := range solvableError.Solutions() {
message += fmt.Sprintf(" - %s\n", hint)
}

message += "\n"
}

log.Fatalf(message)
}
}

type solvableError interface {
Solutions() []string
}

func mainErr() error {
opts, err := parseArgs()
if err != nil {
Expand Down
19 changes: 17 additions & 2 deletions internal/mockgen/generation/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ func generateAndRender(ifaces []*types.Interface, filename string, opts *Options
}

if !opts.DisableFormatting {
if err := exec.Command("goimports", "-w", filename).Run(); err != nil {
return fmt.Errorf("failed format file (install goimports on your PATH or run go-mockgen with --disable-formatting): %s", err)
if err := exec.Command(opts.GoImportsBinary, "-w", filename).Run(); err != nil {
return errorWithSolutions{
err: fmt.Errorf("failed to format file: %s", err),
solutions: []string{
"install goimports on your PATH",
"specify a non-standard path to a goimports binary via --goimports",
"disable post-render formatting via --disable-formatting",
},
}
}
}

Expand All @@ -109,3 +116,11 @@ func generateContent(ifaces []*types.Interface, pkgName, prefix, outputImportPat

return buffer.String(), nil
}

type errorWithSolutions struct {
err error
solutions []string
}

func (e errorWithSolutions) Error() string { return e.err.Error() }
func (e errorWithSolutions) Solutions() []string { return e.solutions }
1 change: 1 addition & 0 deletions internal/mockgen/generation/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Options struct {
Prefix string
Force bool
DisableFormatting bool
GoImportsBinary string
}

0 comments on commit 0f4ed82

Please sign in to comment.