Skip to content

Instantly share code, notes, and snippets.

@dpordomingo
Last active December 18, 2017 09:08
Show Gist options
  • Save dpordomingo/01c242f81174f63a4b9909c71ca77ce3 to your computer and use it in GitHub Desktop.
Save dpordomingo/01c242f81174f63a4b9909c71ca77ce3 to your computer and use it in GitHub Desktop.
Siva read-write
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"gopkg.in/src-d/go-siva.v1"
)
const testPath = "./repos/test.siva"
var testFiles = [2]file{
file{"readme.txt", "This archive contains some text files."},
file{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
}
type file struct{ Name, Body string }
func main() {
write(testPath)
read(testPath)
}
func write(path string) {
siva := newSiva(testFiles)
ioutil.WriteFile(path, siva.Bytes(), 0644)
}
func newSiva(files [2]file) *bytes.Buffer {
buf := new(bytes.Buffer)
writer := siva.NewWriter(buf)
defer writer.Close()
for _, file := range files {
append(writer, file)
}
return buf
}
func append(w siva.Writer, f file) {
hdr := &siva.Header{
Name: f.Name,
Mode: 0644,
ModTime: time.Now(),
}
w.WriteHeader(hdr)
w.Write([]byte(f.Body))
}
func read(path string) {
content, _ := ioutil.ReadFile(path)
r := siva.NewReader(bytes.NewReader(content))
i, _ := r.Index()
for _, e := range i {
fmt.Printf("\nContents of %s:\n", e.Name)
fileContent, _ := r.Get(e)
io.Copy(os.Stdout, fileContent)
fmt.Println()
}
}
package main
import (
"bytes"
"io/ioutil"
"time"
"gopkg.in/src-d/go-siva.v1"
)
const testPath = "./repos/test.siva"
var testFiles = [2]file{
file{"readme.txt", "This archive contains some text files."},
file{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
}
type file struct{ Name, Body string }
func main() {
write(testPath)
}
func write(path string) {
siva := newSiva(testFiles)
ioutil.WriteFile(path, siva.Bytes(), 0644)
}
func newSiva(files [2]file) *bytes.Buffer {
buf := new(bytes.Buffer)
writer := siva.NewWriter(buf)
defer writer.Close()
for _, file := range files {
append(writer, file)
}
return buf
}
func append(w siva.Writer, f file) {
hdr := &siva.Header{
Name: f.Name,
Mode: 0644,
ModTime: time.Now(),
}
w.WriteHeader(hdr)
w.Write([]byte(f.Body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment