82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
|
|
rpmclone "github.com/syonad/clonepack/internal/clone/rpm"
|
|
"github.com/syonad/clonepack/internal/store"
|
|
)
|
|
|
|
var validTypes = map[string]bool{"apt": true, "rpm": true, "docker": true, "binary": true}
|
|
|
|
type RepoService struct {
|
|
store store.RepoStore
|
|
dataDir string
|
|
}
|
|
|
|
func NewRepoService(s store.RepoStore, dataDir string) *RepoService {
|
|
return &RepoService{store: s, dataDir: dataDir}
|
|
}
|
|
|
|
var validSyncModes = map[string]bool{"auto": true, "manual": true}
|
|
|
|
type CreateRepoInput struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
SourceURL string `json:"source_url"`
|
|
SyncMode string `json:"sync_mode"`
|
|
}
|
|
|
|
func (s *RepoService) Create(ctx context.Context, in CreateRepoInput) (*store.Repo, error) {
|
|
if in.Name == "" {
|
|
return nil, fmt.Errorf("name is required")
|
|
}
|
|
if !validTypes[in.Type] {
|
|
return nil, fmt.Errorf("invalid type: %s (must be apt, rpm, docker, or binary)", in.Type)
|
|
}
|
|
if in.SourceURL == "" {
|
|
return nil, fmt.Errorf("source_url is required")
|
|
}
|
|
if in.SyncMode == "" {
|
|
in.SyncMode = "auto"
|
|
} else if !validSyncModes[in.SyncMode] {
|
|
return nil, fmt.Errorf("invalid sync_mode: %s (must be auto or manual)", in.SyncMode)
|
|
}
|
|
|
|
r := &store.Repo{Name: in.Name, Type: in.Type, SourceURL: in.SourceURL, SyncMode: in.SyncMode}
|
|
id, err := s.store.CreateRepo(ctx, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repo, err := s.store.GetRepo(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.initStorage(repo)
|
|
return repo, nil
|
|
}
|
|
|
|
func (s *RepoService) initStorage(repo *store.Repo) {
|
|
switch repo.Type {
|
|
case "rpm":
|
|
localDir := filepath.Join(s.dataDir, "repos", fmt.Sprintf("%d", repo.ID), "rpm")
|
|
if err := rpmclone.InitEmptyRepo(localDir); err != nil {
|
|
log.Printf("init storage for repo %d: %v", repo.ID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *RepoService) List(ctx context.Context) ([]store.Repo, error) {
|
|
return s.store.ListRepos(ctx)
|
|
}
|
|
|
|
func (s *RepoService) Get(ctx context.Context, id int64) (*store.Repo, error) {
|
|
return s.store.GetRepo(ctx, id)
|
|
}
|
|
|
|
func (s *RepoService) Delete(ctx context.Context, id int64) error {
|
|
return s.store.DeleteRepo(ctx, id)
|
|
}
|