repos

package
v0.0.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IdConflictSameContent = iota + 1
	IdConflictSameTimestamp
)
View Source
const (
	KeyRepos = "repos"

	KeyRepoType    = "type"
	KeyRepoLoc     = "loc"
	KeyRepoAuth    = "auth"
	KeyRepoEnabled = "enabled"

	RepoTypeFile             = "file"
	RepoTypeHttp             = "http"
	RepoTypeTmc              = "tmc"
	CompletionKindNames      = "names"
	CompletionKindFetchNames = "fetchNames"
	RepoConfDir              = ".tmc"
	IndexFilename            = "tm-catalog.toc.json"
	TmNamesFile              = "tmnames.txt"
)
View Source
const RelFileUriPlaceholder = "{{ID}}"
View Source
const (
	TMExt = ".tm.json"
)

Variables

View Source
var (
	ErrAmbiguous               = errors.New("multiple repos configured, but target repo not specified")
	ErrRepoNotFound            = errors.New("repo not found")
	ErrInvalidRepoName         = errors.New("invalid repo name")
	ErrRepoExists              = errors.New("named repo already exists")
	ErrTmNotFound              = errors.New("TM not found")
	ErrInvalidErrorCode        = errors.New("invalid error code")
	ErrInvalidCompletionParams = errors.New("invalid completion parameters")
	ErrNotSupported            = errors.New("method not supported")
	ErrResourceAccess          = errors.New("cannot access resource")
	ErrResourceInvalid         = errors.New("invalid resource name")
	ErrResourceNotExists       = errors.New("resource does not exist")
	ErrIndexMismatch           = errors.New("index does not reflect repository content, maybe needs rebuild")
	ErrNoIndex                 = errors.New("no table of contents found. Run `index` for this repo")
)
View Source
var All = func() ([]Repo, error) {
	conf, err := ReadConfig()
	if err != nil {
		return nil, err
	}
	var rs []Repo

	for n, rc := range conf {
		en := utils.JsGetBool(rc, KeyRepoEnabled)
		if en != nil && !*en {
			continue
		}
		r, err := createRepo(rc, model.NewRepoSpec(n))
		if err != nil {
			return rs, err
		}
		rs = append(rs, r)
	}
	return rs, err
}
View Source
var ErrIndexLocked = errors.New("could not acquire lock on index file")
View Source
var ErrRootInvalid = errors.New("root is not a directory")
View Source
var Get = func(spec model.RepoSpec) (Repo, error) {
	if spec.Dir() != "" {
		if spec.RepoName() != "" {
			return nil, model.ErrInvalidSpec
		}
		return NewFileRepo(map[string]any{KeyRepoType: "file", KeyRepoLoc: spec.Dir()}, spec)
	}
	repos, err := ReadConfig()
	if err != nil {
		return nil, err
	}
	repos = filterEnabled(repos)
	rc, ok := repos[spec.RepoName()]
	if spec.RepoName() == "" {
		switch len(repos) {
		case 0:
			return nil, ErrRepoNotFound
		case 1:
			for n, v := range repos {
				rc = v
				spec = model.NewRepoSpec(n)
			}
		default:
			return nil, ErrAmbiguous
		}
	} else {
		if !ok {
			return nil, ErrRepoNotFound
		}
	}
	return createRepo(rc, spec)
}
View Source
var ValidRepoNameRegex = regexp.MustCompile("^[a-zA-Z0-9][\\w\\-_:]*$")

Functions

func Add

func Add(name, typ, confStr string, confFile []byte) error

func AsRepoConfig

func AsRepoConfig(bytes []byte) (map[string]any, error)

func Remove

func Remove(name string) error

func Rename

func Rename(oldName, newName string) error

func SetConfig

func SetConfig(name, typ, confStr string, confFile []byte) error

func ToggleEnabled

func ToggleEnabled(name string) error

Types

type Config

type Config map[string]map[string]any

func ReadConfig

func ReadConfig() (Config, error)

type ErrTMIDConflict

