Commits

Miki Tebeka committed 3e1e105

writeResource

Comments (0)

Files changed (1)

 import (
 	"flag"
 	"fmt"
+	"io"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
+	"mime"
 )
 
 var ignoredDirs = map[string]bool{
 	".git": true,
 }
 
-type Path struct {
+type File struct {
 	path string
 	info os.FileInfo
 }
 
-
-func iterfiles(root string) chan *Path {
-	out := make(chan *Path)
+func iterfiles(root string) chan *File {
+	out := make(chan *File)
 
 	walkfn := func(path string, info os.FileInfo, err error) error {
 		if info.IsDir() {
 			}
 		} else {
 			if info.Name()[0] != '.' {
-				out <- &Path{path, info}
+				out <- &File{path, info}
 			}
 		}
 		return nil
 	return out
 }
 
+func writeResource(root string, file *File, out io.Writer) error {
+	data, err := ioutil.ReadFile(file.path)
+	if err != nil {
+		return err
+	}
+	path, info := file.path, file.info
+
+	key := path[len(root)-1:]
+	fmt.Fprintf(out, "\t\"%s\": &resource{\n", key)
+	fmt.Fprintf(out, "\t\tsize: %d,\n", info.Size())
+	fmt.Fprintf(out, "\t\tmtime: time.Unix(%d, 0),\n", info.ModTime().Unix())
+	mtype := mime.TypeByExtension(filepath.Ext(path))
+	fmt.Fprintf(out, "\t\tmtype: \"%s\",\n", mtype)
+	fmt.Fprintf(out, "\t\tdata: {")
+	for _, b := range data {
+		fmt.Printf("%d, ", b)
+		break
+	}
+	fmt.Println("\t\t},\n\t},")
+
+	return nil
+}
+
+func die(format string, args... interface{}) {
+	message := fmt.Sprintf(format, args...)
+	fmt.Fprintf(os.Stderr, "error: %s\n", message)
+	os.Exit(1)
+}
+
 func main() {
 	root := flag.String("root", "", "resource root")
 	flag.Parse()
 
 	if len(*root) == 0 {
-		fmt.Fprint(os.Stderr, "error: <root> is required\n")
-		os.Exit(1)
+		die("<root> is required")
 	}
 
-	for path := range iterfiles(*root) {
-		fmt.Println(path.path)
+	for file := range iterfiles(*root) {
+		writeResource(*root, file, os.Stdout)
 	}
-
 }