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 support for scanning self defined file extensions #19

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
27 changes: 26 additions & 1 deletion cmd/ego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/benbjohnson/ego"
)

// version is set by the makefile during build.
var version string
var extensions []string

func main() {
outfile := flag.String("o", "ego.go", "output file")
pkgname := flag.String("package", "", "package name")
versionFlag := flag.Bool("version", false, "print version")
extFlag := flag.String("ext", ".ego", "all file extensions to scan,use '|' for separation")
flag.Parse()
log.SetFlags(0)

//parse extentions
exts := strings.Split(*extFlag, "|")
for _, ext := range exts {
ext = strings.TrimSpace(ext)
if len(ext) == 0 {
continue
}
if ext[0] != '.' {
ext = "." + ext
}
extensions = append(extensions, ext)
}

// If the version flag is set then print the version.
if *versionFlag {
fmt.Printf("ego v%s\n", version)
Expand Down Expand Up @@ -84,11 +100,20 @@ type visitor struct {
paths []string
}

func match_ext(ext string) bool {
for _, e := range extensions {
if e == ext {
return true
}
}
return false
}

func (v *visitor) visit(path string, info os.FileInfo, err error) error {
if info == nil {
return fmt.Errorf("file not found: %s", path)
}
if !info.IsDir() && filepath.Ext(path) == ".ego" {
if !info.IsDir() && match_ext(filepath.Ext(path)) {
v.paths = append(v.paths, path)
}
return nil
Expand Down