Commits

Miki Tebeka committed e34ef0c

Passing tests

  • Participants
  • Parent commits edf003a
  • Branches gomain

Comments (0)

Files changed (3)

File nrsc.sh

-#!/bin/bash
-# Pack assets as zip payload in go executable
-
-# Idea from Carlos Castillo (http://bit.ly/SmYXXm)
-
-case "$1" in
-    -h | --help )
-        echo "usage: $(basename $0) EXECTABLE RESOURCE_DIR [ZIP OPTIONS]";
-        exit;;
-    --version )
-        echo "nrsc version 0.3.1"; exit;;
-esac
-
-if [ $# -lt 2 ]; then
-    $0 -h
-    exit 1
-fi
-
-exe=$1
-shift
-root=$1
-shift
-
-if [ ! -f "${exe}" ]; then
-    echo "error: can't find $exe"
-    exit 1
-fi
-
-if [ ! -d "${root}" ]; then
-    echo "error: ${root} is not a directory"
-    exit 1
-fi
-
-# Exit on 1'st error
-set -e
-
-tmp="/tmp/nrsc-$(date +%s).zip"
-trap "rm -f ${tmp}" EXIT
-
-# Create zip file
-(cd "${root}" && zip -r "${tmp}" . $@)
-
-# Append zip to executable
-cat "${tmp}" >> "${exe}"
-# Fix zip offset in file
-zip -q -A "${exe}"

File nrsc/nrsc.go

 	return nil
 }
 
-func appendZip(exe, zipFile string) error {
-	zipfo, err := os.Open(zipFile)
+func appendFile(dest, src string) error {
+	in, err := os.Open(src)
 	if err != nil {
 		return err
 	}
-	defer zipfo.Close()
+	defer in.Close()
 
-	exefo, err := os.OpenFile(exe, os.O_APPEND, os.ModeAppend)
+	out, err := os.OpenFile(dest, os.O_RDWR, 0)
 	if err != nil {
 		return err
 	}
-	defer exefo.Close()
+	out.Seek(0, os.SEEK_END)
+	defer out.Close()
 
-	_, err = exefo.Seek(0, os.SEEK_END)
-	if err != nil {
-		return err
-	}
-
-	_, err = io.Copy(zipfo, exefo)
+	_, err = io.Copy(out, in)
 	return err
 }
 
+
 func fixZipOffset(exe string) error {
 	return exec.Command("zip", "-q", "-A", exe).Run()
 }
 		die("error: can't create zip - %s", err)
 	}
 
-	if err := appendZip(exe, zip); err != nil {
+	if err := appendFile(exe, zip); err != nil {
 		die("error: can't append zip to %s - %s", exe, err)
 	}
 

File nrsc_test.go

 	return nil
 }
 
-func fixGOPATH(cwd string) {
-	os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
-}
-
 func init() {
 	if err := initDir(); err != nil {
 		panic(err)
 	path := func(name string) string {
 		return fmt.Sprintf("%s/%s", cwd, name)
 	}
-	fixGOPATH(cwd)
+
+	os.Chdir("nrsc")
+	cmd := exec.Command("go", "build")
+	if err := cmd.Run(); err != nil {
+		fmt.Printf("error building nrsc: %s\n", err)
+		panic(err)
+	}
 
 	os.Chdir(root)
 	defer os.Chdir(cwd)
 
-	cmd := exec.Command("go", "install", "nrsc")
-	if err := cmd.Run(); err != nil {
-		fmt.Printf("error installing: %s\n", err)
-		panic(err)
-	}
+	testExe := "nrsc-test"
 
-	cmd = exec.Command("go", "build")
+	cmd = exec.Command("go", "build", "-o", testExe)
 	if err := cmd.Run(); err != nil {
 		fmt.Printf("error building: %s\n", err)
 		panic(err)
 	}
 
-	cmd = exec.Command(path("nrsc"), "nrsc-test", path("test-resources"))
+	cmd = exec.Command(path("nrsc/nrsc"), testExe, path("test-resources"))
 	if err := cmd.Run(); err != nil {
 		fmt.Printf("error packing: %s\n", err)
 		panic(err)
 	"os"
 	"regexp"
 
-	"nrsc"
+	"bitbucket.org/tebeka/nrsc"
 )
 
 type params struct {