revolver

package module
v0.0.0-...-605f98c Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 11 Imported by: 0

README

Build Status Coverage Status Go Report Card

revolver

A simple reloader tool written in Go.

Build

go build ./cmd/revolver

Test

go test ./...

Usage

When starting revolver it looks for a file called revolver.yml in the current directory. This is a simple yaml file with the configuration parameters for running revolver. The revolver.yml file can be checked into version control repositories.

A full featured sample revolver.yml file looks like the following:

dir: "."
excludeDir: [".git", "other"]
interval: 1s
action:
  - name: "build"
    pattern: ["**/*.go", "**/*.yml"]
    exclude: ["exclude", "**/exclude*"]
    build: ["build cmd 1", "build cmd 2"]
    run: "run cmd"
  - name: "test"
    pattern: ["**/*_test.go", "**/.*"]
    build: ["build cmd 1", "build cmd 2"]

The configuration file aims to be quite flexible.

If a string array type contains only one element, it can be written as a simple string value:

dir: "."
excludeDir: ".git"
interval: 1s
action:
  - name: "build"
    pattern: "**/*.go"
    exclude: "exclude"
    build: "build cmd 1"
    run: "run cmd"
  - name: "test"
    pattern: "**/*_test.go"
    build: "build cmd 1"

You can omit most of the parameters, the only requirement is to have at least one action with a build or run command:

action:
  - build: ["go install"]

If only one action is needed, the action list can be omited and the values can be written in the top level of the config file:

dir: "src"
excludeDir: ".git"
interval: 1s
pattern: "**/*.go"
exclude: "exclude"
build: "build cmd 1"
run: "run cmd"

A minimal sample revolver.yml file looks like the following:

build: "go install"

Config options:

Name Type Default value
dir string . (current dir)
excludeDir []string []
interval  duration 500ms
action []Action []

Action options:

Name Type Default value
name string
pattern []string [**/*]
exclude []string []
build []string []
run string

Revolver can also be used without a config file. If a build(-b) or run(-r) command line flag is present, it will ignore the config file and configure the application with the specified flags instead. It is possible to add multiple excludeDir(-ed), patter (-p), exclude(-e) and build(-b) flags (ex: revolver -b "echo 1" -b "echo 2"').

The following flags can be used:

Usage of revolver:
  -b value
        Build commands
  -c string
        Path to config file (default "revolver.yml")
  -d string
        Directory to watch
  -e value
        File watch exclude patterns
  -ed value
        Excluded directories
  -i duration
        Poll interval
  -p value
        File watch patterns
  -r string
        Run command
File patterns

File patterns are supported for the pattern, exclude and excludeDir options. The pattern options defaults to every file in every directory (**/*), the exclude and excludeDir options are empty by default.

The following special terms are supported in the patterns:

Special Terms Meaning
* matches any sequence of non-path-separators
** matches any sequence of characters, including path separators
? matches any single non-path-separator character
[class] matches any single non-path-separator character against a class of characters
{alt1,...} matches a sequence of characters if one of the comma-separated alternatives matches

Any character with a special meaning can be escaped with a backslash (\).

Character classes support the following:

Class Meaning
[abc] matches any single character within the set
[a-z] matches any single character in the range
[^class] matches any single character which does not match the class
Actions

You can specify multiple actions with different file watch patterns that execute different commands.

Build commands

Build commands are commands that are executed when a file changes. Build commands are executed in order and if any of them errors out, the execution chain stops.

Run commands

Run commands are long running processes that are started when all the build commands are successfully executed. They are killed and restarted every time a file changes.

License

Authored by Kristóf Szabó and released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(builds []BuildFunc, run RunFunc) (func(), error)

Run executes the build and run functions. All build functions are executed before the run function. It returns an error and stops the executions if an error happens. Otherwise it returns a function to stop the run function's execution.

func Watch

func Watch(config Config) error

Watch runs commands based on file changes.

Types

type Action

type Action struct {
	Name            string    `yaml:"name,omitempty"`
	Patterns        stringArr `yaml:"pattern,omitempty"`
	ExcludePatterns stringArr `yaml:"exclude,omitempty"`
	BuildCommands   stringArr `yaml:"build,omitempty"`
	RunCommand      string    `yaml:"run,omitempty"`
}

Action is a block in a Config file

type BuildFunc

type BuildFunc func() error

BuildFunc is a function that is executed before a RunFunc

func BuildCommand

func BuildCommand(command string, args ...string) BuildFunc

BuildCommand returns a BuildFunc that can execute a command with arguments.

type Config

type Config struct {
	Dir         string        `yaml:"dir,omitempty"`
	ExcludeDirs stringArr     `yaml:"excludeDir,omitempty"`
	Interval    time.Duration `yaml:"interval,omitempty"`
	Actions     []Action      `yaml:"action"`
}

Config holds all the configuration for running revolver.

func ParseFlags

func ParseFlags(args []string) (*Config, error)

ParseFlags parses a Config from command line flags, validates it and sets the default values. If no build(b) or run(r) flags are found it will parse the config from a yaml file based on the configFile(c) flag.

type DetectFunc

type DetectFunc func() []string

DetectFunc detects changes in a filesystem and returns the changed files.

func Detect

func Detect(dir string, excludeDirs []string) DetectFunc

Detect returns a DetectFunc that will walk the filesystem from the given dir recursively, skipping the excludeDirs and return the changed files.

type FilterFunc

type FilterFunc func(files []string) bool

FilterFunc can filter files.

func Filter

func Filter(includePatterns, excludePatterns []string) FilterFunc

Filter returns a FilterFunc that can filter files based on include and exclude patterns.

type RunFunc

type RunFunc func() (stop func(), err error)

RunFunc is a function that runs like a daemon and can be stopped with the returned stop function.

func RunCommand

func RunCommand(command string, args ...string) RunFunc

RunCommand returns a RunFunc that can start a command line app with arguments. It returns a function that can kill the started process.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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