Documentation
¶
Overview ¶
Package util provides utility functions for the application.
Index ¶
- Constants
- Variables
- func AcquireProcessIDLock() interface{ ... }
- func Chdir(path string) interface{ ... }
- func Console() *consoleTerminal
- func GetExecutablePath() string
- func GetHostnameFromPath(path string) string
- func GetIdealConcurrency() uint
- func Getenv(key envVariable) string
- func GetenvBool(key envVariable) bool
- func IntToSizeBytes(s int, unit int64, precision int) string
- func IsGlobMatch(pattern string) bool
- func IsRegex(pattern string) bool
- func ListFilesByExtension(ext string, depth int) []string
- func NewColoredYAMLEncoder(w io.Writer, opts ...yaml.EncodeOption) *coloredYAMLEncoder
- func PathExists(path string) bool
- func PathSanitize(paths ...*string)
- func PreventInterrupt() interface{ ... }
- func PrintlnAndExit(format string, a ...any)
- func StripPathPrefix(path string, keepParents uint) string
- func UncolorizeReader(r io.Reader) (io.Reader, error)
- type PatternList
- type Progressbar
- type ProgressbarOption
- type TablePrinter
- func (t *TablePrinter) AddRowField(field string, colors ...color.Attribute) *TablePrinter
- func (t *TablePrinter) Align() *TablePrinter
- func (t *TablePrinter) EndRow() *TablePrinter
- func (t *TablePrinter) Print()
- func (t *TablePrinter) SetHeader(header ...string) *TablePrinter
- func (t *TablePrinter) SetOutputToStdErr(isStdErr bool) *TablePrinter
- func (t *TablePrinter) Sort() *TablePrinter
- func (t *TablePrinter) Sprint() string
Constants ¶
const ( KB = 1 << (10 * (iota + 1)) MB GB TB PB EB ZB YB )
const CCT = "m" // Color Code Terminator
const CSI = ESC + "[" // Control Sequence Introducer
const ESC = "\x1b" // Begin ANSI sequence
const EnvPrefix = "GITHUB_REPO_"
Prefix for relevant environment variables.
const MUP = CSI + "1A" // Move cursor 1 line up
const ProcessAlreadyRunning = "gr is already running (pid: %d). Either kill the process or wait for it to terminate."
Message when process with given ID is already running.
const Verbose envVariable = "VERBOSE"
Verbose env variable.
Variables ¶
var ( ClearOnFinish = progressbar.OptionClearOnFinish EnableColorCodes = progressbar.OptionEnableColorCodes FullWidth = progressbar.OptionFullWidth OnCompletion = progressbar.OptionOnCompletion SetDescription = progressbar.OptionSetDescription SetElapsedTime = progressbar.OptionSetElapsedTime SetItsString = progressbar.OptionSetItsString SetPredictTime = progressbar.OptionSetPredictTime SetRenderBlankState = progressbar.OptionSetRenderBlankState SetTheme = progressbar.OptionSetTheme SetVisibility = progressbar.OptionSetVisibility SetWidth = progressbar.OptionSetWidth SetWriter = progressbar.OptionSetWriter ShowBytes = progressbar.OptionShowBytes ShowCount = progressbar.OptionShowCount ShowDescriptionAtLineEnd = progressbar.OptionShowDescriptionAtLineEnd ShowElapsedTimeOnFinish = progressbar.OptionShowElapsedTimeOnFinish ShowIts = progressbar.OptionShowIts SpinnerCustom = progressbar.OptionSpinnerCustom SpinnerType = progressbar.OptionSpinnerType Throttle = progressbar.OptionThrottle UseANSICodes = progressbar.OptionUseANSICodes )
var Logger = func() *logrus.Logger { l := logrus.New() l.SetLevel(logrus.WarnLevel) c := Console() l.SetOutput(c.Stdout()) l.SetFormatter(&logrus.JSONFormatter{ PrettyPrint: true, }) supererrors.RegisterCallback(func(err error) { c := Console() l.SetOutput(c.Stderr()) if l.Level >= logrus.DebugLevel { err = tracerr.Wrap(err) var frames []string for _, frame := range err.(tracerr.Error).StackTrace() { switch ctx := frame.String(); { case strings.Contains(ctx, "supererrors.Except"), strings.Contains(ctx, "runtime.main()"), strings.Contains(ctx, "runtime.goexit()"): continue default: frames = append(frames, frame.String()) } } l.WithField("stack", frames).Fatalln(err) } l.SetFormatter(&logrus.TextFormatter{ DisableTimestamp: true, DisableLevelTruncation: true, }) l.Fatalln(err) }) return l }()
App logger (default format JSON).
Functions ¶
func AcquireProcessIDLock ¶
func AcquireProcessIDLock() interface{ Unlock() }
Record process ID and store it into the PID file. If process is already running, return corresponding message and exit.
func GetExecutablePath ¶
func GetExecutablePath() string
Get path of the executable that started current gr process.
func GetIdealConcurrency ¶
func GetIdealConcurrency() uint
Get ideal number of workers for current CPU.
func GetenvBool ¶
func GetenvBool(key envVariable) bool
Retrieve environment variable of boolean type.
func IntToSizeBytes ¶
Format integer as byte size in kMGTPE[B] unit.
func IsGlobMatch ¶ added in v2.7.1
IsGlobMatch checks if the pattern is a glob pattern.
func ListFilesByExtension ¶
List files in current working directory matching given extension (extension has to start with a dot).
func NewColoredYAMLEncoder ¶
func NewColoredYAMLEncoder(w io.Writer, opts ...yaml.EncodeOption) *coloredYAMLEncoder
NewColoredYAMLEncoder creates new colorful YAML encoder for given writer.
func PathSanitize ¶
func PathSanitize(paths ...*string)
Format path to UNIX style path, strip volume name if default.
func StripPathPrefix ¶
StripPathPrefix strips prefix from path given a number of parent directories to keep.
Types ¶
type PatternList ¶
type PatternList []string
PatternList is a list of regular expressions.
func (PatternList) GlobMatch ¶
func (l PatternList) GlobMatch(target string) bool
GlobMatch checks if target matches any of the globs in the list.
func (PatternList) GlobMatchAny ¶ added in v2.7.1
func (l PatternList) GlobMatchAny(targets ...string) bool
GlobMatchAny checks if any of the targets match any of the globs in the list.
func (PatternList) RegexMatch ¶
func (l PatternList) RegexMatch(target string, timeout time.Duration) bool
RegexMatch checks if target matches any of the regular expressions in the list.
func (PatternList) RegexMatchAny ¶ added in v2.7.1
func (l PatternList) RegexMatchAny(timeout time.Duration, targets ...string) bool
RegexMatchAny checks if any of the targets match any of the regular expressions in the list.
type Progressbar ¶
type Progressbar struct { *progressbar.ProgressBar // contains filtered or unexported fields }
Custom implementation of progressbar.
func NewProgressbar ¶
func NewProgressbar(m int, options ...ProgressbarOption) *Progressbar
Create new progressbar with default settings.
func (*Progressbar) Add ¶
func (p *Progressbar) Add(i int) *Progressbar
Increase progressbar counter by i.
func (*Progressbar) ChangeMax ¶
func (p *Progressbar) ChangeMax(max int) *Progressbar
Change max value of progressbar.
func (*Progressbar) Describe ¶
func (p *Progressbar) Describe(format string, a ...any) *Progressbar
Set description.
type TablePrinter ¶
type TablePrinter struct {
// contains filtered or unexported fields
}
Table printer prints text in padded, tabular form.
func (*TablePrinter) AddRowField ¶
func (t *TablePrinter) AddRowField(field string, colors ...color.Attribute) *TablePrinter
Add record.
func (*TablePrinter) Align ¶
func (t *TablePrinter) Align() *TablePrinter
Align records to match the maximum column width and prevent index errors.
func (*TablePrinter) SetHeader ¶ added in v2.7.1
func (t *TablePrinter) SetHeader(header ...string) *TablePrinter
Set header.
func (*TablePrinter) SetOutputToStdErr ¶
func (t *TablePrinter) SetOutputToStdErr(isStdErr bool) *TablePrinter
Switch between Stdout and Stderr.
func (*TablePrinter) Sprint ¶
func (t *TablePrinter) Sprint() string
Print through a buffer to deliver rendered string.