cmd

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cmd bootstraps the application.

Main() reads the provided arguments from the command line and creates a blush.Blush instance. If there is any error, it will terminate the application with os.Exit(1), otherwise it then uses io.Copy() to write to standard output and exits normally.

GetBlush() returns an error if no arguments are provided or it can't find all the passed files. Files should be last arguments, otherwise they are counted as matching strings. If there is no file passed, the input should come in from Stdin as a pipe.

hasArgs(args ...string) function looks for args in input and if it finds it, it removes it and put the rest in the remaining slice.

Notes

We are not using the usual flag package because it cannot handle variables in the args and continues grouping of passed arguments.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoInput is returned when the application doesn't receive any files as the
	// last arguments or a stream of inputs from shell's pipe.
	ErrNoInput = errors.New("no input provided")

	// ErrNoFilesFound is returned when the files pattern passed to the application
	// doesn't match any existing files.
	ErrNoFilesFound = errors.New("no files found")
)
View Source
var (
	Help = "Usage: blush [OPTION]... PATTERN [FILE]...\nTry 'blush --help' for more information."
	// nolint:misspell // it's ok.
	Usage = `` /* 1076-byte string literal not displayed */

)

These variables are used for showing help messages on command line.

Functions

func GetBlush

func GetBlush(input []string) (*blush.Blush, error)

GetBlush returns an error if no arguments are provided or it can't find all the passed files in the input.

Note

The first argument will be dropped as it will be the application's name.

Example (MultiColour)
package main

import (
	"fmt"

	"github.com/arsham/blush/blush"
	"github.com/arsham/blush/cmd"
)

type colourer interface {
	Colour() blush.Colour
}

func main() {
	input := []string{"-b", "term1", "-g", "term2", "/"}
	b, err := cmd.GetBlush(input)
	c1 := b.Finders[0].(colourer)
	c2 := b.Finders[1].(colourer)
	fmt.Println("err == nil:", err == nil)
	fmt.Println("Finders count:", len(b.Finders))
	fmt.Println("Is blue:", c1.Colour() == blush.Blue)
	fmt.Println("Is green:", c2.Colour() == blush.Green)

}
Output:

err == nil: true
Finders count: 2
Is blue: true
Is green: true
Example (Red)
package main

import (
	"fmt"

	"github.com/arsham/blush/blush"
	"github.com/arsham/blush/cmd"
)

type colourer interface {
	Colour() blush.Colour
}

func main() {
	input := []string{"blush", "--red", "term", "/"}
	b, err := cmd.GetBlush(input)
	fmt.Println("err == nil:", err == nil)
	fmt.Println("Finders count:", len(b.Finders))
	c := b.Finders[0].(colourer)
	fmt.Println("Is red:", c.Colour() == blush.Red)

}
Output:

err == nil: true
Finders count: 1
Is red: true

func Main

func Main()

Main reads the provided arguments from the command line and creates a blush.Blush instance.

func WaitForSignal

func WaitForSignal(sig chan os.Signal, exit func(int))

WaitForSignal calls exit with code 130 if receives an SIGINT or SIGTERM, 0 if SIGPIPE, and 1 otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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