util

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// FilepathFuncMap provides standard functions to build/manipulate path in
	// template.Funcmap's format:
	//  - base            : get the path basename
	//  - ext             : get file extension
	//  - sep             : return a path separator ('\' of '/')
	//  - sanitizePath    : replace special chars that don't usually work well when
	//                      use in path name
	//  - sanitizeFilename: like sanitizePath but also
	//                      removes any path separators.
	//  - nospace         : get rid of spaces
	FilepathFuncMap = template.FuncMap{
		"base":             filepath.Base,
		"ext":              filepath.Ext,
		"sep":              func() string { return string(filepath.Separator) },
		"sanitizePath":     pathSanitizer,
		"sanitizeFilename": filenameSanitizer,
		"nospace":          nospaceSanitizer,
	}

	// StringsFuncMap provides standard functions to manipulate strings in
	// template.Funcmap's format:
	//  - join :  join elements separating them wit the given separator.
	//  - lower:  convert string to lower-case
	//  - title:  convert string to title-case
	//  - upper:  convert string to upper-case
	StringsFuncMap = template.FuncMap{
		"join":  strings.Join,
		"lower": strings.ToLower,
		"title": strings.ToTitle,
		"upper": strings.ToUpper,
	}

	// SerializationFuncMap provides standard functions to serialize/deserialize an
	// interface in template.Funcmap's format:
	//  - toJSON      : converts an interface to JSON representation.
	//  - toPrettyJSON: converts an interface to an easy-to-read JSON representation.
	SerializationFuncMap = template.FuncMap{
		"toJSON": func(v interface{}) (string, error) {
			output, err := json.Marshal(v)
			if err != nil {
				return "", err
			}
			return string(output), nil
		},

		"toPrettyJSON": func(v interface{}) (string, error) {
			output, err := json.MarshalIndent(v, "", "  ")
			if err != nil {
				return "", err
			}
			return string(output), nil
		},
	}
)
View Source
var (
	// ErrUnknownLogStatus is returned when trying to set a logswitcher's
	// status tat is neither 'true' nor 'false'.
	ErrUnknownLogStatus = errors.New("unknown status")
)

Functions

func CopyFile

func CopyFile(dst string, src string) error

CopyFile copies src to dst. Directories hosting dst are created as needed. if dst exists, copy does not happen and an error is returned. copyFile forces write to disk (Sync() method of os.File), and value certainty that write operation happens correctly over performance.

func ExecInTTY

func ExecInTTY(name string, arg ...string) error

ExecInTTY executes a command in a TTY.

func IsEmptyFile added in v0.3.0

func IsEmptyFile(path string) (bool, error)

IsEmptyFile checks whether a file is empty or not.

func SamePath

func SamePath(path1, path2 string) (bool, error)

SamePath checks if two path strings are representing the same path. Limitation: this version is only comparing the path strings and do not consider situations where path string are different but are pointing to the same location on file-system.

func TmplFuncMap

func TmplFuncMap(t *template.Template) template.FuncMap

TmplFuncMap provides standard functions to execute go text templates path in template.Funcmap's format:

  • tmpl : execute a sub-template by name. Sub-templates are chosen from t namespace. Unlike {{template xXx}}, tmpl allows to pipe its result to another command.

Types

type GoTemplate

type GoTemplate struct {
	*template.Template
}

GoTemplate wraps a text/template.Template to implement flag.Value interface and get customization through command-line.

func NewGoTemplate

func NewGoTemplate(tmpl *template.Template) *GoTemplate

NewGoTemplate creates a new GoTemplate.

func (*GoTemplate) Set

func (gotmpl *GoTemplate) Set(tmpl string) error

Set implements flag.Value interface for a GoTemplate.

func (GoTemplate) String

func (gotmpl GoTemplate) String() string

String proposes a human-friendly string representation of a go template.

type GoTemplateFS

type GoTemplateFS struct {
	*template.Template
}

GoTemplateFS wraps a text/template.Template that parses its template definition from files in order to implement flag.Value interface and get customization through command-line.

func NewGoTemplateFS

func NewGoTemplateFS(tmpl *template.Template) *GoTemplateFS

NewGoTemplateFS creates a new GoTemplateFS.

func (*GoTemplateFS) Set

func (gotmplFS *GoTemplateFS) Set(pattern string) error

Set implements flag.Value interface for a GoTemplateFS.

func (GoTemplateFS) String

func (gotmplFS GoTemplateFS) String() string

String proposes a human-friendly string representation of a go template.

type KV

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

KV wraps a map to implement flag.Value interface and get ability to allow user to define (key, value) through command-line.

func NewKV

func NewKV(m map[string]string) *KV

NewKV creates a new Map.

func (*KV) Set

func (kv *KV) Set(s string) error

Set implements flag.Value interface for a KV. Command-line flag format is key=value

func (KV) String

func (kv KV) String() string

String proposes a human-friendly string representation of a collection of (key,value).

type Logswitcher

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

Logswitcher wraps a set of log.Logger to implement flag.Value interface and get activation/de-activation from command-line. When activated, logger will print to os.Stderr.

func NewLogSwitcher

func NewLogSwitcher(loggers ...*log.Logger) *Logswitcher

NewLogSwitcher creates a new Logger that prints nothing (output is io.Discard), waiting for user to trigger it through command-line flags.

func (Logswitcher) IsBoolFlag

func (ls Logswitcher) IsBoolFlag() bool

IsBoolFlag implements flag.Value interface to notify that logswitcher flag is boolean.

func (*Logswitcher) Set

func (ls *Logswitcher) Set(status string) error

Set implements flag.Value interface to set logswitcher's status from command-line. Status could be either true or false.

func (Logswitcher) String

func (ls Logswitcher) String() string

String proposes a human-friendly string representation of a logswitcher.

Jump to

Keyboard shortcuts

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