func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error { opt := options if stdin { nopt := *options nopt.Fragment = true opt = &nopt } if in == nil { f, err := os.Open(filename) if err != nil { return err } defer f.Close() in = f } src, err := ioutil.ReadAll(in) if err != nil { return err } res, err := imports.Process(filename, src, opt) if err != nil { return err } if !bytes.Equal(src, res) { // formatting has changed if *list { fmt.Fprintln(out, filename) } if *write { err = ioutil.WriteFile(filename, res, 0) if err != nil { return err } } if *doDiff { data, err := diff(src, res) if err != nil { return fmt.Errorf("computing diff: %s", err) } fmt.Printf("diff %s gofmt/%s\n", filename, filename) out.Write(data) } } if !*list && !*write && !*doDiff { _, err = out.Write(res) } return err }
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error { opt := options if stdin { nopt := *options nopt.Fragment = true opt = &nopt } if in == nil { f, err := os.Open(filename) if err != nil { return err } defer f.Close() in = f } src, err := ioutil.ReadAll(in) if err != nil { return err } target := filename if *srcdir != "" { // Pretend that file is from *srcdir in order to decide // visible imports correctly. target = filepath.Join(*srcdir, filepath.Base(filename)) } res, err := imports.Process(target, src, opt) if err != nil { return err } if !bytes.Equal(src, res) { // formatting has changed if *list { fmt.Fprintln(out, filename) } if *write { err = ioutil.WriteFile(filename, res, 0) if err != nil { return err } } if *doDiff { data, err := diff(src, res) if err != nil { return fmt.Errorf("computing diff: %s", err) } fmt.Printf("diff %s gofmt/%s\n", filename, filename) out.Write(data) } } if !*list && !*write && !*doDiff { _, err = out.Write(res) } return err }