refactoring toward multi-binary project layout

This commit is contained in:
Brad Rydzewski 2015-05-17 11:10:43 -07:00
parent 2e9786c68e
commit 4c847296d4
35 changed files with 91 additions and 91 deletions

View file

@ -13,8 +13,8 @@ import (
"github.com/drone/drone/settings"
"github.com/elazarl/go-bindata-assetfs"
store "github.com/drone/drone/datastore/builtin"
eventbus "github.com/drone/drone/eventbus/builtin"
eventbus "github.com/drone/drone/pkg/bus/builtin"
store "github.com/drone/drone/pkg/store/builtin"
queue "github.com/drone/drone/queue/builtin"
runner "github.com/drone/drone/runner/builtin"

View file

@ -3,26 +3,26 @@ package builtin
import (
"sync"
"github.com/drone/drone/eventbus"
"github.com/drone/drone/pkg/bus"
)
type EventBus struct {
type Bus struct {
sync.Mutex
subs map[chan *eventbus.Event]bool
subs map[chan *bus.Event]bool
}
// New creates a new EventBus that manages a list of
// New creates a new Bus that manages a list of
// subscribers to which events are published.
func New() *EventBus {
return &EventBus{
subs: make(map[chan *eventbus.Event]bool),
func New() *Bus {
return &Bus{
subs: make(map[chan *bus.Event]bool),
}
}
// Subscribe adds the channel to the list of
// subscribers. Each subscriber in the list will
// receive broadcast events.
func (b *EventBus) Subscribe(c chan *eventbus.Event) {
func (b *Bus) Subscribe(c chan *bus.Event) {
b.Lock()
b.subs[c] = true
b.Unlock()
@ -30,19 +30,19 @@ func (b *EventBus) Subscribe(c chan *eventbus.Event) {
// Unsubscribe removes the channel from the
// list of subscribers.
func (b *EventBus) Unsubscribe(c chan *eventbus.Event) {
func (b *Bus) Unsubscribe(c chan *bus.Event) {
b.Lock()
delete(b.subs, c)
b.Unlock()
}
// Send dispatches a message to all subscribers.
func (b *EventBus) Send(event *eventbus.Event) {
func (b *Bus) Send(event *bus.Event) {
b.Lock()
defer b.Unlock()
for s := range b.subs {
go func(c chan *eventbus.Event) {
go func(c chan *bus.Event) {
defer recover()
c <- event
}(s)

View file

@ -3,7 +3,7 @@ package builtin
import (
"testing"
"github.com/drone/drone/eventbus"
"github.com/drone/drone/pkg/bus"
. "github.com/franela/goblin"
)
@ -12,8 +12,8 @@ func TestBuild(t *testing.T) {
g.Describe("Bus", func() {
g.It("Should unsubscribe", func() {
c1 := make(chan *eventbus.Event)
c2 := make(chan *eventbus.Event)
c1 := make(chan *bus.Event)
c2 := make(chan *bus.Event)
b := New()
b.Subscribe(c1)
b.Subscribe(c2)
@ -21,8 +21,8 @@ func TestBuild(t *testing.T) {
})
g.It("Should subscribe", func() {
c1 := make(chan *eventbus.Event)
c2 := make(chan *eventbus.Event)
c1 := make(chan *bus.Event)
c2 := make(chan *bus.Event)
b := New()
b.Subscribe(c1)
b.Subscribe(c2)
@ -33,9 +33,9 @@ func TestBuild(t *testing.T) {
})
g.It("Should send", func() {
e1 := &eventbus.Event{Name: "foo"}
e2 := &eventbus.Event{Name: "bar"}
c := make(chan *eventbus.Event)
e1 := &bus.Event{Name: "foo"}
e2 := &bus.Event{Name: "bar"}
c := make(chan *bus.Event)
b := New()
b.Subscribe(c)
b.Send(e1)

View file

@ -1,4 +1,4 @@
package eventbus
package bus
const (
EventRepo = "repo"

View file

@ -4,8 +4,8 @@ import (
"database/sql"
"os"
"github.com/drone/drone/datastore"
"github.com/drone/drone/datastore/builtin/migrate"
"github.com/drone/drone/pkg/store"
"github.com/drone/drone/pkg/store/builtin/migrate"
"github.com/BurntSushi/migration"
_ "github.com/lib/pq"
@ -71,7 +71,7 @@ func mustConnectTest() *sql.DB {
}
// New returns a new Datastore
func New(db *sql.DB) datastore.Datastore {
func New(db *sql.DB) store.Store {
return struct {
*Userstore
*Repostore

View file

@ -7,11 +7,11 @@ import (
"github.com/stretchr/testify/mock"
)
type Datastore struct {
type Store struct {
mock.Mock
}
func (m *Datastore) User(id int64) (*common.User, error) {
func (m *Store) User(id int64) (*common.User, error) {
ret := m.Called(id)
var r0 *common.User
@ -22,7 +22,7 @@ func (m *Datastore) User(id int64) (*common.User, error) {
return r0, r1
}
func (m *Datastore) UserLogin(_a0 string) (*common.User, error) {
func (m *Store) UserLogin(_a0 string) (*common.User, error) {
ret := m.Called(_a0)
var r0 *common.User
@ -33,7 +33,7 @@ func (m *Datastore) UserLogin(_a0 string) (*common.User, error) {
return r0, r1
}
func (m *Datastore) UserList() ([]*common.User, error) {
func (m *Store) UserList() ([]*common.User, error) {
ret := m.Called()
var r0 []*common.User
@ -44,7 +44,7 @@ func (m *Datastore) UserList() ([]*common.User, error) {
return r0, r1
}
func (m *Datastore) UserFeed(_a0 *common.User, _a1 int, _a2 int) ([]*common.RepoCommit, error) {
func (m *Store) UserFeed(_a0 *common.User, _a1 int, _a2 int) ([]*common.RepoCommit, error) {
ret := m.Called(_a0, _a1, _a2)
var r0 []*common.RepoCommit
@ -55,7 +55,7 @@ func (m *Datastore) UserFeed(_a0 *common.User, _a1 int, _a2 int) ([]*common.Repo
return r0, r1
}
func (m *Datastore) UserCount() (int, error) {
func (m *Store) UserCount() (int, error) {
ret := m.Called()
r0 := ret.Get(0).(int)
@ -63,28 +63,28 @@ func (m *Datastore) UserCount() (int, error) {
return r0, r1
}
func (m *Datastore) AddUser(_a0 *common.User) error {
func (m *Store) AddUser(_a0 *common.User) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) SetUser(_a0 *common.User) error {
func (m *Store) SetUser(_a0 *common.User) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) DelUser(_a0 *common.User) error {
func (m *Store) DelUser(_a0 *common.User) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) Token(_a0 int64) (*common.Token, error) {
func (m *Store) Token(_a0 int64) (*common.Token, error) {
ret := m.Called(_a0)
var r0 *common.Token
@ -95,7 +95,7 @@ func (m *Datastore) Token(_a0 int64) (*common.Token, error) {
return r0, r1
}
func (m *Datastore) TokenLabel(_a0 *common.User, _a1 string) (*common.Token, error) {
func (m *Store) TokenLabel(_a0 *common.User, _a1 string) (*common.Token, error) {
ret := m.Called(_a0, _a1)
var r0 *common.Token
@ -106,7 +106,7 @@ func (m *Datastore) TokenLabel(_a0 *common.User, _a1 string) (*common.Token, err
return r0, r1
}
func (m *Datastore) TokenList(_a0 *common.User) ([]*common.Token, error) {
func (m *Store) TokenList(_a0 *common.User) ([]*common.Token, error) {
ret := m.Called(_a0)
var r0 []*common.Token
@ -117,21 +117,21 @@ func (m *Datastore) TokenList(_a0 *common.User) ([]*common.Token, error) {
return r0, r1
}
func (m *Datastore) AddToken(_a0 *common.Token) error {
func (m *Store) AddToken(_a0 *common.Token) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) DelToken(_a0 *common.Token) error {
func (m *Store) DelToken(_a0 *common.Token) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) Starred(_a0 *common.User, _a1 *common.Repo) (bool, error) {
func (m *Store) Starred(_a0 *common.User, _a1 *common.Repo) (bool, error) {
ret := m.Called(_a0, _a1)
r0 := ret.Get(0).(bool)
@ -139,21 +139,21 @@ func (m *Datastore) Starred(_a0 *common.User, _a1 *common.Repo) (bool, error) {
return r0, r1
}
func (m *Datastore) AddStar(_a0 *common.User, _a1 *common.Repo) error {
func (m *Store) AddStar(_a0 *common.User, _a1 *common.Repo) error {
ret := m.Called(_a0, _a1)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) DelStar(_a0 *common.User, _a1 *common.Repo) error {
func (m *Store) DelStar(_a0 *common.User, _a1 *common.Repo) error {
ret := m.Called(_a0, _a1)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) Repo(id int64) (*common.Repo, error) {
func (m *Store) Repo(id int64) (*common.Repo, error) {
ret := m.Called(id)
var r0 *common.Repo
@ -164,7 +164,7 @@ func (m *Datastore) Repo(id int64) (*common.Repo, error) {
return r0, r1
}
func (m *Datastore) RepoName(owner string, name string) (*common.Repo, error) {
func (m *Store) RepoName(owner string, name string) (*common.Repo, error) {
ret := m.Called(owner, name)
var r0 *common.Repo
@ -175,7 +175,7 @@ func (m *Datastore) RepoName(owner string, name string) (*common.Repo, error) {
return r0, r1
}
func (m *Datastore) RepoList(_a0 *common.User) ([]*common.Repo, error) {
func (m *Store) RepoList(_a0 *common.User) ([]*common.Repo, error) {
ret := m.Called(_a0)
var r0 []*common.Repo
@ -186,28 +186,28 @@ func (m *Datastore) RepoList(_a0 *common.User) ([]*common.Repo, error) {
return r0, r1
}
func (m *Datastore) AddRepo(_a0 *common.Repo) error {
func (m *Store) AddRepo(_a0 *common.Repo) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) SetRepo(_a0 *common.Repo) error {
func (m *Store) SetRepo(_a0 *common.Repo) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) DelRepo(_a0 *common.Repo) error {
func (m *Store) DelRepo(_a0 *common.Repo) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) Commit(_a0 int64) (*common.Commit, error) {
func (m *Store) Commit(_a0 int64) (*common.Commit, error) {
ret := m.Called(_a0)
var r0 *common.Commit
@ -218,7 +218,7 @@ func (m *Datastore) Commit(_a0 int64) (*common.Commit, error) {
return r0, r1
}
func (m *Datastore) CommitSeq(_a0 *common.Repo, _a1 int) (*common.Commit, error) {
func (m *Store) CommitSeq(_a0 *common.Repo, _a1 int) (*common.Commit, error) {
ret := m.Called(_a0, _a1)
var r0 *common.Commit
@ -229,7 +229,7 @@ func (m *Datastore) CommitSeq(_a0 *common.Repo, _a1 int) (*common.Commit, error)
return r0, r1
}
func (m *Datastore) CommitLast(_a0 *common.Repo, _a1 string) (*common.Commit, error) {
func (m *Store) CommitLast(_a0 *common.Repo, _a1 string) (*common.Commit, error) {
ret := m.Called(_a0, _a1)
var r0 *common.Commit
@ -240,7 +240,7 @@ func (m *Datastore) CommitLast(_a0 *common.Repo, _a1 string) (*common.Commit, er
return r0, r1
}
func (m *Datastore) CommitList(_a0 *common.Repo, _a1 int, _a2 int) ([]*common.Commit, error) {
func (m *Store) CommitList(_a0 *common.Repo, _a1 int, _a2 int) ([]*common.Commit, error) {
ret := m.Called(_a0, _a1, _a2)
var r0 []*common.Commit
@ -251,28 +251,28 @@ func (m *Datastore) CommitList(_a0 *common.Repo, _a1 int, _a2 int) ([]*common.Co
return r0, r1
}
func (m *Datastore) AddCommit(_a0 *common.Commit) error {
func (m *Store) AddCommit(_a0 *common.Commit) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) SetCommit(_a0 *common.Commit) error {
func (m *Store) SetCommit(_a0 *common.Commit) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) KillCommits() error {
func (m *Store) KillCommits() error {
ret := m.Called()
r0 := ret.Error(0)
return r0
}
func (m *Datastore) Build(_a0 int64) (*common.Build, error) {
func (m *Store) Build(_a0 int64) (*common.Build, error) {
ret := m.Called(_a0)
var r0 *common.Build
@ -283,7 +283,7 @@ func (m *Datastore) Build(_a0 int64) (*common.Build, error) {
return r0, r1
}
func (m *Datastore) BuildSeq(_a0 *common.Commit, _a1 int) (*common.Build, error) {
func (m *Store) BuildSeq(_a0 *common.Commit, _a1 int) (*common.Build, error) {
ret := m.Called(_a0, _a1)
var r0 *common.Build
@ -294,7 +294,7 @@ func (m *Datastore) BuildSeq(_a0 *common.Commit, _a1 int) (*common.Build, error)
return r0, r1
}
func (m *Datastore) BuildList(_a0 *common.Commit) ([]*common.Build, error) {
func (m *Store) BuildList(_a0 *common.Commit) ([]*common.Build, error) {
ret := m.Called(_a0)
var r0 []*common.Build
@ -305,14 +305,14 @@ func (m *Datastore) BuildList(_a0 *common.Commit) ([]*common.Build, error) {
return r0, r1
}
func (m *Datastore) SetBuild(_a0 *common.Build) error {
func (m *Store) SetBuild(_a0 *common.Build) error {
ret := m.Called(_a0)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) GetBlob(path string) ([]byte, error) {
func (m *Store) GetBlob(path string) ([]byte, error) {
ret := m.Called(path)
var r0 []byte
@ -323,7 +323,7 @@ func (m *Datastore) GetBlob(path string) ([]byte, error) {
return r0, r1
}
func (m *Datastore) GetBlobReader(path string) (io.ReadCloser, error) {
func (m *Store) GetBlobReader(path string) (io.ReadCloser, error) {
ret := m.Called(path)
r0 := ret.Get(0).(io.ReadCloser)
@ -331,21 +331,21 @@ func (m *Datastore) GetBlobReader(path string) (io.ReadCloser, error) {
return r0, r1
}
func (m *Datastore) SetBlob(path string, data []byte) error {
func (m *Store) SetBlob(path string, data []byte) error {
ret := m.Called(path, data)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) SetBlobReader(path string, r io.Reader) error {
func (m *Store) SetBlobReader(path string, r io.Reader) error {
ret := m.Called(path, r)
r0 := ret.Error(0)
return r0
}
func (m *Datastore) DelBlob(path string) error {
func (m *Store) DelBlob(path string) error {
ret := m.Called(path)
r0 := ret.Error(0)

View file

@ -1,4 +1,4 @@
package datastore
package store
import (
"io"
@ -6,7 +6,7 @@ import (
"github.com/drone/drone/common"
)
type Datastore interface {
type Store interface {
// User returns a user by user ID.
User(id int64) (*common.User, error)

View file

@ -6,8 +6,8 @@ import (
"io"
"github.com/drone/drone/common"
"github.com/drone/drone/datastore"
"github.com/drone/drone/eventbus"
"github.com/drone/drone/pkg/bus"
"github.com/drone/drone/pkg/store"
"github.com/drone/drone/remote"
)
@ -19,13 +19,13 @@ type Updater interface {
// NewUpdater returns an implementation of the Updater interface
// that directly modifies the database and sends messages to the bus.
func NewUpdater(bus eventbus.Bus, store datastore.Datastore, rem remote.Remote) Updater {
func NewUpdater(bus bus.Bus, store store.Store, rem remote.Remote) Updater {
return &updater{bus, store, rem}
}
type updater struct {
bus eventbus.Bus
store datastore.Datastore
bus bus.Bus
store store.Store
remote remote.Remote
}
@ -45,9 +45,9 @@ func (u *updater) SetCommit(user *common.User, r *common.Repo, c *common.Commit)
return err
}
u.bus.Send(&eventbus.Event{
u.bus.Send(&bus.Event{
Name: r.FullName,
Kind: eventbus.EventRepo,
Kind: bus.EventRepo,
Msg: msg,
})
return nil
@ -64,9 +64,9 @@ func (u *updater) SetBuild(r *common.Repo, c *common.Commit, b *common.Build) er
return err
}
u.bus.Send(&eventbus.Event{
u.bus.Send(&bus.Event{
Name: r.FullName,
Kind: eventbus.EventRepo,
Kind: bus.EventRepo,
Msg: msg,
})
return nil

View file

@ -7,8 +7,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/drone/drone/common"
"github.com/drone/drone/datastore"
"github.com/drone/drone/eventbus"
"github.com/drone/drone/pkg/bus"
"github.com/drone/drone/pkg/store"
"github.com/drone/drone/queue"
"github.com/drone/drone/remote"
"github.com/drone/drone/runner"
@ -31,19 +31,19 @@ func ToQueue(c *gin.Context) queue.Queue {
return v.(queue.Queue)
}
func SetBus(r eventbus.Bus) gin.HandlerFunc {
func SetBus(r bus.Bus) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("eventbus", r)
c.Set("bus", r)
c.Next()
}
}
func ToBus(c *gin.Context) eventbus.Bus {
v, ok := c.Get("eventbus")
func ToBus(c *gin.Context) bus.Bus {
v, ok := c.Get("bus")
if !ok {
return nil
}
return v.(eventbus.Bus)
return v.(bus.Bus)
}
func ToRemote(c *gin.Context) remote.Remote {
@ -115,15 +115,15 @@ func ToRepo(c *gin.Context) *common.Repo {
return v.(*common.Repo)
}
func ToDatastore(c *gin.Context) datastore.Datastore {
return c.MustGet("datastore").(datastore.Datastore)
func ToDatastore(c *gin.Context) store.Store {
return c.MustGet("datastore").(store.Store)
}
func ToSession(c *gin.Context) session.Session {
return c.MustGet("session").(session.Session)
}
func SetDatastore(ds datastore.Datastore) gin.HandlerFunc {
func SetDatastore(ds store.Store) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("datastore", ds)
c.Next()

View file

@ -8,7 +8,7 @@ import (
"strconv"
"time"
"github.com/drone/drone/eventbus"
"github.com/drone/drone/pkg/bus"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
@ -37,14 +37,14 @@ var upgrader = websocket.Upgrader{
// GetRepoEvents will upgrade the connection to a Websocket and will stream
// event updates to the browser.
func GetRepoEvents(c *gin.Context) {
bus := ToBus(c)
bus_ := ToBus(c)
repo := ToRepo(c)
c.Writer.Header().Set("Content-Type", "text/event-stream")
eventc := make(chan *eventbus.Event, 1)
bus.Subscribe(eventc)
eventc := make(chan *bus.Event, 1)
bus_.Subscribe(eventc)
defer func() {
bus.Unsubscribe(eventc)
bus_.Unsubscribe(eventc)
log.Infof("closed event stream")
}()
@ -55,7 +55,7 @@ func GetRepoEvents(c *gin.Context) {
log.Infof("nil event received")
return false
}
if event.Kind == eventbus.EventRepo &&
if event.Kind == bus.EventRepo &&
event.Name == repo.FullName {
d := map[string]interface{}{}
json.Unmarshal(event.Msg, &d)