/
fmt.go
89 lines (75 loc) · 2.04 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fmtcmd implements the ``go fmt'' command.
package fmtcmd
import (
"os"
"path/filepath"
"runtime"
"sync"
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/str"
)
func init() {
base.AddBuildFlagsNX(&CmdFmt.Flag)
}
var CmdFmt = &base.Command{
Run: runFmt,
UsageLine: "fmt [-n] [-x] [packages]",
Short: "run gofmt on package sources",
Long: `
Fmt runs the command 'gofmt -l -w' on the packages named
by the import paths. It prints the names of the files that are modified.
For more about gofmt, see 'go doc cmd/gofmt'.
For more about specifying packages, see 'go help packages'.
The -n flag prints commands that would be executed.
The -x flag prints commands as they are executed.
To run gofmt with specific options, run gofmt itself.
See also: go fix, go vet.
`,
}
func runFmt(cmd *base.Command, args []string) {
gofmt := gofmtPath()
procs := runtime.GOMAXPROCS(0)
var wg sync.WaitGroup
wg.Add(procs)
fileC := make(chan string, 2*procs)
for i := 0; i < procs; i++ {
go func() {
defer wg.Done()
for file := range fileC {
base.Run(str.StringList(gofmt, "-l", "-w", file))
}
}()
}
for _, pkg := range load.Packages(args) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
for _, file := range files {
fileC <- file
}
}
close(fileC)
wg.Wait()
}
func gofmtPath() string {
gofmt := "gofmt"
if base.ToolIsWindows {
gofmt += base.ToolWindowsExtension
}
gofmtPath := filepath.Join(cfg.GOBIN, gofmt)
if _, err := os.Stat(gofmtPath); err == nil {
return gofmtPath
}
gofmtPath = filepath.Join(cfg.GOROOT, "bin", gofmt)
if _, err := os.Stat(gofmtPath); err == nil {
return gofmtPath
}
// fallback to looking for gofmt in $PATH
return "gofmt"
}