Skip to content

Commit

Permalink
Merge pull request #1 from JonathanTech/AddAppendSimpleCommand
Browse files Browse the repository at this point in the history
Initial working version for append-simple
  • Loading branch information
JonathanTech authored Jun 20, 2017
2 parents c02ca9a + 3c7e780 commit 8589f73
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/example/example.exe
/rice/rice
/rice/rice.exe
/.idea

*.rice-box.go
*.rice-box.syso
Expand Down
2 changes: 1 addition & 1 deletion box.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/GeertJohan/go.rice/embedded"
"github.com/JonathanTech/go.rice/embedded"
)

// Box abstracts a directory for resources/files.
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io/ioutil"
"testing"

"github.com/GeertJohan/go.rice/embedded"
"github.com/JonathanTech/go.rice/embedded"
)

// For all test code in this package, define a set of test boxes.
Expand Down
2 changes: 1 addition & 1 deletion embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"time"

"github.com/GeertJohan/go.rice/embedded"
"github.com/JonathanTech/go.rice/embedded"
)

// re-type to make exported methods invisible to user (godoc)
Expand Down
144 changes: 144 additions & 0 deletions rice/appendSimple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/daaku/go.zipexe"
)

func operationAppendSimple(boxPaths map[string]bool) {
// create tmp zipfile
tmpZipfileName := filepath.Join(os.TempDir(), fmt.Sprintf("ricebox-%d-%s.zip", time.Now().Unix(), randomString(10)))
verbosef("Will create tmp zipfile: %s\n", tmpZipfileName)
tmpZipfile, err := os.Create(tmpZipfileName)
if err != nil {
fmt.Printf("Error creating tmp zipfile: %s\n", err)
os.Exit(1)
}
defer func() {
tmpZipfile.Close()
os.Remove(tmpZipfileName)
}()

// find abs path for binary file
binfileName, err := filepath.Abs(flags.AppendSimple.Executable)
if err != nil {
fmt.Printf("Error finding absolute path for executable to append: %s\n", err)
os.Exit(1)
}
verbosef("Will append to file: %s\n", binfileName)

// check that command doesn't already have zip appended
if rd, _ := zipexe.Open(binfileName); rd != nil {
fmt.Printf("Cannot append to already appended executable. Please remove %s and build a fresh one.\n", binfileName)
os.Exit(1)
}

// open binfile
binfile, err := os.OpenFile(binfileName, os.O_WRONLY, os.ModeAppend)
if err != nil {
fmt.Printf("Error: unable to open executable file: %s\n", err)
os.Exit(1)
}
defer binfile.Close()

binfileInfo, err := binfile.Stat()
if err != nil {
fmt.Printf("Error: unable to stat executable file: %s\n", err)
os.Exit(1)
}

// create zip.Writer
zipWriter := zip.NewWriter(tmpZipfile)

// write the zip offset into the zip data
zipWriter.SetOffset(binfileInfo.Size())

for boxname := range boxPaths {
appendedBoxName := strings.Replace(boxname, `/`, `-`, -1)

// walk box path's and insert files
boxPath := filepath.Clean(boxname)
filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error {
if info == nil {
fmt.Printf("Error: box \"%s\" not found on disk\n", path)
os.Exit(1)
}
// create zipFilename
zipFileName := filepath.Join(appendedBoxName, strings.TrimPrefix(path, boxPath))
// write directories as empty file with comment "dir"
if info.IsDir() {
_, err := zipWriter.CreateHeader(&zip.FileHeader{
Name: zipFileName,
Comment: "dir",
})
if err != nil {
fmt.Printf("Error creating dir in tmp zip: %s\n", err)
os.Exit(1)
}
return nil
}

// create zipFileWriter
zipFileHeader, err := zip.FileInfoHeader(info)
if err != nil {
fmt.Printf("Error creating zip FileHeader: %v\n", err)
os.Exit(1)
}
zipFileHeader.Name = zipFileName
zipFileWriter, err := zipWriter.CreateHeader(zipFileHeader)
if err != nil {
fmt.Printf("Error creating file in tmp zip: %s\n", err)
os.Exit(1)
}
srcFile, err := os.Open(path)
if err != nil {
fmt.Printf("Error opening file to append: %s\n", err)
os.Exit(1)
}
_, err = io.Copy(zipFileWriter, srcFile)
if err != nil {
fmt.Printf("Error copying file contents to zip: %s\n", err)
os.Exit(1)
}
srcFile.Close()

return nil
})
}
//}