type ErrTMIDConflict struct {
	Type       IdConflictType
	ExistingId string
}

func ParseErrTMIDConflict

func ParseErrTMIDConflict(errCode string) (*ErrTMIDConflict, error)

func (*ErrTMIDConflict) Code

func (e *ErrTMIDConflict) Code() string

Code returns a machine-readable string error code, which can be parsed by ParseErrTMIDConflict

func (*ErrTMIDConflict) Error

func (e *ErrTMIDConflict) Error() string

type FileRepo

type FileRepo struct {
	// contains filtered or unexported fields
}

FileRepo implements a Repo TM repository backed by a file system

func NewFileRepo

func NewFileRepo(config map[string]any, spec model.RepoSpec) (*FileRepo, error)

func (*FileRepo) AnalyzeIndex

func (f *FileRepo) AnalyzeIndex(ctx context.Context) error

func (*FileRepo) Delete

func (f *FileRepo) Delete(ctx context.Context, id string) error

func (*FileRepo) Fetch

func (f *FileRepo) Fetch(ctx context.Context, id string) (string, []byte, error)

func (*FileRepo) Index

func (f *FileRepo) Index(ctx context.Context, ids ...string) error

func (*FileRepo) List

func (f *FileRepo) List(ctx context.Context, search *model.SearchParams) (model.SearchResult, error)

func (*FileRepo) ListCompletions

func (f *FileRepo) ListCompletions(ctx context.Context, kind string, toComplete string) ([]string, error)

func (*FileRepo) Push

func (f *FileRepo) Push(ctx context.Context, id model.TMID, raw []byte) error

func (*FileRepo) RangeResources

func (f *FileRepo) RangeResources(ctx context.Context, filter model.ResourceFilter,
	visit func(model.Resource, error) bool) error

func (*FileRepo) Spec

func (f *FileRepo) Spec() model.RepoSpec

func (*FileRepo) Versions

func (f *FileRepo) Versions(ctx context.Context, name string) ([]model.FoundVersion, error)

type HttpRepo

type HttpRepo struct {
	// contains filtered or unexported fields
}

HttpRepo implements a Repo backed by a http server. It does not allow writing to the repository and is thus a read-only view

func NewHttpRepo

func NewHttpRepo(config map[string]any, spec model.RepoSpec) (*HttpRepo, error)

func (*HttpRepo) AnalyzeIndex

func (h *HttpRepo) AnalyzeIndex(context.Context) error

func (*HttpRepo) Delete

func (h *HttpRepo) Delete(ctx context.Context, id string) error

func (*HttpRepo) Fetch

func (h *HttpRepo) Fetch(ctx context.Context, id string) (string, []byte, error)

func (*HttpRepo) Index

func (h *HttpRepo) Index(context.Context, ...string) error

func (*HttpRepo) List

func (h *HttpRepo) List(ctx context.Context, search *model.SearchParams) (model.SearchResult, error)

func (*HttpRepo) ListCompletions

func (h *HttpRepo) ListCompletions(ctx context.Context, kind string, toComplete string) ([]string, error)

func (*HttpRepo) Push

func (h *HttpRepo) Push(ctx context.Context, id model.TMID, raw []byte) error

func (*HttpRepo) RangeResources

func (h *HttpRepo) RangeResources(context.Context, model.ResourceFilter, func(model.Resource, error) bool) error

func (*HttpRepo) Spec

func (h *HttpRepo) Spec() model.RepoSpec

func (*HttpRepo) Versions

func (h *HttpRepo) Versions(ctx context.Context, name string) ([]model.FoundVersion, error)

type IdConflictType

type IdConflictType int

func (IdConflictType) String

func (t IdConflictType) String() string

type Repo

