source

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package source provides the Source interface. All source drivers must implement this interface, register themselves, optionally provide a `WithInstance` function and pass the tests in package source/testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func List

func List() []string

List lists the registered drivers

func Register

func Register(name string, driver Driver)

Register globally registers a driver.

Types

type DirectoryReader

type DirectoryReader struct {
	DirectoryPath  string
	FileReaders    []*FileReader
	SubDirectories []*DirectoryReader
}

type Driver

type Driver interface {
	// Open returns a a new driver instance configured with parameters
	// coming from the URL string.
	Open(url string) (Driver, error)

	// Close closes the underlying source instance managed by the driver.
	Close() error

	// ReadFiles returns `FileReader` slice for the file at the given relative directory
	// that match the given pattern. Returns `nil` if relative path does not exist.
	ReadFiles(relativeDir string, fileNamePattern string) (files []*FileReader, err error)

	// ReadDirectories returns `DirectoryReader` slice for the directories at the given relative directory
	// that match the given pattern. Returns `nil` if relative path does not exist.
	ReadDirectories(relativeDir string, dirNamePattern string) (files []*DirectoryReader, err error)

	// ReadTree returns a `DirectoryReader` for directory at the given relative path
	// with the `SubDirectories` member recursively populated. Returns `nil` if the path
	// does not exist.
	ReadTree(relativeDir string, fileNamePattern string) (tree *DirectoryReader, err error)
}

Driver is the interface every source driver must implement.

How to implement a source driver?

  1. Implement this interface.
  2. Optionally, add a function named `WithInstance`. This function should accept an existing source instance and a Config{} struct and return a driver instance.
  3. Add a test that calls source/testing.go:Test()
  4. Add own tests for Open(), WithInstance() (when provided) and Close(). All other functions are tested by tests in source/testing. Saves you some time and makes sure all source drivers behave the same way.
  5. Call Register in init().

Guidelines:

  • All configuration input must come from the URL string in func Open() or the Config{} struct in WithInstance. Don't os.Getenv().
  • Drivers are supposed to be read only.
  • Ideally don't load any contents (into memory) in Open or WithInstance.

func Open

func Open(url string) (Driver, error)

Open returns a new driver instance.

type FileReader

type FileReader struct {
	FilePath string
	Reader   io.ReadCloser
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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