ron

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: MIT Imports: 15 Imported by: 1

README

ron

Ron GoDoc Build Status

Package ron provides a command line interface to common build tasks.

$ ron
Usage: ron <command>

Available commands are:
    b, bash_completion    Print the bash completion script.
    burgundy              Stay classy.
    cmd                   Run a command with optional restart and watch for changes to restart.
    hs, httpstat          HTTP trace timings
    replace               Find and replace in text.
    t, target             Execute a configured target.
    template              Render a Go template using environment variables.
    upgrade               Upgrade the ron binary.
    version               Print the version.

http://godoc.org/github.com/upsight/ron

Installation
$ go get -u github.com/upsight/ron/cmd/...

or download from releases

Upgrade
LATEST_URL=https://github.com/upsight/ron/releases/download/v1.1.3/ron-linux-v1.1.3 ron upgrade
Testing
$ ron t go:test
Extending

To extend or customize ron, you can either fork the repo, or vendor it and overwrite the target/default.yaml file and add custom commands.

The vendoring approach may look something like this:

  1. Add vendor/github.com/upsight/ron to your project with whatever package manager you choose (dep, glide, etc.)

  2. Create a new folder for your custom commands ./commands/commandname/commandname.go

package commandname

import (
	"fmt"
	"io"
)

// Command ...
type Command struct {
	Name       string
	W          io.Writer
	WErr       io.Writer
	AppName    string
	AppVersion string
	GitCommit  string
}

// Run ...
func (c *Command) Run(args []string) (int, error) {
	fmt.Fprintln(c.W, c.AppName, c.AppVersion, c.GitCommit)
	return 0, nil
}

// Key returns the commands name for sorting.
func (c *Command) Key() string {
	return c.Name
}

// Aliases are the aliases and name for the command. For instance
// a command can have a long form and short form.
func (c *Command) Aliases() map[string]struct{} {
	return map[string]struct{}{
		"someothername": struct{}{},
	}
}

// Description is what is printed in Usage.
func (c *Command) Description() string {
	return "My command."
}
  1. Create a custom cmd/ron/main.go and add all of your commands.
package main

import (
	"log"
	"os"

	"mygitrepo.com/ron/commands/commandname"

	"github.com/upsight/ron"
	"github.com/upsight/ron/color"
)

func main() {
	c := ron.NewDefaultCommander(os.Stdout, os.Stderr)
	c.Add(&commandname.Command{AppName: ron.AppName, Name: "project", W: os.Stdout, WErr: os.Stderr})
	status, err := ron.Run(c, os.Args[1:])
	if err != nil {
		hostname, _ := os.Hostname()
		log.Println(hostname, color.Red(err.Error()))
	}
	os.Exit(status)
}
  1. Optionally overwrite ./vendor/github.com/upsight/ron/target/default.yaml to have custom targets.

This can be done in a target or manually.

touch default.yaml
cp default.yaml vendor/$RONREPO/target/default.yaml
  1. Create a ./ron.yaml file with instructions on how to put it all together.
{{$path := "export PATH=$GOPATH/bin:$PATH"}}
envs:
  - APP: ron
  - ARCH: amd64
  - PACKAGE_VERSION: +echo `git describe --tags`.`git rev-parse HEAD`
  - REPO: myrepo.com/ron
  - RONREPO: github.com/upsight/ron
  - UNAME: +uname | tr '[:upper:]' '[:lower:]'
  - VERSION: 1.4.0
  - TAG: v${VERSION}
