internal

package
v0.0.0-...-b0bff92 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2021 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAndLog

func CloseAndLog(closer io.Closer, label string, logger Logger)

CloseAndLog calls the closer's Close() and logs any error returned therefrom.

func ContextDownload

func ContextDownload(deps ContextDownloadDeps) (path string, err error)

ContextDownload downloads the named resource and returns the path to which it was downloaded. If the resource does not exist or has not been uploaded yet then errors.NotFound is returned.

Note that the downloaded file is checked for correctness.

func Download

func Download(target DownloadTarget, remote ContentSource) error

Download downloads the resource from the provied source to the target.

func WriteContent

func WriteContent(target io.Writer, content Content, deps WriteContentDeps) error

WriteContent writes the resource file to the target provided by the deps.

Types

type Checker

type Checker struct {
	// Content holds the expected content values.
	Content Content

	// SizeTracker tracks the number of bytes read.
	SizeTracker SizeTracker

	// ChecksumWriter tracks the checksum of the read bytes.
	ChecksumWriter ChecksumWriter
}

Checker provides the functionality for verifying that read data is correct.

func NewContentChecker

func NewContentChecker(content Content, sizeTracker SizeTracker, checksumWriter ChecksumWriter) *Checker

NewContentChecker returns a Checker for the provided data.

func (Checker) Verify

func (c Checker) Verify() error

Verify implements ContentChecker.

func (Checker) WrapReader

func (c Checker) WrapReader(reader io.Reader) io.Reader

WrapReader implements ContentChecker.

type ChecksumWriter

type ChecksumWriter interface {
	io.Writer

	// Fingerprint is the fingerprint for the tracked checksum.
	Fingerprint() charmresource.Fingerprint
}

ChecksumWriter tracks the checksum of all written bytes.

type Content

type Content struct {
	// Data holds the resource content, ready to be read (once).
	Data io.Reader

	// Size is the byte count of the data.
	Size int64

	// Fingerprint holds the checksum of the data.
	Fingerprint charmresource.Fingerprint
}

Content holds a reader for the content of a resource along with details about that content.

func (Content) Verify

func (c Content) Verify(size int64, fp charmresource.Fingerprint) error

Verify ensures that the actual resource content details match the expected ones.

type ContentChecker

type ContentChecker interface {
	// WrapReader wraps the provided reader in another reader
	// that tracks the read data.
	WrapReader(io.Reader) io.Reader

	// Verify fails if the tracked data does not match
	// the expected data.
	Verify() error
}

ContentChecker exposes functionality for verifying the data read from a reader.

type ContentSource

type ContentSource interface {
	// Content returns the content for the opened resource.
	Content() Content

	// Info returns the info for the opened resource.
	Info() resource.Resource
}

ContentSource represents the functionality of OpenedResource, relative to Content.

type ContextDirectorySpec

type ContextDirectorySpec interface {
	Resolver

	// Initializeprepares the target directory and returns it.
	Initialize() (DownloadDirectory, error)

	// IsUpToDate indicates whether or not the resource dir is in sync
	// with the content.
	IsUpToDate(Content) (bool, error)
}

ContextDirectorySpec exposes the functionality of a resource dir spec in a hook context.

func NewContextDirectorySpec

func NewContextDirectorySpec(dataDir, name string, deps DirectorySpecDeps) ContextDirectorySpec

NewContextDirectorySpec returns a new directory spec for the context.

type ContextDownloadDeps

type ContextDownloadDeps interface {
	// NewContextDirectorySpec returns the dir spec for the resource
	// in the hook context.
	NewContextDirectorySpec() ContextDirectorySpec

	// OpenResource reads the resource info and opens the resource
	// content for reading.
	OpenResource() (ContextOpenedResource, error)

	// CloseAndLog closes the closer and logs any error.
	CloseAndLog(io.Closer, string)

	// Download writes the remote to the target directory.
	Download(DownloadTarget, ContextOpenedResource) error
}

ContextDownloadDeps provides the externally defined functions on which ContextDownload depends. The functionality all relates to a single resource.

type ContextDownloadDirectory

type ContextDownloadDirectory struct {
	*TempDirectorySpec
}

ContextDownloadDirectory is an adapter for TempDirectorySpec.

func (ContextDownloadDirectory) Initialize

func (dir ContextDownloadDirectory) Initialize() (DownloadDirectory, error)

Initialize implements DownloadTarget.

type ContextOpenedResource

type ContextOpenedResource interface {
	ContentSource
	io.Closer
}

ContextOpenedResource exposes the functionality of an "opened" resource.

type Directory

type Directory struct {
	*DirectorySpec

	// Deps holds the external dependencies of the directory.
	Deps DirectoryDeps
}

Directory represents a resource directory.

func NewDirectory

func NewDirectory(spec *DirectorySpec, deps DirectoryDeps) *Directory

NewDirectory returns a new directory for the provided spec.

func (*Directory) Write

func (dir *Directory) Write(opened ContentSource) error

Write writes all relevant files from the given source to the directory.

func (*Directory) WriteContent

func (dir *Directory) WriteContent(relPath string, content Content) error

WriteContent writes the resource file to the given path within the directory.

type DirectoryDeps

