2017-05-27 06:12:13 +00:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-07 21:35:10 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-05-27 06:12:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Source interface {
|
|
|
|
ID() string
|
2017-07-06 19:07:42 +00:00
|
|
|
Resolve(ctx context.Context, id Identifier) (SourceInstance, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
type SourceInstance interface {
|
2017-07-06 20:15:54 +00:00
|
|
|
CacheKey(ctx context.Context) (string, error)
|
2017-07-06 19:07:42 +00:00
|
|
|
Snapshot(ctx context.Context) (cache.ImmutableRef, error)
|
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
type Manager struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
sources map[string]Source
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() (*Manager, error) {
|
|
|
|
return &Manager{
|
|
|
|
sources: make(map[string]Source),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *Manager) Register(src Source) {
|
|
|
|
sm.mu.Lock()
|
|
|
|
sm.sources[src.ID()] = src
|
|
|
|
sm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
func (sm *Manager) Resolve(ctx context.Context, id Identifier) (SourceInstance, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
sm.mu.Lock()
|
|
|
|
src, ok := sm.sources[id.ID()]
|
|
|
|
sm.mu.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("no handler fro %s", id.ID())
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
return src.Resolve(ctx, id)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|