kickoff

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultConfigFileName is the default name kickoff's user config file.
	DefaultConfigFileName = "config.yaml"
	// SkeletonConfigFileName is the name of the file that is searched to
	// file skeletons and their config.
	SkeletonConfigFileName = ".kickoff.yaml"
	// SkeletonTemplateExtension is the file extension for template files
	// within kickoff skeletons. Although kickoff template files are
	// gotemplates, we must not use .tmpl as user may want to include their own
	// gotemplate files in skeletons which must not be evaluated by kickoff,
	// hence we use .skel to avoid issues here.
	SkeletonTemplateExtension = ".skel"
	// SkeletonsDir is the subdirectory of a repository where skeletons
	// can be found.
	SkeletonsDir = "skeletons"
)
View Source
const (
	// DefaultProjectHost denotes the default git host that is passed to
	// templates so that project related urls can be rendered in files like
	// READMEs.
	DefaultProjectHost = "github.com"
	// DefaultRepositoryName is the name of the default skeleton repository.
	DefaultRepositoryName = "default"
	// DefaultSkeletonName is the name of the default skeleton in a repository.
	DefaultSkeletonName = "default"
)
View Source
const (
	// NoLicense means that no license file will be generated for a new
	// project.
	NoLicense = "none"
	// NoGitignore means that no .gitignore file will be generated for a new
	// project.
	NoGitignore = "none"
)

Variables

View Source
var (
	// LocalConfigDir points to the user's local configuration dir which is
	// platform specific.
	LocalConfigDir = configdir.LocalConfig("kickoff")
	// DefaultConfigPath holds the default kickof config path in the user's
	// local config directory.
	DefaultConfigPath = filepath.Join(LocalConfigDir, DefaultConfigFileName)
	// DefaultRepositoryURL is the url of the default skeleton repository if
	// the user did not configure anything else.
	DefaultRepositoryURL = "https://github.com/martinohmann/kickoff-skeletons"
	// LocalCacheDir points to the user's local cache dir which is
	// platform specific.
	LocalCacheDir = configdir.LocalCache("kickoff")

	LocalRepositoryCacheDir = filepath.Join(LocalCacheDir, "repositories")
)
View Source
var ErrMergeEmpty = errors.New("cannot merge empty list of skeletons")

ErrMergeEmpty is returned by MergeSkeletons if no skeletons were passed.

Functions

func Load

func Load(path string, v interface{}) error

Load loads a file from path into v. Returns an error if reading the file fails. Does not perform any defaulting or validation.

func Save

func Save(path string, v interface{}) error

Save saves v to path.

func SaveConfig

func SaveConfig(path string, config *Config) error

SaveConfig saves config to path.

Types

type BufferedFile

type BufferedFile struct {
	// RelPath is the file path relative to root directory of the skeleton.
	// This is used to construct the path for the file relative to the the
	// target project directory.
	RelPath string
	// Content holds the file contents.
	Content []byte
	// FileMode is the os.FileMode for the file. This provides information
	// about the type of file, e.g. whether it is a directory or not.
	FileMode os.FileMode
}

BufferedFile is a file that is already buffered in memory.

func NewBufferedFile

func NewBufferedFile(relPath string, content []byte, mode os.FileMode) *BufferedFile

NewBufferedFile creates a new *BufferedFile.

func (*BufferedFile) IsTemplate

func (f *BufferedFile) IsTemplate() bool

IsTemplate implements the File interface.

func (*BufferedFile) Mode

func (f *BufferedFile) Mode() os.FileMode

Mode implements the File interface.

func (*BufferedFile) Path

func (f *BufferedFile) Path() string

Path implements the File interface.

func (*BufferedFile) Reader

func (f *BufferedFile) Reader() (io.Reader, error)

Reader implements the File interface.

type Config

type Config struct {
	// Project holds default configuration values like default project host and
	// owner which are the same for new projects most of the time. Can be
	// overridden on project creation.
	Project ProjectConfig `json:"project,omitempty"`
	// Repositories holds a map of configured repositories to search for
	// available skeletons. Keys are the locally configured names for these
	// repositories.
	Repositories map[string]string `json:"repositories,omitempty"`
	// Values holds user-defined values that get merged on to of skeleton
	// values.
	Values template.Values `json:"values,omitempty"`
}

Config describes the schema for the local user-defined kickoff configuration file.

func LoadConfig

func LoadConfig(path string) (*Config, error)

Load loads the config from path and returns it.

func (*Config) ApplyDefaults

func (c *Config) ApplyDefaults()

ApplyDefaults applies default values to the config.

func (*Config) MergeFromFile

func (c *Config) MergeFromFile(path string) error

MergeFromFile loads the config from path and merges it into c. Returns any errors that may occur during loading or merging. Non-zero fields in c will not be overridden if present in the file at path.

