util

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

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

Go to latest
Published: Jan 28, 2020 License: MIT Imports: 13 Imported by: 2

README

util

An assortment of utility functions that don't fit into any particular package and may be needed by more than one package.

These are not considered part of the gotables interface surface and may change at any time.

They are made public to facilitate their use across all urban-wombat repositories.

See util godoc

Documentation

Index

Examples

Constants

View Source
const (
	FlagRequired = true
	FlagOptional = false
)

Utility function to test string flags.

It avoids boilerplate code testing flags.

It can be called and:-

(1) Required flags can trust the existence of an argument.

// Required flag.
exists, err := util.CheckStringFlag("r", flags.r, util.FlagRequired)
if !exists {
	fmt.Fprintf(os.Stderr, "%v\n", err)
	os.Exit(1)
}

(2) Optional flags can test exists.

// Optional flag.
exists, err := util.CheckStringFlag("o", flags.o, util.FlagOptional)
if exists {
	// Do something with flag.
}

Variables

This section is empty.

Functions

func BuildDateTime

func BuildDateTime() (buildDateTime string)

Return a string with the build date/time and (seconds-ago) of the executable and where it is installed.

func CanReadFromPipe

func CanReadFromPipe() (bool, error)

Check to see if this program can read piped input on this machine.

func CheckStringFlag

func CheckStringFlag(name string, arg string, required bool) (exists bool, err error)

func FilepathAbs

func FilepathAbs(inputPath string) (path string, err error)

Handle Cygwin environment.

The problem:

cygwinPath := "/cygdrive/c/mypath/myfile"
windowsPath := filepath.Abs(cygwinPath)

returns: "C:/cygdrive/c/mypath/myfile"

It should return: "C:/mypath/myfile"

func FormatSource

func FormatSource(source string) (formattedSource string, err error)

func FuncCaller

func FuncCaller() string

func FuncName

func FuncName() string

Short function name with parentheses.

pkgName.funcName

becomes:

funcName()
Example
// Called from inside func ExampleFuncName()
fmt.Println(FuncName())
Output:

ExampleFuncName()

func FuncNameFull

func FuncNameFull() string

Full path of function source code with line number followed by full package name of function. Like this:

<mydrive>/golang/src/github.com/urban-wombat/util/util_test.go[39] github.com/urban-wombat/util.TestFuncNameFull

func FuncNameNoParens

func FuncNameNoParens() string

Short function name with NO parentheses.

pkgName.funcName

becomes:

funcName
Example
// Called from inside func ExampleFuncNameNoParens()
fmt.Println(FuncNameNoParens())
Output:

ExampleFuncNameNoParens

func FuncSource

func FuncSource() string

Short source file name and line number. Like this:

util_test.go[39]

func GoFmtProgramString

func GoFmtProgramString(goProgramString string) (formattedGoProgramString string, err error)

NOTE: As per egonelbre suggestion (see https://www.reddit.com/r/golang/comments/9umtp2/beta_release_of_flattables_go_flatbuffers/e95iffn/?context=3) gotables and flattables now calls go/format/Source() to format code. github.com/urban-wombat/util/GoFmtProgramString() will be DEPRECATED at some point.

Instead use go.format.Source()

util.FormatSource()

GoFmtProgramString() pipes a Go program file (as a string) through the Go tool gofmt and returns its output.

Use it to tidy up generated Go source code before writing it to file.

On error the input string is returned unchanged, not an empty "" string. This is unusual but we do that here to avoid crunching goProgramString in the calling function if it happens to be called like this:

goProgramString, err = GoFmtProgramString(goProgramString)
if err != nil {
	// goProgramString is unchanged, not crunched
}
// goProgramString has been formatted by gofmt

Because this function calls out to gofmt in the operating system, the potential for failure is possible on some machines (and hence not testable during development). Hence a more forgiving return of its input string so as to avoid crunching user data.

func GulpFromPipe

func GulpFromPipe() (string, error)

Read and return piped input as a string.

Beware: this blocks waiting for stdin.

stdin, err := util.GulpFromPipe()

func GulpFromPipeWithTimeout

func GulpFromPipeWithTimeout(timeout time.Duration) (input string, err error)

Read and return piped input as a string.

This waits for stdin but only until timeout expires.

stdin, err := util.GulpFromPipe(1 * time.Second)

func IsCommandInstalled

func IsCommandInstalled(commandName string) (bool, error)

Check whether commandName is installed on this machine.

func PrintCaller

func PrintCaller()

See 1: https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang

See 2: http://moazzam-khan.com/blog/golang-get-the-function-callers-name

This is a blend of both (above URLs) examples. Provides:-

(1) The function name called.

(2) The function name of the caller.

(2) The file name[line number] at the call.

This is intentionally a print-only function because calling it from another function (other than the one being tracked) will change the calling information by nesting to an additional level.

func ProgName

func ProgName() string

func Round

func Round(val float64, places int) (rounded float64)

Round is a custom implementation for rounding values.

Round up if fraction is >= 0.5 otherwise round down.

From: https://medium.com/@edoardo849/mercato-how-to-build-an-effective-web-scraper-with-golang-e2918d279f49#.istjzw4nl

func WordSize

func WordSize() int

The word size (in bits) of the machine we're now running on. Typically 64 or 32 bits.

Or use: intBits := strconv.IntSize

Types

type StringFlag

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

StringFlag implements the flag.Value interface https://golang.org/pkg/flag/#Value

type Value interface {
	String() string
	Set(string) error
}

func (*StringFlag) AllOk

func (sf *StringFlag) AllOk() bool

AllOk() is specific to gotables.Util It means:-

(1) flag exists
(2) flag is set
(3) error is nil

func (*StringFlag) Error

func (sf *StringFlag) Error() error

Error() is specific to gotables.Util

func (*StringFlag) Exists

func (sf *StringFlag) Exists() bool

Exists() is specific to gotables.Util

func (*StringFlag) IsSet

func (sf *StringFlag) IsSet() bool

IsSet() is specific to gotables.Util

func (*StringFlag) Print

func (sf *StringFlag) Print()

Print to stdout StringFlag field values and method results.

func (*StringFlag) Set

func (sf *StringFlag) Set(s string) error

Set() implements part of the flag.Value interface https://golang.org/pkg/flag/#Value

func (*StringFlag) String

func (sf *StringFlag) String() string

String() implements part of the flag.Value interface https://golang.org/pkg/flag/#Value

Jump to

Keyboard shortcuts

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