repos

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

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

	KeyRepoType        = "type"
	KeyRepoLoc         = "loc"
	KeyRepoAuth        = "auth"
	KeyRepoHeaders     = "headers"
	KeyRepoEnabled     = "enabled"
	KeyRepoDescription = "description"

	KeyRepoAWSRegion          = "aws_region"
	KeyRepoAWSBucket          = "aws_bucket"
	KeyRepoAWSAccessKeyId     = "aws_access_key_id"
	KeyRepoAWSSecretAccessKey = "aws_secret_access_key"
	AuthMethodNone            = "none"
	AuthMethodBearerToken     = "bearer"
	AuthMethodBasic           = "basic"

	RepoTypeFile              = "file"
	RepoTypeHttp              = "http"
	RepoTypeTmc               = "tmc"
	RepoTypeS3                = "s3"
	CompletionKindNames       = "names"
	CompletionKindFetchNames  = "fetchNames"
	CompletionKindNamesOrIds  = "namesOrIds"
	CompletionKindAttachments = "attachments"
	RepoConfDir               = ".tmc"
	IndexFilename             = "tm-catalog.toc.json"
	TmNamesFile               = "tmnames.txt"
	TmIgnoreFile              = ".tmcignore"
)
View Source
const (
	ImportResultOK      = ImportResultType(iota + 1)
	ImportResultWarning // imported but with warning
	ImportResultError   // not imported because of error
)
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")
	ErrAttachmentExists        = errors.New("attachment already exists")
	ErrInvalidErrorCode        = errors.New("invalid error code")
	ErrInvalidCompletionParams = errors.New("invalid completion parameters")
	ErrNotSupported            = errors.New("method not supported")
	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
	}
	conf = filterEnabled(conf)
	var rs []Repo

	for n, rc := range conf {
		r, err := createRepo(rc, model.NewRepoSpec(n))
		if err != nil {
			return rs, err
		}
		rs = append(rs, r)
	}
	return rs, err
}
View Source
var ErrRootInvalid = errors.New("root is not a directory")
View Source
var ErrS3NotExists = errors.New("file does not exist in S3")
View Source
var ErrS3Op = errors.New("operational error")
View Source
var ErrS3Unknown = errors.New("unknown error")
View Source
var Get = func(spec model.RepoSpec) (Repo, error) {
	if spec.Dir() != "" {
		if spec.RepoName() != "" {
			return nil, fmt.Errorf("could not initialize a repo instance for %s: %w\ncheck config", spec, model.ErrInvalidSpec)
		}
		return NewFileRepo(map[string]any{KeyRepoType: "file", KeyRepoLoc: spec.Dir()}, spec)
	}
	repos, err := ReadConfig()
	if err != nil {
		return nil, fmt.Errorf("could not initialize a repo instance for %s: could not read config: %w", spec, err)
	}
	repos = filterEnabled(repos)
	parent, child := splitRepoName(spec.RepoName())
	spec = model.NewRepoSpec(parent)
	rc, ok := repos[parent]
	if parent == "" {
		switch len(repos) {
		case 0:
			return nil, fmt.Errorf("could not initialize a repo instance for %s: %w\ncheck config", spec, ErrRepoNotFound)
		case 1:
			for n, v := range repos {
				rc = v
				spec = model.NewRepoSpec(n)
			}
		default:
			return nil, fmt.Errorf("could not initialize a repo instance for %s: %w\ncheck config", spec, ErrAmbiguous)
		}
	} else {
		if !ok {
			return nil, fmt.Errorf("could not initialize a repo instance for %s: %w\ncheck config", spec, ErrRepoNotFound)
		}
	}
	if child != "" {
		rc[keySubRepo] = child
	}
	repo, err := createRepo(rc, spec)
	if err != nil {
		return nil, fmt.Errorf("could not initialize a repo instance for %s: %w\ncheck config", spec, err)
	}
	return repo, err
}
View Source
var GetDescriptions = func(ctx context.Context, spec model.RepoSpec) ([]model.RepoDescription, error) {
	if spec.Dir() != "" {
		r := model.RepoDescription{
			Name:        spec.Dir(),
			Type:        RepoTypeFile,
			Description: "Repo generated from the directory specified in the arguments of serve command",
		}
		return []model.RepoDescription{r}, nil
	}
	conf, err := ReadConfig()
	if err != nil {
		return nil, err
	}
	conf = filterEnabled(conf)
	var rs []model.RepoDescription
	for n, rc := range conf {
		if spec.RepoName() == "" || n == spec.RepoName() {
			ds, _ := rc[KeyRepoDescription].(string)
			r := model.RepoDescription{
				Name:        n,
				Type:        fmt.Sprintf("%v", rc[KeyRepoType]),
				Description: ds,
			}
			rs = append(rs, r)
		}
	}
	rs, err = expandTmcRepos(ctx, rs)
	return rs, err
}