targets:
  prep:
    description: Compile the default yaml asset to a go file.
    cmd: |
      go install ./vendor/github.com/jteeuwen/go-bindata/go-bindata || go get -u github.com/jteeuwen/go-bindata/...
      cp default.yaml vendor/$RONREPO/target/default.yaml
      cd vendor/$RONREPO/target
      go-bindata -o default.go -pkg=target default.yaml
  build:
    description: Compile a binary to ./bin/${UNAME}_${ARCH}
    before:
      - go:prep
    cmd: |
      {{$path}}
      mkdir -p bin/${UNAME}_${ARCH}
      GOARCH=$ARCH GOOS=$UNAME go build -o bin/${UNAME}_${ARCH}/${APP}-${UNAME}-${TAG} -ldflags "-X $REPO/vendor/$RONREPO.GitCommit=$PACKAGE_VERSION -X $REPO/vendor/$RONREPO.AppVersion=$TAG -X $REPO/vendor/$RONREPO.AppName=$APP" cmd/ron/*.go
  1. Run the build
ron t ron:build && ls bin/

Documentation

Overview

Package ron provides a command line interface to common build tasks.

$ ron
Usage: ron <command>

Available commands are:
    b, bash_completion    Print the bash completion script.
    burgundy              Stay classy.
    cmd                   Run a command with optional restart and watch for changes to restart.
    hs, httpstat          HTTP trace timings
    replace               Find and replace in text.
    t, target             Execute a configured target.
    template              Render a Go template using environment variables.
    upgrade               Upgrade the ron binary.
    version               Print the version.

Installation

ron can be used as a standalone binary, or imported as a library.

To install the latest ron use go get

$ go get -u github.com/upsight/ron/cmd/ron

Bash Completion

The bash completion command (b,bash_completion) can be added to bashrc or bash_profile by appending the output or manually by copying it's output. Tab completion is intended for use the target command to get a list of targets.

$ ron bash_completion >> ~/.bashrc
$ source ~/.bashrc

$ ron t
--debug             --list              default:_hello      ron:build           ron:install         ron:run             ron:vet
--default           --verbose           default:burgundy    ron:build_all       ron:lint            ron:test
--envs              --yaml              ron:_vendor_update  ron:cover           ron:prep            ron:testv

$ ron t ron
ron:_vendor_update  ron:build_all       ron:install         ron:prep            ron:test            ron:vet
ron:build           ron:cover           ron:lint            ron:run             ron:testv

$ ron t defa
default:_hello    default:burgundy

Http Stat

The http stat (hs,httpstat) command will give response times broken up by task.

$ ron httpstat http://google.com
Connected to 74.125.199.106:80

HTTP/1.1 200 OK
Server: gws
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Date: Tue, 28 Feb 2017 20:27:43 GMT
Expires: -1
P3p: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
Set-Cookie: NID=98=TKCxQ2pGglNzt6RfHysWo6m-KmiUtHV0UWIuIJa2SLqnLIx2G9mRwuDTntyLvOIZD6bDVVw_jnbHuqUwiiAUqE_xIaiyNpcBtIjpkoCRkuGQu1Pb2Y1rBaLlVBLWrj008GArnUIe5lmshObT; expires=Wed, 30-Aug-2017 20:27:43 GMT; path=/; domain=.google.com; HttpOnly
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block

Body discarded

   DNS Lookup   TCP Connection   Server Processing   Content Transfer
[      14ms  |          19ms  |             56ms  |             0ms  ]
             |                |                   |                  |
    namelookup:14       ms      |                   |                  |
                        connect:33       ms         |                  |
                                      starttransfer:89       ms        |
                                                                 total:90       ms

Cmd

The cmd (cmd) command allows for watching file changes and restarting or executing commands.

$ ron cmd
Usage: ron cmd --wait --watch <path> --restart <command>
	-ignore string
			a comma seperated list of regex patterns to ignore *optional (default ".*\\.git.*,.*\\.DS_Store$,.*\\.pyc$")
	-restart
			Restart the command if it dies.
	-wait
			With watch wait for file changes before running the command.
	-watch string
			Path to directory or file to watch.

$ ron cmd --restart ls
2015/08/03 17:02:38 runit.go:69: running ls
2015/08/03 17:02:38 runit.go:104: running ls
Dockerfile	README.md	_vendor		bin		config		make.sh		src
2015/08/03 17:02:38 cmd.go:40: captured child exited continue...
2015/08/03 17:02:39 runit.go:69: running ls
2015/08/03 17:02:39 runit.go:104: running ls
Dockerfile	README.md	_vendor		bin		config		make.sh		src
2015/08/03 17:02:39 cmd.go:40: captured child exited continue...
^C2015/08/03 17:02:40 cmd.go:37: captured interrupt

$ ron cmd --wait --watch . ls
2015/08/03 17:03:06 runit.go:104: running ls
Dockerfile	README.md	_vendor		bin		config		make.sh		src
2015/08/03 17:03:06 cmd.go:40: captured child exited continue...
2015/08/03 17:03:11 watch.go:28: event:  "foo": CREATE
2015/08/03 17:03:11 watch.go:47: Detected new file foo
2015/08/03 17:03:11 runit.go:87: restart event
2015/08/03 17:03:11 runit.go:141: restarting
2015/08/03 17:03:11 runit.go:123: killing subprocess
2015/08/03 17:03:11 runit.go:104: running ls
Dockerfile	README.md	_vendor		bin		config		foo		make.sh		src
2015/08/03 17:03:11 cmd.go:40: captured child exited continue...
2015/08/03 17:03:11 watch.go:28: event:  "foo": CHMOD
2015/08/03 17:03:11 watch.go:28: event:  "foo": CHMOD

Replace

The replace (replace) command will replace text in a file or directories. If a file is input only that file will replace text, if a directory is given it will recurse all files and do the replace.

$ ron replace
Usage: ron replace [-debug] [path] [replace] [replacewith]
  -debug
    	Debug the replace run

Target

The target (t,target) command allows for specifying bash scripts within yaml files for execution. Executed targets can be specified by giving the filename without extension, and the target name seperated by colon.

$ ron target
Usage: ron target <target> <target>
  -debug
    	Debug the target command being run
  -default string
    	Path to the default yaml config file, local or http.
  -envs
    	List the initialized environment variables.
  -l	List the available targets.
  -list
    	List the available targets.
  -v	Be verbose.
  -verbose
    	Be verbose.
  -yaml string
    	Path to override yaml file, can be local or http.

	ron contains a default set of envs and targets that can be inspected with the
	flag options listed above. Those can also be overidden with another yaml file.
	If no -default or -yaml is provided and in the current or parent working directory there
	exists a ron.yaml, then those will be used as the -yaml option.

	The yaml config should contain "remotes" (optional), "envs", and a hash of "targets".

	remotes should be defined as a map with any environment name and a list of server values. It's only
	necessary to define them once so they could be globally set for example in ~/.ron/remotes.yaml
	You can then reference it with -remote=remotes:some_other_env

		remotes:
			staging:
				-
					host: example1.com
					port: 22
					user: test
				-
					host: example2.com
					port: 22
					user: test
			some_other_env:
				-
					host: exampleprod.com
					port: 22
					user: test
					proxy_host: bastionserver.com
					proxy_port: 22
					proxy_user: bastion_user
					identity_file: /optional/path/to/identityfile

	If no identity file is provided, the users local ssh agent will be attempted. You can add
	keys with ssh-add.

	env values prefixed with a +(subject to change) will be executed and set to the os environment
	prior to target execution.

		envs:
			- APP: ron
			- UNAME: +uname | tr '[:upper:]' '[:lower:]'

	targets can contain a before/after hash which is a list of other targets to
	execute. Each target should contain a cmd which can contain any valid bash
	scripting and can use previously defined envs

		targets:
			prep:
				cmd: |
					echo prep
			install:
				before:
					- prep
				after:
					- prep
				cmd: |
					echo $APP

In order to execute a target you can either run it with the yaml file prefix without extension or if you leave that off it will find the first available target, with the default targets executing last.

$ ron target default:prep
$ ron target ron:prep
$ ron target prep

ron will attempt to find any additional files in the following order:

  1. A -yaml flag filepath config file
  2. A local ./ron.yaml or in a parent directory
  3. Any files in the folder ./.ron/*.yaml
  4. Any ~/.ron/*yaml files
  5. A -default configuration or any binary built in targets

Template

The template (template) will render a Go template file to the given output file.

$ ron template
Usage: ron template
	-input string
			Path or URL to template file.
	-output string
			Path to output file. Defaults to stdout.

Upgrade

Ron can be upgraded if you already have it installed. The easiest way is to just provide a LATEST_URL to the upgrade command:

$ LATEST_URL=https://github.com/upsight/ron/releases/download/v1.1.3/ron-linux-v1.1.3 ron upgrade

Version

To print the current tag and git commit run:

$ ron version
ron 0.0.1 53a7de4612c36b4cf36a9059b5dfa66fbc2639f9

Index

Constants

This section is empty.

Variables

View Source
var (
	// AppName is the applications name, classy
	AppName = "ron"
	// AppVersion the application version.
	AppVersion = "0.0.1"
	// GitCommit is the git hash for the current commit.
	GitCommit = ""
)

Functions

func Run

func Run(c *Commander, args []string) (int, error)

Run builds its internal commands and executes a matching command by parsing args and flags.

Types

type Command

type Command interface {
	Description() string
	Key() string
	Aliases() map[string]struct{}
	Run([]string) (int, error)
}

Command defines the methods a command should implement.

type Commander

type Commander struct {
	Stderr   io.Writer
	Stdout   io.Writer
	Commands []Command
}

Commander is a mapping of name to command.

func NewDefaultCommander added in v1.0.1

func NewDefaultCommander(stdOut io.Writer, stdErr io.Writer) *Commander

NewDefaultCommander creates a new map of command objects, where the keys are the names of the commands.

func (*Commander) Add added in v1.0.1

func (c *Commander) Add(cmd Command)

Add will insert a new Command and then sort it by the commands Name field.

func (Commander) Len added in v1.0.1

func (c Commander) Len() int

func (Commander) Less added in v1.0.1

func (c Commander) Less(i, j int) bool

func (*Commander) List added in v1.1.0

func (c *Commander) List(writer io.Writer)

List will space seperate the list of possible commands.

func (Commander) Swap added in v1.0.1

func (c Commander) Swap(i, j int)

func (*Commander) Usage

func (c *Commander) Usage(writer io.Writer)

Usage displays the available commands.

Directories

Path Synopsis
cmd
ron
commands
cmd
httpstat
Package httpstat provides http trace timings for requests.
Package httpstat provides http trace timings for requests.
Package execute tries it's best to run complex, sometimes multiline bash commands.
Package execute tries it's best to run complex, sometimes multiline bash commands.
Package target implements gnu make like target processing from yaml files and OS environment variables.
Package target implements gnu make like target processing from yaml files and OS environment variables.
Package template renders templates to files.
Package template renders templates to files.

Jump to

Keyboard shortcuts

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