Skip to content
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 prefix match exclude feature #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Usage:
//
// gocmt [-i] [-p] [-t "comment template"] [-d dir]
// gocmt [-i] [-p] [-t "comment template"] [-d dir] [-e exclude_dirs]
//
// This tools exists because I have to work with some existed code base, which
// is lack of documentation for many exported identifiers. Iterating over them
Expand Down
9 changes: 9 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"go/scanner"
"os"
"strings"
Expand All @@ -17,6 +18,14 @@ func printError(err error) {

func walkFunc(path string, fi os.FileInfo, err error) error {
if err == nil && isGoFile(fi) {
// exclude path
for _, e := range excludeDirs {
if strings.HasPrefix(path, e) {
fmt.Fprintf(os.Stdout, "ignore %s\n", path)
return nil
}
}

err = processFile(path, *template, *inPlace)
}

Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ var (
defaultMode = os.FileMode(0644)
tralingWsRegex = regexp.MustCompile(`(?m)[\t ]+$`)
newlinesRegex = regexp.MustCompile(`(?m)\n{3,}`)
excludeDirs []string
)

var (
inPlace = flag.Bool("i", false, "Make in-place editing")
template = flag.String("t", "...", "Comment template")
dir = flag.String("d", "", "Directory to process")
parenComment = flag.Bool("p", false, "Add comments to all const inside the parens if true")
exclude = flag.String("e", "",
"Path to exclude(only support prefix match), such as 'pkg/xxx/', 'pkg/xxx/,pkg/yyy/', and so on")
)

func main() {
Expand All @@ -43,6 +46,10 @@ func usage() {
func gocmtRun() int {
flag.Parse()

if *exclude != "" {
excludeDirs = strings.Split(*exclude, ",")
}

if *dir != "" {
if err := filepath.Walk(*dir, walkFunc); err != nil {
printError(err)
Expand Down
2 changes: 2 additions & 0 deletions testdata/pkg/xxx/xxx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// auto generate, should ignore
package xxx
1 change: 1 addition & 0 deletions testdata/pkg/yyy/yyy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package yyy