-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add exclude path feature #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ var ( | |
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl") | ||
licensef = flag.String("f", "", "license file") | ||
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)") | ||
exclude = flag.String("e", "", "Files which exclude from check") | ||
verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified") | ||
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing") | ||
) | ||
|
@@ -140,8 +141,10 @@ func main() { | |
} | ||
}() | ||
|
||
excludePath := NewExcludePaths(*exclude) | ||
|
||
for _, d := range flag.Args() { | ||
walk(ch, d) | ||
walk(ch, d, excludePath) | ||
} | ||
close(ch) | ||
<-done | ||
|
@@ -152,7 +155,7 @@ type file struct { | |
mode os.FileMode | ||
} | ||
|
||
func walk(ch chan<- *file, start string) { | ||
func walk(ch chan<- *file, start string, excludePath *ExcludePath) { | ||
filepath.Walk(start, func(path string, fi os.FileInfo, err error) error { | ||
if err != nil { | ||
log.Printf("%s error: %v", path, err) | ||
|
@@ -161,11 +164,34 @@ func walk(ch chan<- *file, start string) { | |
if fi.IsDir() { | ||
return nil | ||
} | ||
if excludePath.Contains(path) { | ||
log.Printf("%s skipped", path) | ||
return nil | ||
} | ||
ch <- &file{path, fi.Mode()} | ||
return nil | ||
}) | ||
} | ||
|
||
type ExcludePath struct { | ||
paths []string | ||
} | ||
|
||
func NewExcludePaths(arg string) *ExcludePath { | ||
return &ExcludePath{ | ||
paths: strings.Split(arg, ","), | ||
} | ||
} | ||
|
||
func (e *ExcludePath) Contains(path string) bool { | ||
for _, p := range e.paths { | ||
if p == path { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this work if someone specifies a relative path, e.g. /foo/bar, ./foo, ../foo/bar? |
||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// addLicense add a license to the file if missing. | ||
// | ||
// It returns true if the file was updated. | ||
|
@@ -272,6 +298,7 @@ func hashBang(b []byte) []byte { | |
|
||
// go generate: ^// Code generated .* DO NOT EDIT\.$ | ||
var goGenerated = regexp.MustCompile(`(?m)^.{1,2} Code generated .* DO NOT EDIT\.$`) | ||
|
||
// cargo raze: ^DO NOT EDIT! Replaced on runs of cargo-raze$ | ||
var cargoRazeGenerated = regexp.MustCompile(`(?m)^DO NOT EDIT! Replaced on runs of cargo-raze$`) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,3 +206,33 @@ func TestMPL(t *testing.T) { | |
t.Fatalf("%v\n%s", err, out) | ||
} | ||
} | ||
|
||
func TestExcludePath(t *testing.T) { | ||
if os.Getenv("RUNME") != "" { | ||
main() | ||
return | ||
} | ||
|
||
tmp := tempDir(t) | ||
t.Logf("tmp dir: %s", tmp) | ||
|
||
origin1 := filepath.Join(tmp, "file.c") | ||
origin2 := filepath.Join(tmp, "file.h") | ||
expected1 := "testdata/initial/file.c" | ||
expected2 := "testdata/expected/file.h" | ||
|
||
run(t, "cp", "testdata/initial/file.c", origin1) | ||
run(t, "cp", "testdata/initial/file.h", origin2) | ||
|
||
cmd := exec.Command(os.Args[0], | ||
"-test.run=TestExcludePath", | ||
"-y", "2018", "-e", origin1, origin1, origin2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two questions: 1) this is excluding a file. Isn't the more likely use case to exclude an entire dir tree? If so, could you add a test for that case? 2) Is the two appearances of origin1 a mistake or because you want to verify the logic handles duplicate paths? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be beneficial to support directories: the main use case (and ours!) is dev teams removing test folders from the path. Test folders have a number of files that change over time, so it's cumbersome to update the command every time a new file is added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree - I think dirs is likely to be a far more valuable use case than enumerating individual files. If you do a prefix match then you get both, dirs and files, with a one line change. |
||
) | ||
cmd.Env = []string{"RUNME=1"} | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
t.Fatalf("%v\n%s", err, out) | ||
} | ||
|
||
run(t, "diff", origin1, expected1) | ||
run(t, "diff", origin2, expected2) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Files to exclude from check" seems more accurate