GetDescriptions returns the list of descriptions of repositories that could be used as targets for write operations or be returned as "found-in" sources when reading from catalog

View Source
var ValidRepoNameRegex = regexp.MustCompile("^[a-zA-Z0-9][\\w\\-_:]*$")

Functions

func Add

func Add(name string, repoConf ConfigMap) error

func BleveIndexPath added in v0.1.3

func BleveIndexPath(repo Repo) string

func Remove

func Remove(name string) error

func Rename

func Rename(oldName, newName string) error

func SetConfig

func SetConfig(name string, repoConf ConfigMap) error

func ToggleEnabled

func ToggleEnabled(name string) error

func UpdateRepoIndex added in v0.1.3

func UpdateRepoIndex(ctx context.Context, repo Repo) error

Types

type CodedError

type CodedError interface {
	Code() string
}

type Config

type Config map[string]map[string]any

func ReadConfig

func ReadConfig() (Config, error)

type ConfigMap added in v0.1.3

type ConfigMap map[string]any

func AsRepoConfig

func AsRepoConfig(bytes []byte) (ConfigMap, error)

func NewRepoConfig added in v0.1.3

func NewRepoConfig(typ string, confFile []byte) (ConfigMap, error)

func (ConfigMap) GetBool added in v0.1.3

func (m ConfigMap) GetBool(key string) (bool, bool)

GetBool reads a bool value from the ConfigMap. If the value in the map is a string, it'll attempt to expand an environment variable and parse the result as bool. It is very similar to util.JsGetBool, except the latter does not expand variables

func (ConfigMap) GetString added in v0.1.3

func (m ConfigMap) GetString(key string) (string, bool)

GetString reads a string value from the ConfigMap and expands environment variable if necessary It is very similar to util.JsGetString, except the latter does not expand variables

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 ConfigMap, spec model.RepoSpec) (*FileRepo, error)

func (*FileRepo) CanonicalRoot added in v0.1.3

func (f *FileRepo) CanonicalRoot() string

func (*FileRepo) CheckIntegrity added in v0.1.3

func (f *FileRepo) CheckIntegrity(ctx context.Context, filter model.ResourceFilter) (results []model.CheckResult, err error)

func (*FileRepo) Delete

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

func (*FileRepo) DeleteAttachment

func (f *FileRepo) DeleteAttachment(ctx context.Context, ref model.AttachmentContainerRef, attachmentName string) error

func (*FileRepo) Fetch

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

func (*FileRepo) FetchAttachment

func (f *FileRepo) FetchAttachment(ctx context.Context, ref model.AttachmentContainerRef, attachmentName string) ([]byte, error)

func (*FileRepo) GetTMMetadata

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

func (*FileRepo) Import

func (f *FileRepo) Import(ctx context.Context, id model.TMID, raw []byte, opts ImportOptions) (ImportResult, error)

func (*FileRepo) ImportAttachment

func (f *FileRepo) ImportAttachment(ctx context.Context, container model.AttachmentContainerRef, attachment model.Attachment, content []byte, force bool) 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.Filters) (model.SearchResult, error)

func (*FileRepo) ListCompletions

