runnr

package module
v0.0.0-...-843f4db Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2021 License: MIT Imports: 2 Imported by: 0

README

Simple task runner

runnr is simple task runner for go (similar to packages like rake). It's a small wrapper around a cobra cli application, to create more flexible application build commands.

Getting started

go get github.com/mdev5000/runnr
go get github.com/mdev5000/runnr/runnr

# Create a new runnr project.
runnr g new 

# Run the example.
runnr hello

By default runnr creates a local exe at internal/cmd/runnr_local/main.go. You can move and configure this in runnr.yml.

Registering commands

Different module commands can be registered. You can also limit which module commands get registered.

Also you can register a cobra.Command directly.

runnr/main.go

package main

import (
	"github.com/mdev5000/runnr"
	"github.com/spf13/cobra"
	"somecommands"
	"somecommands2"
	"somecommands3"
	"context"
)

func main() {
	runner := runnr.NewRunner()

	runner.Register(somecommands.Commands)
	runner.Register(somecommands2.Commands).Only("first", "second")
	runner.Register(somecommands2.Commands).Exclude("third", "fourth")

	runner.AddCommand(&cobra.Command{
		Use: "hello",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("hello world")
		},
	})

	ctx := context.Background()
	if err := runner.Run(ctx); err != nil {
		panic(err)
	}
}

Given the example above you could run the hello command like you would any cobra subcommand.

runnr hello

You can recompile at anytime by appending -r after the command.

runnr hello -r

For more examples of modules and packages registering commands see the example/ folder.


Running static commands

You can run a static project using the static command

alias myapp="runnr g s run ~/path/to/main.go ~/path/to/myapp --"

Then you can run myapp like a runnr command:

# Recompile your application.
myapp -r

# Show the help for your application (if it exists).
myapp -h

Documentation

Overview

A simple task runner for go (similar to packages like rake). It's a small wrapper around a `cobra` cli application, to create more flexible application build commands.

See the github page, https://github.com/mdev5000/runnr, for more details.

Example (RegisterThirdPartyCommands)
package main

import (
	"context"
	"github.com/mdev5000/runnr"
	"github.com/spf13/cobra"
)

func GetCommands() []*cobra.Command {
	first := &cobra.Command{
		Use: "first",
	}
	second := &cobra.Command{
		Use: "second",
	}
	return []*cobra.Command{first, second}
}

func main() {
	runner := runnr.NewRunner()

	// Register the third party commands under a root task called tp and exclude the "second" command.
	//
	// For example you would run the "first" command as follows:
	//   runnr tp first
	//
	runner.Register(GetCommands).UnderParent("tp").Exclude("second")

	ctx := context.Background()
	if err := runner.Run(ctx); err != nil {
		panic(err)
	}
}
Example (SettingsUpApplication)
package main

import (
	"context"
	"fmt"
	"github.com/mdev5000/runnr"
	"github.com/spf13/cobra"
)

func main() {
	runner := runnr.NewRunner()

	runner.AddCommand(&cobra.Command{
		Use: "hello",
		RunE: func(cmd *cobra.Command, args []string) error {
			fmt.Println("hello world")
			return nil
		},
	})

	ctx := context.Background()
	if err := runner.Run(ctx); err != nil {
		panic(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandRegisterer

type CommandRegisterer = func() []*cobra.Command

Interface implemented by third parties to register tasks (tasks are basically just cobra comands).

type Registration

type Registration interface {
	// Register all the commands under a parent task.
	UnderParent(parentCmdName string) Registration

	// Include only commands with the given names.
	Only(commandsNames ...string) Registration

	// Exclude command with the given names.
	Exclude(commandsNames ...string) Registration
}

Controls the registration of a group of tasks. @todo add example for UnderParent

type Runner

type Runner struct {
	InstanceName string
	// contains filtered or unexported fields
}

func NewRunner

func NewRunner() *Runner

Create a new runnr application.

func (*Runner) AddCommand

func (r *Runner) AddCommand(cmd *cobra.Command)

func (*Runner) Register

func (r *Runner) Register(registration CommandRegisterer) Registration

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

func (*Runner) ToCommand

func (r *Runner) ToCommand() *cobra.Command

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

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