type DirectoryDeps interface {
	// CreateWriter creates a new writer to which the resource file
	// will be written.
	CreateWriter(string) (io.WriteCloser, error)

	// CloseAndLog closes the closer and logs any error.
	CloseAndLog(io.Closer, string)

	// WriteContent writes the content to the directory.
	WriteContent(io.Writer, Content) error
}

DirectoryDeps exposes the external functionality needed by Directory.

type DirectorySpec

type DirectorySpec struct {
	// Name is the resource name.
	Name string

	// Dirname is the path to the resource directory.
	Dirname string

	// Deps is the external dependencies of DirectorySpec.
	Deps DirectorySpecDeps
}

DirectorySpec identifies information for a resource directory.

func NewDirectorySpec

func NewDirectorySpec(dataDir, name string, deps DirectorySpecDeps) *DirectorySpec

NewDirectorySpec returns a new directory spec for the given info.

func (DirectorySpec) Initialize

func (spec DirectorySpec) Initialize() (*Directory, error)

Initialize preps the spec'ed directory and returns it.

func (DirectorySpec) IsUpToDate

func (spec DirectorySpec) IsUpToDate(content Content) (bool, error)

IsUpToDate determines whether or not the content matches the resource directory.

func (DirectorySpec) Resolve

func (spec DirectorySpec) Resolve(path ...string) string

Resolve returns the fully resolved file path, relative to the directory.

type DirectorySpecDeps

type DirectorySpecDeps interface {
	DirectoryDeps

	// FingerprintMatches determines whether or not the identified file
	// exists and has the provided fingerprint.
	FingerprintMatches(filename string, fp charmresource.Fingerprint) (bool, error)

	// Join exposes the functionality of filepath.Join().
	Join(...string) string

	// MkdirAll exposes the functionality of os.MkdirAll().
	MkdirAll(string) error
}

DirectorySpecDeps exposes the external depenedencies of DirectorySpec.

type DownloadDirectory

type DownloadDirectory interface {
	Resolver

	// Write writes all the relevant files for the provided source
	// to the directory.
	Write(ContentSource) error
}

DownloadDirectory exposes the functionality of a resource directory needed by Download().

type DownloadTarget

type DownloadTarget interface {
	// Initialize prepares the target directory and returns it.
	Initialize() (DownloadDirectory, error)
}

DownloadTarget exposes the functionality of a directory spec needed by Download().

type Logger

type Logger interface {
	// Errorf formats the provided log message and writes it to the log.
	Errorf(string, ...interface{})
}

Logger exposes the logger functionality needed by CloseAndLog.

type NopChecker

type NopChecker struct{}

NopChecker is a ContentChecker that accepts all data.

func (NopChecker) Verify

func (NopChecker) Verify() error

Verify implements ContentChecker.

func (NopChecker) WrapReader

func (NopChecker) WrapReader(reader io.Reader) io.Reader

WrapReader implements ContentChecker.

type OpenedResource

type OpenedResource struct {
	resource.Resource
	io.ReadCloser
}

OpenedResource wraps the resource info and reader returned from the API.

func OpenResource

func OpenResource(name string, client OpenedResourceClient) (*OpenedResource, error)

OpenResource opens the identified resource using the provided client.

func (OpenedResource) Content

func (or OpenedResource) Content() Content

Content returns the "content" for the opened resource.

func (OpenedResource) Info

func (or OpenedResource) Info() resource.Resource

Info returns the info for the opened resource.

type OpenedResourceClient

type OpenedResourceClient interface {
	// GetResource returns the resource info and content for the given
	// name (and unit-implied application).
	GetResource(resourceName string) (resource.Resource, io.ReadCloser, error)
}

OpenedResourceClient exposes the API functionality needed by OpenResource.

type Resolver

type Resolver interface {
	// Resolve returns the fully resolved path for the provided path items.
	Resolve(...string) string
}

Resolver exposes the functionality of DirectorySpec needed by DownloadIndirect.

type SizeTracker

type SizeTracker interface {
	io.Writer

	// Size returns the number of bytes written.
	Size() int64
}

SizeTracker tracks the number of bytes written.

type TempDirDeps

type TempDirDeps interface {
	DirectorySpecDeps

	// NewTempDir returns the path to a new temporary directory.
	NewTempDir() (string, error)

	// RemoveDir deletes the specified directory.
	RemoveDir(string) error
}

TempDirDeps exposes the external functionality needed by NewTempDirectorySpec().

type TempDirectorySpec

type TempDirectorySpec struct {
	*DirectorySpec

	// CleanUp cleans up the temp directory in which the resource
	// directory is placed.
	CleanUp func() error
}

TempDirectorySpec represents a resource directory placed under a temporary data dir.

func NewTempDirectorySpec

func NewTempDirectorySpec(name string, deps TempDirDeps) (*TempDirectorySpec, error)

NewTempDirectorySpec creates a new temp directory spec for the given resource.

func (TempDirectorySpec) Close

func (spec TempDirectorySpec) Close() error

Close implements io.Closer.

type WriteContentDeps

type WriteContentDeps interface {
	//NewChecker provides a content checker for the given content.
	NewChecker(Content) ContentChecker

	// Copy copies the data from the reader into the writer.
	Copy(io.Writer, io.Reader) error
}

WriteContentDeps exposes the external functionality needed by WriteContent.

Jump to

Keyboard shortcuts

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