err = zipWriter.Close()
if err != nil {
fmt.Printf("Error closing tmp zipfile: %s\n", err)
os.Exit(1)
}

err = tmpZipfile.Sync()
if err != nil {
fmt.Printf("Error syncing tmp zipfile: %s\n", err)
os.Exit(1)
}
_, err = tmpZipfile.Seek(0, 0)
if err != nil {
fmt.Printf("Error seeking tmp zipfile: %s\n", err)
os.Exit(1)
}
_, err = binfile.Seek(0, 2)
if err != nil {
fmt.Printf("Error seeking bin file: %s\n", err)
os.Exit(1)
}

_, err = io.Copy(binfile, tmpZipfile)
if err != nil {
fmt.Printf("Error appending zipfile to executable: %s\n", err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion rice/embed-syso.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"text/template"

"github.com/GeertJohan/go.rice/embedded"
"github.com/JonathanTech/go.rice/embedded"
"github.com/akavel/rsrc/coff"
)

Expand Down
5 changes: 5 additions & 0 deletions rice/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var flags struct {
Executable string `long:"exec" description:"Executable to append" required:"true"`
} `command:"append"`

AppendSimple struct {
BoxPath []string `long:"box-path" short:"b" description:"Box path(s) to use. Bypasses code parsing to enable simpler use of boxes. Ignores import-path. Specify multiple times for more box paths to append" required:"true"`
Executable string `long:"exec" description:"Executable to append" required:"true"`
} `command:"append-simple"`

EmbedGo struct{} `command:"embed-go" alias:"embed"`
EmbedSyso struct{} `command:"embed-syso"`
Clean struct{} `command:"clean"`
Expand Down
45 changes: 45 additions & 0 deletions rice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import (
"go/build"
"log"
"os"
"path"
)

func main() {
log.Println("Jon's version")
// parser arguments
parseArguments()

// find package for path
var pkgs []*build.Package
var boxes = make(map[string]bool)

for _, boxPath := range flags.AppendSimple.BoxPath {
boxPath, value := pathForBoxPath(boxPath)
boxes[boxPath] = value
verbosef("box path %q added and exists=%t\n", boxPath, value)
}

for _, importPath := range flags.ImportPaths {
pkg := pkgForPath(importPath)
pkgs = append(pkgs, pkg)
Expand All @@ -29,6 +39,8 @@ func main() {
for _, pkg := range pkgs {
operationEmbedSyso(pkg)
}
case "append-simple":
operationAppendSimple(boxes)
case "append":
operationAppend(pkgs)
case "clean":
Expand Down Expand Up @@ -61,6 +73,39 @@ func pkgForPath(path string) *build.Package {
return pkg
}

// helper function to get *build.Package for given path
func pathForBoxPath(boxPath string) (string,bool) {
// get pwd for relative imports
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("error getting pwd (required for relative box paths): %s\n", err)
os.Exit(1)
}

if path.IsAbs(boxPath) {
boxPath = boxPath
} else {
boxPath = path.Join(pwd, boxPath)
}

result, err := exists(boxPath)
if err != nil {
fmt.Printf("error finding box path %q : %s\n", boxPath, err)
}
return boxPath, result
}

// exists returns whether the given file or directory exists or not
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func verbosef(format string, stuff ...interface{}) {
if flags.Verbose {
log.Printf(format, stuff...)
Expand Down
2 changes: 1 addition & 1 deletion virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"sort"

"github.com/GeertJohan/go.rice/embedded"
"github.com/JonathanTech/go.rice/embedded"
)

//++ TODO: IDEA: merge virtualFile and virtualDir, this decreases work done by rice.File
Expand Down

0 comments on commit 8589f73

Please sign in to comment.