func (*Config) Validate

func (c *Config) Validate() error

Validate implements the Validator interface.

type Defaulter

type Defaulter interface {
	// ApplyDefaults sets unset fields of the data structure to its default
	// values which might not necessarily be the zero value.
	ApplyDefaults()
}

Defaulter can set defaults for unset fields.

type File

type File interface {
	// Path must return the path relative to the new project root.
	Path() string

	// Mode returns the mode of the file. The target in the project directory
	// will be created using this mode. The mode is also used to determine if
	// the file is regular or a directory.
	Mode() os.FileMode

	// Reader provides an io.Reader to read the contents of the file.
	Reader() (io.Reader, error)

	// IsTemplate returns true if the source is a template.
	IsTemplate() bool
}

File is the interface for a file that should be created in new projects.

type FileRef

type FileRef struct {
	// RelPath is the file path relative to root directory of the skeleton.
	// This is used to construct the path for the file relative to the the
	// target project directory.
	RelPath string `json:"relPath"`
	// AbsPath is the absolute path to the file on disk.
	AbsPath string `json:"absPath"`
	// FileMode is the os.FileMode for the file. This provides information
	// about the type of file, e.g. whether it is a directory or not.
	FileMode os.FileMode `json:"mode"`
}

FileRef contains paths and other information about a skeleton file. May also reference a directory with templated path segments.

func (*FileRef) IsTemplate

func (r *FileRef) IsTemplate() bool

IsTemplate implements the File interface.

func (*FileRef) Mode

func (r *FileRef) Mode() os.FileMode

Mode implements the File interface.

func (*FileRef) Path

func (r *FileRef) Path() string

Path implements the File interface.

func (*FileRef) Reader

func (r *FileRef) Reader() (io.Reader, error)

Reader implements the File interface.

type ParentRef

type ParentRef struct {
	// SkeletonName holds the name of the parent skeleton. May include slashes
	// if the skeleton is organized in a subdirectory relative to the skeletons
	// root.
	SkeletonName string `json:"skeletonName"`
	// RepositoryURL can be a local path or are remote url to a skeleton
	// repository. If empty the parent is assumed to be in the same repository
	// as the child.
	RepositoryURL string `json:"repositoryURL,omitempty"`
}

ParentRef is a reference to a parent skeleton, possibly in a different repository.

func (*ParentRef) Validate

func (r *ParentRef) Validate() error

Validate implements the Validator interface.

type ProjectConfig

type ProjectConfig struct {
	// Host holds the default git host's domain, e.g. 'github.com'.
	Host string `json:"host,omitempty"`
	// Owner holds the default repository owner name.
	Owner string `json:"owner,omitempty"`
	// License holds the name of the default open source license.
	License string `json:"license,omitempty"`
	// Gitignore holds a comma-separated list of gitignore templates, e.g.
	// 'go,hugo'.
	Gitignore string `json:"gitignore,omitempty"`
}

ProjectConfig contains project specific configuration like git host, owner and project name.

func (*ProjectConfig) ApplyDefaults

func (p *ProjectConfig) ApplyDefaults()

ApplyDefaults applies defaults to unset fields. If the Owner field is empty ApplyDefaults will attempt to fill it with the git config values of github.user or user.name if exists.

func (*ProjectConfig) Validate

func (c *ProjectConfig) Validate() error

Validate implements the Validator interface.

type RepoRef

type RepoRef struct {
	// Name holds the optional local name for the repository.
	Name string `json:"name,omitempty"`
	// URL to the repository, e.g. 'https://github.com/foobar/baz' or
	// `/some/local/path`.
	URL string `json:"url,omitempty"`
	// Path is the path to a local repository.
	Path string `json:"path,omitempty"`
	// Revision holds the revision of the remote git repository to checkout.
	// This can be a branch, tag or commit SHA.
	Revision string `json:"revision,omitempty"`
}

RepoRef holds information about a skeleton repository's location.

func ParseRepoRef

func ParseRepoRef(rawurl string) (*RepoRef, error)

ParseRepoRef parses a raw repository url and returns a repository ref describing a local or remote skeleton repository. The rawurl parameter must be either a local path or a remote url to a git repository. Remote url may optionally include a `revision` query parameter. If absent, `master` will be assumed. Returns an error if url does not match any of the criteria mentioned above.

func (*RepoRef) IsEmpty

func (r *RepoRef) IsEmpty() bool

IsEmpty return true if l is empty, that is: it neither describes a local nor remote repository.

func (*RepoRef) IsRemote

func (r *RepoRef) IsRemote() bool

IsRemote returns true if the repo ref describes a remote repository.

func (*RepoRef) String

func (r *RepoRef) String() string

String implements fmt.Stringer.

func (*RepoRef) Validate

func (r *RepoRef) Validate() error

