godemon

package module
v0.0.0-...-e616e65 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: Unlicense Imports: 16 Imported by: 0

README

Golang gopher mascot with little horns

godemon

go test

godemon is a tool that runs a command, monitors for changes to files, and restarts the command automatically whenever files change.

It is great for:

  • Restarting a development server whenever you change source code
  • Running tests whenever you change source code
  • Mirroring a directory to another location, using a tool like rsync
  • Lots of other things!

It is inspired by nodemon which is a similar tool written in JavaScript.

Goals

  • Generic: Does not give preference to any language or framework (even Go). Can run arbitrary comands when changing arbitrary files.
  • Configurable: There is no "one size fits all" approach to file watchers, so godemon gives you fine-grained control over the paths that are included or ignored. For more advanced users, it also lets you control which signals are used to restart your process, since different processes respond differently to different signals.
  • Strong defaults: godemon ships with a default ignore list and also treats .gitignore files as additional ignore patterns. These defaults can be turned off via config options.

Status

godemon has been tested on Mac and Linux. It has not yet been tested on Windows.

The command line interface and config file format are not yet stable.

Installation

The easiest way to install godemon is with the go CLI:

go install github.com/bduffany/godemon/cmd/godemon@latest

Basic usage

The godemon command can be used as follows:

godemon [options ...] <command> [args ...]

By default, the given command is executed whenever any file in the current directory changes.

Examples

To watch the current directory (recursively) and run a command on any change:

godemon command

To watch multiple directories, use --watch or -w. When using this flag, the current directory needs to be passed explicitly, otherwise it's assumed that you only want to watch /some/other/dir (in this example).

godemon --watch . --watch /some/other/dir command

To only restart on changes to certain paths or patterns, use --only or -o:

godemon --only '*.ts' --only '*.tsx' command

To ignore changes to certain paths or patterns, use --ignore or -i:

godemon --ignore '**/build/**' --ignore '**/dist/**' command

Default ignored patterns

The default ignored patterns are listed in default_ignore.go.

To disable the default ignore list, pass --no-default-ignore.

By default, godemon also ignores patterns that are listed in .gitignore files for any watched paths which are contained in a Git repository. To disable this behavior, pass --no-gitignore.

Documentation

Overview

TODO: Split up this package

Index

Constants

View Source
const (
	// DefaultNotifySignal is the default signal sent to a command on changes
	// (in order to get it to restart).
	DefaultNotifySignal = syscall.SIGTERM
)

Variables

This section is empty.

Functions

func ChildPids

func ChildPids(pid int) ([]int, error)

ChildPids returns all *direct* child pids of a process identified by pid.

func ExponentialBackoff

func ExponentialBackoff(start, max time.Duration, factor float64) func() <-chan time.Time

func KillProcessTree

func KillProcessTree(pid int) error

KillProcessTree kills the given pid as well as any descendant processes.

It tries to kill as many processes in the tree as possible. If it encounters an error along the way, it proceeds to kill subsequent pids in the tree. It returns the last error encountered, if any.

func Main

func Main(args []string)

func Tree

func Tree(pid int) (map[int]ps.Process, error)

func TreePids

func TreePids(pid int) ([]int, error)

TreePids returns all the pids in the tree rooted at the given pid, including the pid itself.

Types

type Cmd

type Cmd struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

func (*Cmd) Shutdown

func (c *Cmd) Shutdown(s syscall.Signal) error

func (*Cmd) Signal

func (c *Cmd) Signal(s syscall.Signal) error

func (*Cmd) Start

func (c *Cmd) Start() error

func (*Cmd) Wait

func (c *Cmd) Wait() error

type Config

type Config struct {
	// Command is the command and arguments to be executed upon any change.
	//
	// The command is not interpreted using a shell. If you would like shell
	// features such as environment variable expansion, specify the command
	// using something like the following:
	//
	//     ["sh", "-c", "$YOUR_COMMAND"]
	Command []string `json:"command"`

	// Watch specifies a list of files or directories to be watched. Defaults to
	// the current working directory from which the command is invoked.
	// Directories are watched recursively.
	// TODO(bduffany): Accept glob patterns here.
	Watch []string `json:"watch,omitempty"`
	// Only specifies a list of allowed patterns. If non-empty, at least one
	// pattern must match in order for the command to be executed.
	Only []string `json:"only,omitempty"`
	// Ignore specifies a list of paths to be ignored. Glob patterns are supported.
	Ignore []string `json:"ignore,omitempty"`

	// UseDefaultIgnoreList specifies whether to use the default list of
	// ignore patterns. These will be appended to the list of ignore patterns.
	// Defaults to true.
	UseDefaultIgnoreList *bool `json:"useDefaultIgnoreList,omitempty"`
	// UseGitignore specifies whether to respect .gitignore files when watching
	// directories.
	// Defaults to true.
	UseGitignore *bool `json:"useGitignore,omitempty"`

	// NotifySignal is the signal used to notify the command of file changes.
	// Defaults to "SIGINT" (Ctrl+C), which should gracefully stop most
	// well-behaved commands.
	NotifySignal *string `json:"notifySignal,omitempty"`

	// Lockfile specifies a path to a file which, if it exists, will cause
	// file-based restarts to be paused until the file is removed. This can be
	// used to temporarily pause godemon's file-watching behavior while running a
	// batch of updates, then finally trigger a restart only once all the updates
	// are complete.
	Lockfile *string `json:"lockfile,omitempty"`

	// Clear specifies whether to clear the terminal on each re-run.
	//
	// Note, this option is just for convenience. The same functionality can
	// be achieved with the following command:
	// godemon [ flags ... ] sh -c 'clear && exec {command}'
	Clear bool `json:"clear,omitempty"`
	// contains filtered or unexported fields
}

type TreeSnapshot

type TreeSnapshot struct {
	Processes map[int]ps.Process
}

TreeSnapshot represents a snapshot of the process tree rooted at the current process tree.

func NewTreeSnapshot

func NewTreeSnapshot() (*TreeSnapshot, error)

func (*TreeSnapshot) Diff

func (s *TreeSnapshot) Diff() (*TreeSnapshot, error)

Diff returns a snapshot containing only processes which are different from the previous snapshot.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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