9 // Generate generates a new archive from the content provided
12 // `files` is a sequence of path/content pairs. A new file is
13 // added to the archive for each pair.
14 // If the last pair is incomplete, the file is created with an
15 // empty content. For example:
17 // Generate("foo.txt", "hello world", "emptyfile")
19 // The above call will return an archive with 2 files:
20 // * ./foo.txt with content "hello world"
21 // * ./empty with empty content
23 // FIXME: stream content instead of buffering
24 // FIXME: specify permissions and other archive metadata
25 func Generate(input ...string) (Archive, error) {
26 files := parseStringPairs(input...)
27 buf := new(bytes.Buffer)
28 tw := tar.NewWriter(buf)
29 for _, file := range files {
30 name, content := file[0], file[1]
33 Size: int64(len(content)),
35 if err := tw.WriteHeader(hdr); err != nil {
38 if _, err := tw.Write([]byte(content)); err != nil {
42 if err := tw.Close(); err != nil {
45 return ioutil.NopCloser(buf), nil
48 func parseStringPairs(input ...string) (output [][2]string) {
49 output = make([][2]string, 0, len(input)/2+1)
50 for i := 0; i < len(input); i += 2 {
56 output = append(output, pair)