func (f *FileRepo) ListCompletions(ctx context.Context, kind string, args []string, toComplete string) ([]string, 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) CanonicalRoot added in v0.1.3

func (h *HttpRepo) CanonicalRoot() string

func (*HttpRepo) CheckIntegrity added in v0.1.3

func (h *HttpRepo) CheckIntegrity(ctx context.Context, filter model.ResourceFilter) (results []model.CheckResult, err error)

func (*HttpRepo) Delete

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

func (*HttpRepo) DeleteAttachment

func (h *HttpRepo) DeleteAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName string) error

func (*HttpRepo) Fetch

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

func (*HttpRepo) FetchAttachment

func (h *HttpRepo) FetchAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName string) ([]byte, error)

func (*HttpRepo) GetTMMetadata

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

func (*HttpRepo) Import

func (h *HttpRepo) Import(ctx context.Context, id model.TMID, raw []byte, opts ImportOptions) (ImportResult, error)

func (*HttpRepo) ImportAttachment

func (h *HttpRepo) ImportAttachment(ctx context.Context, container model.AttachmentContainerRef, attachment model.Attachment, content []byte, force bool) error

func (*HttpRepo) Index

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

func (*HttpRepo) List

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

func (*HttpRepo) ListCompletions

func (h *HttpRepo) ListCompletions(ctx context.Context, kind string, args []string, toComplete string) ([]string, 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 ImportOptions

type ImportOptions struct {
	Force           bool
	OptPath         string
	IgnoreExisting  bool
	WithAttachments bool
}

type ImportResult

type ImportResult struct {
	Type ImportResultType `json:"type"`
	// TmID is not empty when the result is successful, i.e. Type is OK or Warning
	TmID    string `json:"-"`
	Message string `json:"message,omitempty"`
	// Err is not nil when there was an ID conflict or another error during import, i.e. Type is TMExists or Warning or Error
	Err error `json:"-"`
}

func ImportResultFromError

func ImportResultFromError(err error) (ImportResult, error)

func (ImportResult) IsSuccessful

func (r ImportResult) IsSuccessful() bool

func (ImportResult) String

func (r ImportResult) String() string

type ImportResultType

type ImportResultType int

func (ImportResultType) MarshalJSON added in v0.1.3

func (t ImportResultType) MarshalJSON() ([]byte, error)

func (ImportResultType) String

func (t ImportResultType) String() string

type Repo

type Repo interface {
	// Import 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
	Import(ctx context.Context, id model.TMID, raw []byte, opts ImportOptions) (ImportResult, 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
	// CheckIntegrity checks the internal resources for integrity and consistency
	CheckIntegrity(ctx context.Context, filter model.ResourceFilter) (results []model.CheckResult, err error)
	// List searches the catalog for TMs matching search parameters
	List(ctx context.Context, search *model.Filters) (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
	// CanonicalRoot returns the canonical representation of the repository's root location
	CanonicalRoot() string
	// Delete deletes the TM with given id from repo. Returns ErrTMNotFound if TM does not exist
	Delete(ctx context.Context, id string) error

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

	GetTMMetadata(ctx context.Context, tmID string) ([]model.FoundVersion, error)
	ImportAttachment(ctx context.Context, container model.AttachmentContainerRef, attachment model.Attachment, content []byte, force bool) error
	FetchAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName string) ([]byte, error)
	DeleteAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName 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 S3Client added in v0.1.3

type S3Client interface {
	GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
	ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
	DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
	DeleteObjects(ctx context.Context, params *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error)
	PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
	HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
	Options() s3.Options
}

type S3ObjectInfo added in v0.1.3

type S3ObjectInfo struct {
	Path string
	Name string
}

type S3Repo added in v0.1.3

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

func NewS3Repo added in v0.1.3

func NewS3Repo(cfg ConfigMap, spec model.RepoSpec) (*S3Repo, error)

func (*S3Repo) CanonicalRoot added in v0.1.3

func (s *S3Repo) CanonicalRoot() string

func (*S3Repo) CheckIntegrity added in v0.1.3

func (s *S3Repo) CheckIntegrity(ctx context.Context, filter model.ResourceFilter) (results []model.CheckResult, err error)

func (*S3Repo) Delete added in v0.1.3

func (s *S3Repo) Delete(ctx context.Context, id string) error

func (*S3Repo) DeleteAttachment added in v0.1.3

func (s *S3Repo) DeleteAttachment(ctx context.Context, ref model.AttachmentContainerRef, attachmentName string) error

func (*S3Repo) Fetch added in v0.1.3

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

func (*S3Repo) FetchAttachment added in v0.1.3

func (s *S3Repo) FetchAttachment(ctx context.Context, ref model.AttachmentContainerRef, attachmentName string) ([]byte, error)

func (*S3Repo) GetTMMetadata added in v0.1.3

func (s *S3Repo) GetTMMetadata(ctx context.Context, tmID string) ([]model.FoundVersion, error)

func (*S3Repo) Import added in v0.1.3

func (s *S3Repo) Import(ctx context.Context, id model.TMID, raw []byte, opts ImportOptions) (ImportResult, error)

func (*S3Repo) ImportAttachment added in v0.1.3

func (s *S3Repo) ImportAttachment(ctx context.Context, container model.AttachmentContainerRef, attachment model.Attachment, content []byte, force bool) error

func (*S3Repo) Index added in v0.1.3

func (s *S3Repo) Index(ctx context.Context, ids ...string) error

func (*S3Repo) List added in v0.1.3

func (s *S3Repo) List(ctx context.Context, search *model.Filters) (model.SearchResult, error)

func (*S3Repo) ListCompletions added in v0.1.3

func (s *S3Repo) ListCompletions(ctx context.Context, kind string, args []string, toComplete string) ([]string, error)

func (*S3Repo) Spec added in v0.1.3

func (s *S3Repo) Spec() model.RepoSpec

func (*S3Repo) Versions added in v0.1.3

func (s *S3Repo) Versions(ctx context.Context, name string) ([]model.FoundVersion, 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) CanonicalRoot added in v0.1.3

func (t *TmcRepo) CanonicalRoot() string

func (*TmcRepo) CheckIntegrity added in v0.1.3

func (t *TmcRepo) CheckIntegrity(ctx context.Context, filter model.ResourceFilter) (results []model.CheckResult, err error)

func (*TmcRepo) Delete

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

func (*TmcRepo) DeleteAttachment

func (t *TmcRepo) DeleteAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName string) error

func (*TmcRepo) Fetch

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

func (*TmcRepo) FetchAttachment

func (t *TmcRepo) FetchAttachment(ctx context.Context, container model.AttachmentContainerRef, attachmentName string) ([]byte, error)

func (*TmcRepo) GetSubRepos

func (t *TmcRepo) GetSubRepos(ctx context.Context) ([]model.RepoDescription, error)

func (*TmcRepo) GetTMMetadata

func (t *TmcRepo) GetTMMetadata(ctx context.Context, tmID string) ([]model.FoundVersion, error)

func (*TmcRepo) Import

func (t *TmcRepo) Import(ctx context.Context, id model.TMID, raw []byte, opts ImportOptions) (ImportResult, error)

func (*TmcRepo) ImportAttachment

func (t *TmcRepo) ImportAttachment(ctx context.Context, container model.AttachmentContainerRef, attachment model.Attachment, content []byte, force bool) error

func (*TmcRepo) Index

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

func (*TmcRepo) List

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

func (*TmcRepo) ListCompletions

func (t *TmcRepo) ListCompletions(ctx context.Context, kind string, args []string, toComplete string) ([]string, 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 GetUnion added in v0.1.3

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

GetUnion returns a union containing the repo specified by spec, or a union of all repos, 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) GetTMMetadata

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

func (*Union) List

func (u *Union) List(ctx context.Context, search *model.Filters) (model.SearchResult, []*RepoAccessError)

func (*Union) ListCompletions

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

func (*Union) Search added in v0.1.3

func (u *Union) Search(ctx context.Context, query string) (model.SearchResult, []*RepoAccessError)

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