1. Miki Tebeka
  2. nrsc

Commits

Miki Tebeka  committed 43e17b5

create nrsc directory and start writing file

  • Participants
  • Parent commits 7d33f05
  • Branches default

Comments (0)

Files changed (1)

File nrsc.go

View file
 	return out
 }
 
-func writeResource(root string, file *File, out io.Writer) error {
+func writeResource(prefix int, 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:]
+	key := path[prefix:]
 	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())
 	fmt.Fprintf(out, "\t\tmtype: \"%s\",\n", mtype)
 	fmt.Fprintf(out, "\t\tdata: {")
 	for _, b := range data {
-		fmt.Printf("%d, ", b)
-		break
+		fmt.Fprintf(out, "%d, ", b)
 	}
-	fmt.Println("\t\t},\n\t},")
+	fmt.Fprintf(out, "\t\t},\n\t},\n")
 
 	return nil
 }
 	os.Exit(1)
 }
 
+func dirExists(path string) bool {
+	info, err := os.Stat(path)
+	if err == nil {
+		return info.IsDir()
+	}
+
+	return false
+}
+
 func main() {
-	root := flag.String("root", "", "resource root")
+	var root string
+
+	flag.StringVar(&root, "root", "", "root direcotry")
 	flag.Parse()
 
-	if len(*root) == 0 {
+	if len(root) == 0 {
 		die("<root> is required")
 	}
 
-	for file := range iterfiles(*root) {
-		writeResource(*root, file, os.Stdout)
+	if !dirExists(root) {
+		die("%s is not a directory", root)
 	}
+
+	if !dirExists("nrsc") {
+		if err := os.Mkdir("nrsc", 0700); err != nil {
+			die("can't create nrsc directory - %s", err)
+		}
+	}
+
+	ok := false
+
+	defer func() {
+		if !ok {
+			os.RemoveAll("nrsc")
+		}
+	}()
+
+	out, err := os.Create("nrsc/nrsc.go")
+	if err != nil {
+		die("can't create nrsc/nrsc.go - %s", err)
+	}
+
+	prefix := len(root)-1
+	for file := range iterfiles(root) {
+		if err := writeResource(prefix, file, out); err != nil {
+			die("can't write %s - %s", file.path, err)
+		}
+	}
+	ok = true
 }