type Repo interface {
	// Push writes the Thing Model file into the path under root that corresponds to id.
	// Returns ErrTMIDConflict if the same file is already stored with a different timestamp or
	// there is a file with the same semantic version and timestamp but different content
	Push(ctx context.Context, id model.TMID, raw []byte) error
	// Fetch retrieves the Thing Model file from repo
	// Returns the actual id of the retrieved Thing Model (it may differ in the timestamp from the id requested), the file contents, and an error
	Fetch(ctx context.Context, id string) (string, []byte, error)
	// Index updates repository's index file with data from given TM files. For ids that refer to non-existing files,
	// removes those from index. Performs a full update if no updatedIds given
	Index(ctx context.Context, updatedIds ...string) error
	// AnalyzeIndex checks the index for consistency.
	AnalyzeIndex(ctx context.Context) error
	// List searches the catalog for TMs matching search parameters
	List(ctx context.Context, search *model.SearchParams) (model.SearchResult, error)
	// Versions lists versions of a TM with given name
	Versions(ctx context.Context, name string) ([]model.FoundVersion, error)
	// Spec returns the spec this Repo has been created from
	Spec() model.RepoSpec
	// Delete deletes the TM with given id from repo. Returns ErrTmNotFound if TM does not exist
	Delete(ctx context.Context, id string) error
	// RangeResources iterates over resources within this Repo.
	// Iteration can be narrowed down by a ResourceFilter. Each iteration calls the visit function.
	RangeResources(ctx context.Context, filter model.ResourceFilter, visit func(res model.Resource, err error) bool) error

	ListCompletions(ctx context.Context, kind string, toComplete string) ([]string, error)
}

type RepoAccessError

type RepoAccessError struct {
	// contains filtered or unexported fields
}

func NewRepoAccessError

func NewRepoAccessError(spec model.RepoSpec, err error) *RepoAccessError

func (*RepoAccessError) Error

func (e *RepoAccessError) Error() string

func (*RepoAccessError) Unwrap

func (e *RepoAccessError) Unwrap() error

type TmcRepo

type TmcRepo struct {
	// contains filtered or unexported fields
}

TmcRepo implements a Repo TM repository backed by an instance of TM catalog REST API server

func NewTmcRepo

func NewTmcRepo(config map[string]any, spec model.RepoSpec) (*TmcRepo, error)

func (TmcRepo) AnalyzeIndex

func (t TmcRepo) AnalyzeIndex(context.Context) error

func (TmcRepo) Delete

func (t TmcRepo) Delete(ctx context.Context, id string) error

func (TmcRepo) Fetch

func (t TmcRepo) Fetch(ctx context.Context, id string) (string, []byte, error)

func (TmcRepo) Index

func (t TmcRepo) Index(context.Context, ...string) error

func (TmcRepo) List

func (t TmcRepo) List(ctx context.Context, search *model.SearchParams) (model.SearchResult, error)

func (TmcRepo) ListCompletions

func (t TmcRepo) ListCompletions(ctx context.Context, kind string, toComplete string) ([]string, error)

func (TmcRepo) Push

func (t TmcRepo) Push(ctx context.Context, id model.TMID, raw []byte) error

func (TmcRepo) RangeResources

func (t TmcRepo) RangeResources(context.Context, model.ResourceFilter, func(model.Resource, error) bool) error

func (TmcRepo) Spec

func (t TmcRepo) Spec() model.RepoSpec

func (TmcRepo) Versions

func (t TmcRepo) Versions(ctx context.Context, name string) ([]model.FoundVersion, error)

type Union

type Union struct {
	// contains filtered or unexported fields
}

func GetSpecdOrAll

func GetSpecdOrAll(spec model.RepoSpec) (*Union, error)

GetSpecdOrAll returns a union containing the repo specified by spec, or union of all repo, if the spec is empty

func NewUnion

func NewUnion(rs ...Repo) *Union

func (*Union) Fetch

func (u *Union) Fetch(ctx context.Context, id string) (string, []byte, error, []*RepoAccessError)

func (*Union) List

func (*Union) ListCompletions

func (u *Union) ListCompletions(ctx context.Context, kind string, toComplete string) []string

func (*Union) Versions

func (u *Union) Versions(ctx context.Context, name string) ([]model.FoundVersion, []*RepoAccessError)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL