From 29ebb9ff51efa39952ecad420665947facaad56d Mon Sep 17 00:00:00 2001 From: Jacob Kozol Date: Tue, 5 Nov 2019 11:31:21 +0100 Subject: [PATCH] store: add sources to store The api is initialized with a base repo but users may want to add their own custom repos. Based on lorax's implementation, the store now supports sources. The source object is similar to but not the same as the repos used by dnf. The store can now add, delete, and get info about sources. --- internal/store/store.go | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/internal/store/store.go b/internal/store/store.go index 0a24d1bdc..bb8972ffa 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -25,6 +25,7 @@ type Store struct { Blueprints map[string]blueprint.Blueprint `json:"blueprints"` Workspace map[string]blueprint.Blueprint `json:"workspace"` Composes map[uuid.UUID]Compose `json:"composes"` + Sources map[string]SourceConfig `json:"sources"` mu sync.RWMutex // protects all fields pendingJobs chan Job @@ -58,6 +59,15 @@ type Image struct { Size int64 } +type SourceConfig struct { + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + CheckGPG bool `json:"check_gpg"` + CheckSSL bool `json:"check_ssl"` + System bool `json:"system"` +} + type NotFoundError struct { message string } @@ -126,6 +136,9 @@ func New(stateFile *string) *Store { // TODO: push waiting/running composes to workers again s.Composes = make(map[uuid.UUID]Compose) } + if s.Sources == nil { + s.Sources = make(map[string]SourceConfig) + } s.pendingJobs = make(chan Job, 200) return &s @@ -487,3 +500,53 @@ func (s *Store) GetImage(composeID uuid.UUID) (*Image, error) { return nil, &NotFoundError{"compose could not be found"} } + +func (s *Store) PushSource(source SourceConfig) { + s.change(func() error { + s.Sources[source.Name] = source + return nil + }) +} + +func (s *Store) DeleteSource(name string) { + s.change(func() error { + delete(s.Sources, name) + return nil + }) +} + +func (s *Store) ListSources() []string { + s.mu.RLock() + defer s.mu.RUnlock() + names := make([]string, 0, len(s.Sources)) + for name := range s.Sources { + names = append(names, name) + } + sort.Strings(names) + + return names +} + +func (s *Store) GetSource(name string) *SourceConfig { + s.mu.RLock() + defer s.mu.RUnlock() + + source, ok := s.Sources[name] + if !ok { + return nil + } + return &source +} + +func (s *Store) GetAllSources() map[string]SourceConfig { + s.mu.RLock() + defer s.mu.RUnlock() + + sources := make(map[string]SourceConfig) + + for k, v := range s.Sources { + sources[k] = v + } + + return sources +}