summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/GeertJohan/go.rice/rice/append.go
blob: ae3d95fd7b67e95b15cad7312860364c033e8479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main

import (
	"archive/zip"
	"fmt"
	"go/build"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"

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

func operationAppend(pkgs []*build.Package) {
	if runtime.GOOS == "windows" {
		_, err := exec.LookPath("zip")
		if err != nil {
			fmt.Println("#### WARNING ! ####")
			fmt.Println("`rice append` is known not to work under windows because the `zip` command is not available. Please let me know if you got this to work (and how).")
		}
	}

	// MARKED FOR DELETION
	// This is actually not required, the append command now has the option --exec required.
	// // check if package is a command
	// if !pkg.IsCommand() {
	// 	fmt.Println("Error: can not append to non-main package. Please follow instructions at github.com/GeertJohan/go.rice")
	// 	os.Exit(1)
	// }

	// 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.Append.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)
	}

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

	for _, pkg := range pkgs {
		// find boxes for this command
		boxMap := findBoxes(pkg)

		// notify user when no calls to rice.FindBox are made (is this an error and therefore os.Exit(1) ?
		if len(boxMap) == 0 {
			fmt.Printf("no calls to rice.FindBox() or rice.MustFindBox() found in import path `%s`\n", pkg.ImportPath)
			continue
		}

		verbosef("\n")

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

			// walk box path's and insert files
			boxPath := filepath.Clean(filepath.Join(pkg.Dir, 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)
	}

	zipA := exec.Command("zip", "-A", binfileName)
	err = zipA.Run()
	if err != nil {
		fmt.Printf("Error setting zip offset: %s\n", err)
		os.Exit(1)
	}
}