core

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package core provides the verless business logic.

Each verless sub-command maintains its own file in the core package, like build.go for `verless build`. This file provides a function in the form Run<Sub-Command>, e.g. RunBuild, serving as an entry point for other packages.

Sub-commands of sub-commands like `verless create project` don't require an own file, they can just be grouped together in a common file like create.go.

An entry point function either implements the business logic itself like RunVersion does, or prepares components and passes them to an inner core package for more complex scenarios.

Typically, a verless command has multiple options. These options are types in the core package, declared in the command's file. They have to be initialized by the caller - for example the cli package - and then passed to the entry point function. The name of an option type must be in the form <Sub-Command>Options like BuildOptions. Options for sub-commands of sub-commands like `verless create project` must have a name in the form <Sub-Command><Sub-Command>Options, like CreateProjectOptions.

As a result, most entry point functions accept a dedicated options instance. Some of them also require an initialized config instance or fixed command line arguments - see RunBuild as an example.

Core functions typically shouldn't have outgoing dependencies except for dependencies like the fs or model packages. Instead, they should define their dependencies as interfaces which can be implemented by outside packages. A good example for this is build.Parser implemented by parser.Markdown.

It is the entry point function's job to initialize those external dependencies, like calling parser.NewMarkdown, and passing it to the particular core function. Again, RunBuild is good example for this.

Package watch provides verless' ability to watch a project and react to changes in a verless project.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCannotOverwrite states that verless isn't allowed to delete or
	// overwrite the output directory.
	ErrCannotOverwrite = errors.New(`cannot overwrite the output directory.
Consider using the --overwrite flag or enable build.overwrite in verless.yml`)

	// ErrMissingVersionKey states that the top-level `version` key is
	// empty or missing in verless.yml.
	ErrMissingVersionKey = errors.New("missing `version` key in verless.yml")
)
View Source
var (
	// ErrProjectExists states that the specified project already exists.
	ErrProjectExists = errors.New("project already exists, use --overwrite to remove it")

	// ErrProjectNotExists states that the specified project doesn't exist.
	ErrProjectNotExists = errors.New("project doesn't exist yet, create it first")

	// ErrThemeExists states that the specified theme already exists.
	ErrThemeExists = errors.New("theme already exists, remove it first")

	// ErrFileExist states that the specified file already exists.
	ErrFileExists = errors.New("file already exists")
)

Functions

func CreateFile added in v0.4.5

func CreateFile(filePath string, options CreateFileOptions) error

CreateFile creates a file with specified path under content directory.

func CreateProject added in v0.4.4

func CreateProject(path string, options CreateProjectOptions) error

CreateProject creates a new verless project. If the specified project path already exists, CreateProject returns an error unless --overwrite has been used.

func CreateTheme added in v0.4.4

func CreateTheme(options CreateThemeOptions, name string) error

CreateTheme creates a new theme with the specified name inside the given path. Returns an error if it already exists, unless --overwrite has been used.

func RunVersion

func RunVersion(options VersionOptions) error

RunVersion prints verless version information.

func Serve added in v0.4.4

func Serve(path string, options ServeOptions) error

Serve serves a verless project using a simple file server. It can build the project automatically if ServeOptions.Build is true and even watch the whole project directory for changes if ServeOptions.Watch is true.

Types

type Build added in v0.4.4

type Build struct {
	Path    string
	Parser  Parser
	Builder Builder
	Writer  Writer
	Plugins []plugin.Plugin
	Types   map[string]*model.Type
	Options BuildOptions
}

Build provides methods for building a static site.

func NewBuild added in v0.4.4

func NewBuild(targetFs afero.Fs, path string, options BuildOptions) (*Build, error)

New initializes a new Build instance.

func (*Build) Run added in v0.4.4

func (b *Build) Run() error

Run executes the build using the provided build context.

The current build implementation runs the following steps:

  1. Read all files in the content directory and send them through a channel.
  2. Spawn workers reading from that channel.
  3. Process each received file: 3.1. Read the file as a []byte 3.2. Parse the []byte and convert it to a model.Page. 3.3. Register the page in the builder's site model. 3.4. Let each plugin process the page.
  4. Get the site model from the builder and render it as a website.
  5. Let each plugin finish its work, e.g. by writing a file.

type BuildOptions

type BuildOptions struct {
	// OutputDir sets the output directory. If this field is empty, config.OutputDir
	// will be used.
	OutputDir string
	// Overwrite specifies that the output folder can be overwritten.
	Overwrite bool
	// RecompileTemplates forces a recompilation of all templates.
	RecompileTemplates bool
}

BuildOptions represents options for running a verless build.

type Builder added in v0.4.4

type Builder interface {
	// RegisterPage must be safe for concurrent usage.
	RegisterPage(page model.Page) error
	Dispatch() (model.Site, error)
}

Builder represents a model builder that maintains a Site instance and registers all parsed pages in that instance.

type CreateFileOptions added in v0.4.5

type CreateFileOptions struct {
	Project string
}

CreateFileOptions represents project path for creating file.

type CreateProjectOptions

type CreateProjectOptions struct {
	Overwrite bool
}

CreateProjectOptions represents options for creating a project.

type CreateThemeOptions added in v0.4.5

type CreateThemeOptions struct {
	Project string
}

CreateThemeOptions represents project path for creating new theme.

type Parser added in v0.4.4

type Parser interface {
	// ParsePage must be safe for concurrent usage.
	ParsePage(src []byte) (model.Page, error)
}

Parser represents a parser that processes Markdown files and converts them into a model instance.

type ServeOptions added in v0.3.0

type ServeOptions struct {
	// BuildOptions stores all options for re-builds when watching the site.
	BuildOptions
	// Port specifies the port to run the server at.
	Port uint16
	// IP specifies the IP to listen on in combination with the port.
	IP net.IP
	// Watch enables automatic re-builds when a file changes.
	Watch bool
}

ServeOptions represents options for running a verless listenAndServe command.

type VersionOptions

type VersionOptions struct {
	// Quiet only prints the plain version number.
	Quiet bool
}

VersionOptions represents options for the version command.

type Writer added in v0.4.4

type Writer interface {
	Write(site model.Site) error
}

Writer represents a model writer that renders the site model as HTML using the corresponding templates.

Jump to

Keyboard shortcuts

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