Add -rm flag to remove target output file (#151)
File is removed before mock generation, if it exists. This is useful when the mock generation is likely to fail due to some reason, for example, package load failure due to change in the interface being mocked (the existing mock is no longer valid and cannot be compiled). In such cases, the -rm flag can be used instead of manually removing the file (which could have fixed the issue).
This commit is contained in:
parent
b4465d5f96
commit
ab5b7bc086
10
main.go
10
main.go
@ -22,6 +22,7 @@ type userFlags struct {
|
|||||||
formatter string
|
formatter string
|
||||||
stubImpl bool
|
stubImpl bool
|
||||||
skipEnsure bool
|
skipEnsure bool
|
||||||
|
remove bool
|
||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,6 +36,7 @@ func main() {
|
|||||||
printVersion := flag.Bool("version", false, "show the version for moq")
|
printVersion := flag.Bool("version", false, "show the version for moq")
|
||||||
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
|
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
|
||||||
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
|
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
|
||||||
|
flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists")
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
|
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
|
||||||
@ -63,6 +65,14 @@ func run(flags userFlags) error {
|
|||||||
return errors.New("not enough arguments")
|
return errors.New("not enough arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flags.remove && flags.outFile != "" {
|
||||||
|
if err := os.Remove(flags.outFile); err != nil {
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var out io.Writer = os.Stdout
|
var out io.Writer = os.Stdout
|
||||||
if flags.outFile != "" {
|
if flags.outFile != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user