Validate implements the Validator interface.

type Repository

type Repository interface {
	// GetSkeleton retrieves information about a skeleton from the
	// repository. The passed in context is propagated to all operations that
	// cross API boundaries (e.g. git operations) and can be used to enforce
	// timeouts or cancel them. Returns an error of type SkeletonNotFoundError
	// if the named skeleton was not found in the repository.
	GetSkeleton(ctx context.Context, name string) (*SkeletonRef, error)

	// ListSkeletons retrieves information about all skeletons in the
	// repository. The passed in context is propagated to all operations that
	// cross API boundaries (e.g. git operations) and can be used to enforce
	// timeouts or cancel them. If the repository is empty, ListSkeletons
	// will return an empty slice.
	ListSkeletons(ctx context.Context) ([]*SkeletonRef, error)
}

Repository is the interface for a skeleton repository.

type Skeleton

type Skeleton struct {
	// Description is the skeleton description text obtained from the skeleton
	// config.
	Description string `json:"description,omitempty"`
	// Ref holds the information about the location that was used to load the
	// skeleton. May be nil if the skeleton is the merged result of composing
	// multiple skeletons.
	Ref *SkeletonRef `json:"ref,omitempty"`
	// Parent points to the parent skeleton if there is one.
	Parent *Skeleton `json:"parent,omitempty"`
	// The Files slice contains a merged and sorted list of file references
	// that includes all files from the skeleton and its parents (if any).
	Files []File `json:"files,omitempty"`
	// Values are the template values from the skeleton's metadata merged with
	// those of it's parents (if any).
	Values template.Values `json:"values,omitempty"`
}

Skeleton is the representation of a skeleton loaded from a skeleton repository.

func MergeSkeletons

func MergeSkeletons(skeletons ...*Skeleton) (*Skeleton, error)

MergeSkeletons merges multiple skeletons together and returns a new *Skeleton. The skeletons are merged left to right with template values, skeleton files and skeleton info of the rightmost skeleton taking preference over already existing values. Template values are recursively merged and may cause errors on type mismatch. The resulting *Skeleton will have the second-to-last skeleton set as its parent, the second-to-last will have the third-to-last as parent and so forth. The original skeletons are not altered. If only one skeleton is passed it will be returned as is without modification. Passing a slice with length of zero will return in an error.

func (*Skeleton) Merge

func (s *Skeleton) Merge(other *Skeleton) (*Skeleton, error)

Merge merges two skeletons. The skeletons are merged left to right with template values, skeleton files and skeleton ref of the rightmost skeleton taking preference over already existing values. Template values are recursively merged and may cause errors on type mismatch. The resulting *Skeleton will have the lhs skeleton set as its parent. The original skeletons are not altered.

func (*Skeleton) String

func (s *Skeleton) String() string

String implements fmt.Stringer.

type SkeletonConfig

type SkeletonConfig struct {
	// Description holds a decription of the skeleton that can give some more
	// user-defined hints on the skeleton usage, e.g. interesting values to
	// tweak.
	Description string `json:"description,omitempty"`
	// Parent references an optional parent skeleton.
	Parent *ParentRef `json:"parent,omitempty"`
	// Values holds user-defined values available in .skel templates.
	Values template.Values `json:"values,omitempty"`
}

SkeletonConfig describes the schema of a skeleton's .kickoff.yaml that is bundled together with the skeleton.

func LoadSkeletonConfig

func LoadSkeletonConfig(path string) (*SkeletonConfig, error)

LoadSkeletonConfig loads the skeleton config from path and returns it.

func (*SkeletonConfig) Validate

func (c *SkeletonConfig) Validate() error

Validate implements the Validator interface.

type SkeletonRef

type SkeletonRef struct {
	// Name of the skeleton. May include slashes if the skeleton is organized
	// in a subdirectory relative to the skeletons root.
	Name string `json:"name"`
	// Path contains the local path to the skeleton.
	Path string `json:"path"`
	// Repository references the repository where the skeleton can be found.
	Repo *RepoRef `json:"repo"`
}

SkeletonRef holds information about the location of a skeleton.

func (*SkeletonRef) LoadConfig

func (r *SkeletonRef) LoadConfig() (*SkeletonConfig, error)

LoadConfig loads the skeleton config for the info.

func (*SkeletonRef) String

func (r *SkeletonRef) String() string

String implements fmt.Stringer.

func (*SkeletonRef) Validate

func (r *SkeletonRef) Validate() error

Validate implements the Validator interface.

type ValidationError

type ValidationError struct {
	Context string
	Err     error
}

ValidationError wraps all errors that occur during validation.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

type Validator

type Validator interface {
	// Validate returns an error if the data structure contains invalid values.
	Validate() error
}

Validator can validate itself to ensure the absence of invalid values.

Jump to

Keyboard shortcuts

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