mirror of https://github.com/docker/docs.git
Merge pull request #87 from docker/remove_append_cl
removing single file append changelist
This commit is contained in:
commit
af7bc19c76
|
@ -1,89 +1,5 @@
|
||||||
package changelist
|
package changelist
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AppendChangelist represents a list of TUF changes
|
|
||||||
type AppendChangelist struct {
|
|
||||||
path string
|
|
||||||
file *os.File
|
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAppendChangelist is a convinience method that returns an append only TUF
|
|
||||||
// change list
|
|
||||||
func NewAppendChangelist(path string) (*AppendChangelist, error) {
|
|
||||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &AppendChangelist{
|
|
||||||
path: path,
|
|
||||||
file: file,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// List returns a list of Changes
|
|
||||||
func (cl *AppendChangelist) List() []Change {
|
|
||||||
cl.file.Seek(0, 0) // seek to start of file
|
|
||||||
var changes []Change
|
|
||||||
scnr := bufio.NewScanner(cl.file)
|
|
||||||
for scnr.Scan() {
|
|
||||||
line := scnr.Bytes()
|
|
||||||
c := &TufChange{}
|
|
||||||
err := json.Unmarshal(line, c)
|
|
||||||
if err != nil {
|
|
||||||
// TODO(david): How should we handle this?
|
|
||||||
logrus.Warn(err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
changes = append(changes, c)
|
|
||||||
}
|
|
||||||
return changes
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add adds a change to the append only changelist
|
|
||||||
func (cl *AppendChangelist) Add(c Change) error {
|
|
||||||
cl.file.Seek(0, 2) // seek to end of file
|
|
||||||
entry, err := json.Marshal(c)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
n, err := cl.file.Write(entry)
|
|
||||||
if err != nil {
|
|
||||||
if n > 0 {
|
|
||||||
// trim partial write if necessary
|
|
||||||
size, _ := cl.file.Seek(-int64(n), 2)
|
|
||||||
cl.file.Truncate(size)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cl.file.Write([]byte("\n"))
|
|
||||||
cl.file.Sync()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear empties the changelist file. It does not currently
|
|
||||||
// support archiving
|
|
||||||
func (cl *AppendChangelist) Clear(archive string) error {
|
|
||||||
cl.file.Seek(0, 0) // seek to start
|
|
||||||
cl.file.Truncate(0) // truncate
|
|
||||||
cl.file.Sync()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close marks the change list as closed
|
|
||||||
func (cl *AppendChangelist) Close() error {
|
|
||||||
cl.file.Sync()
|
|
||||||
cl.closed = true
|
|
||||||
return cl.file.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// memChangeList implements a simple in memory change list.
|
// memChangeList implements a simple in memory change list.
|
||||||
type memChangelist struct {
|
type memChangelist struct {
|
||||||
changes []Change
|
changes []Change
|
||||||
|
|
|
@ -1,27 +1,17 @@
|
||||||
package changelist
|
package changelist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFileChangelist(t *testing.T) {
|
func TestMemChangelist(t *testing.T) {
|
||||||
tmpDir, err := ioutil.TempDir("/tmp", "test")
|
cl := memChangelist{}
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
file := path.Join(tmpDir, "list")
|
|
||||||
cl, err := NewAppendChangelist(file)
|
|
||||||
assert.Nil(t, err, "Error initializing appendChangelist")
|
|
||||||
|
|
||||||
c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1})
|
c := NewTufChange(ActionCreate, "targets", "target", "test/targ", []byte{1})
|
||||||
|
|
||||||
err = cl.Add(c)
|
err := cl.Add(c)
|
||||||
assert.Nil(t, err, "Non-nil error while adding change")
|
assert.Nil(t, err, "Non-nil error while adding change")
|
||||||
|
|
||||||
cs := cl.List()
|
cs := cl.List()
|
||||||
|
|
Loading…
Reference in New Issue