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 two new flags for glob support #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 41 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ type Applier interface {

// TagContext keeps context info for Applier
type TagContext struct {
ppath string
excludeList []string
excludeDirs []string
excludeFiles []string
templatePath string
templateFiles TemplateFiles
dryRun bool
Expand All @@ -44,6 +47,8 @@ type TemplateFiles struct {
func main() {
ppath := flag.String("path", ".", "project path")
excludes := flag.String("excludes", "vendor", "exclude folders")
excludedirs := flag.String("exclude-dirs", ".git .svn vendor", "exclude dirs matching with glob patterns")
excludefiles := flag.String("exclude-files", "LICENSE MAINTAINERS", "exclude files matching with glob patterns")
tpath := flag.String("t", "./template", "template files path")
dryRun := flag.Bool("check", false, "check files missing header")
verbose := flag.Bool("v", false, "verbose output")
Expand Down Expand Up @@ -82,6 +87,8 @@ func main() {
}

excludeList := strings.Split(*excludes, " ")
excludeDirs := strings.Split(*excludedirs, " ")
excludeFiles := strings.Split(*excludefiles, " ")

templateFiles := TemplateFiles{
mTemplateFile: makeTFile,
Expand All @@ -90,10 +97,14 @@ func main() {
dTemplateFile: dTFile}

t := TagContext{
ppath: *ppath,
excludeList: excludeList,
excludeDirs: excludeDirs,
excludeFiles: excludeFiles,
templateFiles: templateFiles,
templatePath: *tpath,
dryRun: *dryRun}
dryRun: *dryRun,
}

if err = filepath.Walk(*ppath, t.tagFiles); err != nil {
panic(err)
Expand All @@ -118,10 +129,27 @@ func (t *TagContext) tagFiles(path string, f os.FileInfo, err error) error {
var applier Applier
processed := false

relpath, err := filepath.Rel(t.ppath, path)
if err != nil {
return err
}

if (f.Mode() & os.ModeSymlink) != 0 { // skip symlinks
return nil
}

if f.IsDir() {
for _, dir := range t.excludeDirs {
if matched, err := filepath.Match(dir, relpath); err != nil {
return err
} else {
if matched {
return filepath.SkipDir
}
}
}
}

if (f.Name() == ".git" || f.Name() == ".svn" || f.Name() == "..") && f.IsDir() {
return filepath.SkipDir
}
Expand All @@ -134,6 +162,18 @@ func (t *TagContext) tagFiles(path string, f os.FileInfo, err error) error {
}
}

if !f.IsDir() && f.Size() > 0 {
for _, file := range t.excludeFiles {
if matched, err := filepath.Match(file, relpath); err != nil {
return err
} else {
if matched {
return nil
}
}
}
}

if !f.IsDir() && f.Size() > 0 {

if f.Name() == "LICENSE" || f.Name() == "MAINTAINERS" {
Expand Down
Loading