gochips

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: MIT Imports: 9 Imported by: 8

README

gochips

Small useful pieces of golang code

import (
  gc "github.com/untillpro/gochips"
) 

Chips for Tests

func Test_Get(t *testing.T) {
  err := MyFunc()
  gc.FatalIfError(t, err, "Error in MyFunc()")
}

Chips for Logging


  gc.Error(args ...interface{})
  gc.Info(args ...interface{})
  
  // Adds "..." suffux to  argument
  gc.Doing("Getting things done")

  // Does nothing until you set gc.IsVerbose = true
  gc.Verbose(args ...interface{})

}

Prefixes:

  • Verbose() output is prefixed with gc.VerbosePrefix = "--- "
  • Error() output is prefixed with gc.ErrorPrefix = "*** "

Change default behavior:

  • var Output func(funcName, s string)
    • This function is called by default implementation of Error, Info etc
  • It is possible to redefine all implementations, ref:
func init() {
	Doing = implDoing
	Info = implInfo
	Verbose = implVerbose
	Error = implError
	Output = implOutput
	VerboseWriters = implVerboseWriters
}

Chips for Exeс

Piped execution a-la shell

echo README.md | grep README.md | sed s/READ/forgive/:

	err := new(PipedExec).
		Command("echo", "README.md").WorkingDir("/").
		Command("grep", "README.md").
		Command("echo", "good").
		Run(os.Stdout, os.Stdout)

Avoid stdout until gc.IsVerbose is set to true:

	err := new(PipedExec).
		Command("echo", "README.md").WorkingDir("/").
		Command("grep", "README.md").
		Command("echo", "good").
		Run(gc.VerboseWriters())

Ref. also PipedExec_test.go

Documentation

Index

Constants

View Source
const (
	StderrRedirectNone = iota
	StderrRedirectStdout
	StderrRedirectNull
)

Stderr redirection

Variables

View Source
var Doing func(arg interface{})

Doing printlns "$obj..." to stdout

View Source
var Error func(args ...interface{})

Error printlns to stdout

View Source
var ErrorPrefix = "*** "

ErrorPrefix prefixes Verbose output

View Source
var Info func(args ...interface{})

Info printlns to stdout

View Source
var IsVerbose = false

IsVerbose enables Verbose

View Source
var Output func(funcName, s string)

Output is used by all functions

View Source
var Verbose func(subj string, args ...interface{})

Verbose If IsVerbose is true printlns VerbosePrefix + subj + ": " + ...args args are printed in %#+v format (json-like)

View Source
var VerbosePrefix = "--- "

VerbosePrefix prefixes Verbose output

View Source
var VerboseWriters func() (out io.Writer, err io.Writer)

VerboseWriters returns (os.Stdout, os.Stderr) if IsVerbose, (ioutil.Discard, os.Stderr) otherwise

Functions

func ExitIfError

func ExitIfError(err error, args ...interface{})

ExitIfError s.e.

func ExitIfFalse

func ExitIfFalse(cond bool, args ...interface{})

ExitIfFalse s.e.

func Fatal added in v1.6.0

func Fatal(args ...interface{})

Fatal exists with given parameters

func FatalIfError added in v1.9.0

func FatalIfError(t *testing.T, err error, args ...interface{})

FatalIfError calls t.Fatal(args, err) if err is not nil

func PanicIfError

func PanicIfError(err error)

PanicIfError panics if err is not null

Types

type PipedExec

type PipedExec struct {
	// contains filtered or unexported fields
}

PipedExec allows to execute commands in pipe

func (*PipedExec) Command

func (Self *PipedExec) Command(name string, args ...string) *PipedExec

Command adds a command to a pipe

func (*PipedExec) GetCmd added in v1.8.0

func (Self *PipedExec) GetCmd(idx int) *exec.Cmd

GetCmd returns cmd with given index

func (*PipedExec) Run

func (Self *PipedExec) Run(out io.Writer, err io.Writer) error

Run starts the pipe

func (*PipedExec) RunToStrings added in v1.8.0

func (Self *PipedExec) RunToStrings() (stdout string, stderr string, err error)

RunToStrings runs the pipe and saves outputs to strings

func (*PipedExec) Start added in v1.8.0

func (Self *PipedExec) Start(out io.Writer, err io.Writer) error

Start all cmds

func (*PipedExec) Wait added in v1.8.0

func (Self *PipedExec) Wait() error

Wait until all cmds finish

func (*PipedExec) WorkingDir

func (Self *PipedExec) WorkingDir(wd string) *PipedExec

WorkingDir sets working directory for the last command

Jump to

Keyboard shortcuts

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