Skip to content
This repository
tree: f611eb7854
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 148 lines (120 sloc) 3.103 kb
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
// Copyright (c) 2012, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/soundcloud/visor

package visor

import (
"fmt"
"time"
)

// A Revision represents an application revision,
// identifiable by its `ref`.
type Revision struct {
Path
App *App
Ref string
ArchiveUrl string
}

const REVS_PATH = "revs"

// NewRevision returns a new instance of Revision.
func NewRevision(app *App, ref string, snapshot Snapshot) (rev *Revision) {
rev = &Revision{App: app, Ref: ref}
rev.Path = Path{snapshot, app.Path.Prefix(REVS_PATH, ref)}

return
}

func (r *Revision) createSnapshot(rev int64) Snapshotable {
tmp := *r
tmp.Snapshot = Snapshot{rev, r.conn}
return &tmp
}

// FastForward advances the revision in time. It returns
// a new instance of Revision with the supplied revision.
func (r *Revision) FastForward(rev int64) *Revision {
return r.Snapshot.fastForward(r, rev).(*Revision)
}

// Register registers a new Revision with the registry.
func (r *Revision) Register() (revision *Revision, err error) {
exists, _, err := r.conn.Exists(r.Path.Dir)
if err != nil {
return
}
if exists {
return nil, ErrKeyConflict
}

rev, err := r.Set("archive-url", r.ArchiveUrl)
if err != nil {
return
}
rev, err = r.Set("registered", time.Now().UTC().String())
if err != nil {
return
}

revision = r.FastForward(rev)

return
}

// Unregister unregisters a revision from the registry.
func (r *Revision) Unregister() (err error) {
return r.Del("/")
}

func (r *Revision) SetArchiveUrl(url string) (revision *Revision, err error) {
rev, err := r.Set("archive-url", url)
if err != nil {
return
}
revision = r.FastForward(rev)
return
}

func (r *Revision) String() string {
return fmt.Sprintf("Revision<%s:%s>", r.App.Name, r.Ref)
}

func (r *Revision) Inspect() string {
return fmt.Sprintf("%#v", r)
}

func GetRevision(s Snapshot, app *App, ref string) (r *Revision, err error) {
path := app.Path.Prefix(REVS_PATH, ref)
codec := new(StringCodec)

f, err := Get(s, path+"/archive-url", codec)
if err != nil {
return
}

r = &Revision{
Path: Path{s, path},
App: app,
Ref: ref,
ArchiveUrl: f.Value.(string),
}
return
}

// Revisions returns an array of all registered revisions.
func Revisions(s Snapshot) (revisions []*Revision, err error) {
apps, err := Apps(s)
if err != nil {
return
}

revisions = []*Revision{}

for i := range apps {
revs, e := AppRevisions(s, apps[i])
if e != nil {
return nil, e
}
revisions = append(revisions, revs...)
}

return
}

// AppRevisions returns an array of all registered revisions belonging
// to the given application.
func AppRevisions(s Snapshot, app *App) (revisions []*Revision, err error) {
refs, err := s.Getdir(app.Path.Prefix("revs"))
if err != nil {
return
}
revisions = make([]*Revision, len(refs))

for i := range refs {
r, e := GetRevision(s, app, refs[i])
if e != nil {
return nil, e
}

revisions[i] = r
}

return
}
Something went wrong with that request. Please try again.