diff --git a/rice/embed-go.go b/rice/embed-go.go index c5a0e9e..c188a1f 100644 --- a/rice/embed-go.go +++ b/rice/embed-go.go @@ -15,7 +15,7 @@ import ( const boxFilename = "rice-box.go" -func operationEmbedGo(pkg *build.Package) { +func operationEmbedGo(pkg *build.Package, excludePaths []string) { boxMap := findBoxes(pkg) @@ -76,44 +76,62 @@ func operationEmbedGo(pkg *build.Package) { filename = strings.Replace(filename, "\\", "/", -1) filename = strings.TrimPrefix(filename, "/") if info.IsDir() { - dirData := &dirDataType{ - Identifier: "dir" + nextIdentifier(), - FileName: filename, - ModTime: info.ModTime().Unix(), - ChildFiles: make([]*fileDataType, 0), - ChildDirs: make([]*dirDataType, 0), + includeDir := true + for _, excludePath := range excludePaths { + if strings.Contains(filename, excludePath) { + includeDir = false + break + } } - verbosef("\tincludes dir: '%s'\n", dirData.FileName) - box.Dirs[dirData.FileName] = dirData - - // add tree entry (skip for root, it'll create a recursion) - if dirData.FileName != "" { - pathParts := strings.Split(dirData.FileName, "/") - parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")] - parentDir.ChildDirs = append(parentDir.ChildDirs, dirData) + if includeDir { + dirData := &dirDataType{ + Identifier: "dir" + nextIdentifier(), + FileName: filename, + ModTime: info.ModTime().Unix(), + ChildFiles: make([]*fileDataType, 0), + ChildDirs: make([]*dirDataType, 0), + } + verbosef("\tincludes dir: '%s'\n", dirData.FileName) + box.Dirs[dirData.FileName] = dirData + + // add tree entry (skip for root, it'll create a recursion) + if dirData.FileName != "" { + pathParts := strings.Split(dirData.FileName, "/") + parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")] + parentDir.ChildDirs = append(parentDir.ChildDirs, dirData) + } } } else { - fileData := &fileDataType{ - Identifier: "file" + nextIdentifier(), - FileName: filename, - ModTime: info.ModTime().Unix(), + includeFile := true + for _, excludePath := range excludePaths { + if strings.Contains(filename, excludePath) { + includeFile = false + break + } } - verbosef("\tincludes file: '%s'\n", fileData.FileName) - fileData.Content, err = ioutil.ReadFile(path) - if err != nil { - fmt.Printf("error reading file content while walking box: %s\n", err) - os.Exit(1) - } - box.Files = append(box.Files, fileData) - - // add tree entry - pathParts := strings.Split(fileData.FileName, "/") - parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")] - if parentDir == nil { - fmt.Printf("Error: parent of %s is not within the box\n", path) - os.Exit(1) + if includeFile { + fileData := &fileDataType{ + Identifier: "file" + nextIdentifier(), + FileName: filename, + ModTime: info.ModTime().Unix(), + } + verbosef("\tincludes file: '%s'\n", fileData.FileName) + fileData.Content, err = ioutil.ReadFile(path) + if err != nil { + fmt.Printf("error reading file content while walking box: %s\n", err) + os.Exit(1) + } + box.Files = append(box.Files, fileData) + + // add tree entry + pathParts := strings.Split(fileData.FileName, "/") + parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")] + if parentDir == nil { + fmt.Printf("Error: parent of %s is not within the box\n", path) + os.Exit(1) + } + parentDir.ChildFiles = append(parentDir.ChildFiles, fileData) } - parentDir.ChildFiles = append(parentDir.ChildFiles, fileData) } return nil }) diff --git a/rice/find_test.go b/rice/find_test.go index dcf5036..c282b56 100644 --- a/rice/find_test.go +++ b/rice/find_test.go @@ -190,7 +190,7 @@ func FindBox(s string) { func LoadBoxes() { rice := fakerice{} rice.FindBox("foo") - + FindBox("bar") } `), @@ -232,7 +232,7 @@ func FindBox(s string) { func LoadBoxes1() { rice := fakerice{} rice.FindBox("foo") - + FindBox("bar") } `), diff --git a/rice/flags.go b/rice/flags.go index 167fea8..7891315 100644 --- a/rice/flags.go +++ b/rice/flags.go @@ -11,7 +11,8 @@ import ( // flags var flags struct { Verbose bool `long:"verbose" short:"v" description:"Show verbose debug information"` - ImportPaths []string `long:"import-path" short:"i" description:"Import path(s) to use. Using PWD when left empty. Specify multiple times for more import paths to append"` + ImportPaths []string `long:"import-path" short:"i" description:"Import path(s) to use. Using PWD when left empty. Specify multiple times for more import paths to append."` + ExcludePaths []string `long:"exclude-path" short:"e" description:"Exclude path(s). Specify multiple times to exclude paths to append."` Append struct { Executable string `long:"exec" description:"Executable to append" required:"true"` diff --git a/rice/main.go b/rice/main.go index 7bac5fa..a1ab465 100644 --- a/rice/main.go +++ b/rice/main.go @@ -22,7 +22,7 @@ func main() { switch flagsParser.Active.Name { case "embed", "embed-go": for _, pkg := range pkgs { - operationEmbedGo(pkg) + operationEmbedGo(pkg, flags.ExcludePaths) } case "embed-syso": log.Println("WARNING: embedding .syso is experimental..") diff --git a/rice/templates.go b/rice/templates.go index 02561ca..f95a055 100644 --- a/rice/templates.go +++ b/rice/templates.go @@ -26,7 +26,7 @@ func init() { {{range .Files}}{{.Identifier}} := &embedded.EmbeddedFile{ Filename: ` + "`" + `{{.FileName}}` + "`" + `, FileModTime: time.Unix({{.ModTime}}, 0), - Content: string({{.Content | printf "%q"}}), + Content: string({{.Content | printf "%q"}}), } {{end}}