From c879511377bdb76615b58b212f030e82fb88d3be Mon Sep 17 00:00:00 2001 From: Pacien Date: Mon, 1 Jul 2013 12:42:28 +0200 Subject: Replace file manipulation functions by the fcmd package --- common.go | 11 +++--- context.go | 3 +- files.go | 106 --------------------------------------------------------- interactive.go | 11 +++--- main.go | 3 ++ 5 files changed, 17 insertions(+), 117 deletions(-) delete mode 100644 files.go diff --git a/common.go b/common.go index 3547af1..56bffd1 100644 --- a/common.go +++ b/common.go @@ -22,6 +22,7 @@ package main import ( "bytes" "fmt" + "github.com/Pacien/fcmd" "github.com/drbawb/mustache" "github.com/russross/blackfriday" "io/ioutil" @@ -69,7 +70,7 @@ func merge(files map[string][]byte) (merged []byte) { // render and write everything inside func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) map[string][]byte { - _, filesList := ls(dirPath) + _, filesList := fcmd.Ls(dirPath) for _, fileName := range filesList { if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { var err error @@ -93,7 +94,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s elements = parse(dirPath, elements, exts, true) if recursive { - dirs, _ := ls(dirPath) + dirs, _ := fcmd.Ls(dirPath) for _, dir := range dirs { go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) } @@ -102,7 +103,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s template := merge(elements) page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) - err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) + err := fcmd.WriteFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) if err != nil { fmt.Println(err) return @@ -117,10 +118,10 @@ func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bo return } - dirs, files := ls(dirPath) + dirs, files := fcmd.Ls(dirPath) for _, file := range files { if !isParsable(file, exts) { - err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) + err := fcmd.Cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) if err != nil { fmt.Println(err) } diff --git a/context.go b/context.go index 8b41a52..aff6a5d 100644 --- a/context.go +++ b/context.go @@ -20,6 +20,7 @@ package main import ( + "github.com/Pacien/fcmd" "path" "strings" ) @@ -48,7 +49,7 @@ func (c context) Title() string { } func (c context) SubPages() (subPages []page) { - dirs, _ := ls(c.path) + dirs, _ := fcmd.Ls(c.path) for _, dir := range dirs { var page page page.Title = dir diff --git a/files.go b/files.go deleted file mode 100644 index afdac86..0000000 --- a/files.go +++ /dev/null @@ -1,106 +0,0 @@ -/* - - This file is part of CompileTree (https://github.com/Pacien/CompileTree) - - CompileTree is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - CompileTree is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with CompileTree. If not, see . - -*/ - -package main - -import ( - "io" - "io/ioutil" - "os" - "path" - "strings" -) - -// Filesystem utils - -func isDir(dirPath string) bool { - stat, err := os.Stat(dirPath) - if err != nil { - return false - } - return stat.IsDir() -} - -func isHidden(fileName string) bool { - return strings.HasPrefix(fileName, ".") -} - -func ls(path string) (dirs []string, files []string) { - content, err := ioutil.ReadDir(path) - if err != nil { - return - } - for _, element := range content { - if isHidden(element.Name()) { - continue - } - if element.IsDir() { - dirs = append(dirs, element.Name()) - } else { - files = append(files, element.Name()) - } - } - return -} - -func explore(dirPath string) (paths []string) { - dirs, _ := ls(dirPath) - for _, dir := range dirs { - sourceDir := path.Join(dirPath, dir) - paths = append(paths, sourceDir) - subDirs := explore(sourceDir) - for _, subDir := range subDirs { - paths = append(paths, subDir) - } - } - return -} - -func cp(source, target string) error { - sourceFile, err := os.Open(source) - if err != nil { - return err - } - defer sourceFile.Close() - - dir, _ := path.Split(target) - err = os.MkdirAll(dir, 0777) - if err != nil { - return err - } - - targetFile, err := os.Create(target) - if err != nil { - return err - } - defer targetFile.Close() - - _, err = io.Copy(targetFile, sourceFile) - return err -} - -func writeFile(target string, body []byte) error { - dir, _ := path.Split(target) - err := os.MkdirAll(dir, 0777) - if err != nil { - return err - } - err = ioutil.WriteFile(target, body, 0777) - return err -} diff --git a/interactive.go b/interactive.go index 22f5933..1c9ca71 100644 --- a/interactive.go +++ b/interactive.go @@ -21,6 +21,7 @@ package main import ( "fmt" + "github.com/Pacien/fcmd" "github.com/howeyc/fsnotify" "os" "path" @@ -30,7 +31,7 @@ import ( func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { watcher.Watch(dirPath) - dirs := explore(dirPath) + dirs, _ := fcmd.Explore(dirPath) for _, dir := range dirs { if !strings.HasPrefix(dir, *settings.outputDir) { err := watcher.Watch(dir) @@ -70,7 +71,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { fmt.Println(ev) // ignore hidden files - if isHidden(ev.Name) { + if fcmd.IsHidden(ev.Name) { break } @@ -81,7 +82,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { fmt.Println(err) return } - } else if ev.IsCreate() && isDir(ev.Name) { + } else if ev.IsCreate() && fcmd.IsDir(ev.Name) { watcher = watch(ev.Name, watcher) } @@ -90,7 +91,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { // remove previously compiled files if ev.IsDelete() || ev.IsRename() || ev.IsModify() { var err error - if isDir(ev.Name) || !isParsable(ev.Name, exts) { + if fcmd.IsDir(ev.Name) || !isParsable(ev.Name, exts) { err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) } else { err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) @@ -103,7 +104,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { // recompile changed files if ev.IsCreate() || ev.IsModify() { - if isDir(ev.Name) { + if fcmd.IsDir(ev.Name) { elements := parseParents(ev.Name, sourceDir, exts) dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true) diff --git a/main.go b/main.go index 2c0cf81..9b6bf8a 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ package main import ( "flag" "fmt" + "github.com/Pacien/fcmd" "strings" ) @@ -35,6 +36,8 @@ var settings struct { } func init() { + fcmd.DefaultPerm = 0755 // -rwxr-xr-x + // read settings settings.mode = flag.String("mode", "compiled", "compiled|interactive|dynamic") settings.sourceDir = flag.String("source", ".", "Path to sources directory.") -- cgit v1.2.3