diff --git a/imp.go b/imp.go index 8d90abe..672efd7 100644 --- a/imp.go +++ b/imp.go @@ -16,7 +16,7 @@ import ( ) func runImp(root, ignore string, write, gofumpt, gofumptExtra bool) error { - mod, err := modInfo() + mod, err := modInfo(root) if err != nil { return err } diff --git a/main.go b/main.go index 929bb33..b2f6e20 100644 --- a/main.go +++ b/main.go @@ -29,11 +29,16 @@ func mainErr() error { stdinFlag := fs.Bool("stdin", false, "Format stdin") if err := fs.Parse(os.Args[1:]); err != nil { - fmt.Println(err) + return err + } + + cwd, err := os.Getwd() + if err != nil { + return err } if *stdinFlag { - mod, err := modInfo() + mod, err := modInfo(cwd) if err != nil { return err } @@ -46,9 +51,10 @@ func mainErr() error { return err } fmt.Print(string(formatted)) + return nil } - root := "." + root := cwd if fs.NArg() > 0 { root = fs.Arg(0) } @@ -79,8 +85,9 @@ func (m *module) UnmarshalJSON(data []byte) error { return nil } -func modInfo() (module, error) { +func modInfo(dir string) (module, error) { cmd := exec.Command("go", "mod", "edit", "--json") + cmd.Dir = dir out, err := cmd.Output() if err != nil { return module{}, err