utils

package
v0.49.3 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GitCommand = func(args ...string) (*exec.Cmd, error) {
	gitExe, err := safeexec.LookPath("git")
	if err != nil {
		programName := "git"
		if runtime.GOOS == "windows" {
			programName = "Git for Windows"
		}
		return nil, fmt.Errorf("unable to find git executable in PATH; please install %s before retrying", programName)
	}
	return exec.Command(gitExe, args...), nil
}
View Source
var IsTerminal = func(f *os.File) bool {
	return isatty.IsTerminal(f.Fd()) || IsCygwinTerminal(f)
}
View Source
var TerminalSize = func(w interface{}) (int, int, error) {
	if f, isFile := w.(*os.File); isFile {
		return term.GetSize(int(f.Fd()))
	}

	return 0, 0, fmt.Errorf("%v is not a file", w)
}

Functions

func AvgFloat64 added in v0.21.0

func AvgFloat64(array []float64) float64

SumFloat64 returns the average of all float64 values of the slice

func AvgInt added in v0.21.0

func AvgInt(array []int) float64

AvgInt returns the average of all int values of the slice

func ClearScreen added in v0.20.2

func ClearScreen()

func DownloadFile

func DownloadFile(filepath string, url string) error

DownloadFile will download a url to a local file. It's efficient because it will write as it downloads and not load the whole file into memory.

func DownloadURL added in v0.6.0

func DownloadURL(url string) ([]byte, error)

DownloadURL downloads from a given URL and returns the response body

func ExtractOwnerRepoFromGitURL added in v0.4.0

func ExtractOwnerRepoFromGitURL(url string) (owner string, repo string, err error)

ExtractOwnerRepoFromGitURL extracts owner and repo from a Git URL: https or ssh url

func GetShortVersion added in v0.18.0

func GetShortVersion(version string) (string, bool)

func GitDir added in v0.4.0

func GitDir() (string, error)

GitDir returns the directory path of the current repository

func GitRemoteOriginURL added in v0.4.0

func GitRemoteOriginURL() (string, error)

func GitShortRev added in v0.18.0

func GitShortRev() (string, error)

func HTTPTraceRequest added in v0.42.1

func HTTPTraceRequest(method, url string, status *int, r io.Reader) (io.Reader, string, error)

HTTPTraceRequest - returns a beautified trace log for http requests returns an io.Reader containing the original data presented to allow reuse

func HTTPTraceResponse added in v0.42.1

func HTTPTraceResponse(method, url string, status *int, r io.Reader) (io.Reader, string, error)

HTTPTraceRequest - returns a beautified trace log for http responses returns an io.Reader containing the original data presented to allow reuse

func IfThenElse

func IfThenElse(condition bool, a interface{}, b interface{}) interface{}

IfThenElse evaluates a condition, if true returns the first parameter otherwise the second

func IsCygwinTerminal added in v0.4.0

func IsCygwinTerminal(f *os.File) bool

func IsGitRepo added in v0.4.0

func IsGitRepo() bool

IsGitRepo indicates whether the current working directory is a git repository

func IsValidUUID added in v0.20.0

func IsValidUUID(u string) bool

IsValidUUID indicates whether the string as argument contains a valid UUID

func LabelsToSelector added in v0.44.0

func LabelsToSelector(labels map[string]string) (string, error)

Turn a mapping of labels into a base64 encoded json payload suitable for querying resources

func NestedMapLookup

func NestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}, err error)

NestedMapLookup ... m: a map from strings to other maps or values, of arbitrary depth ks: successive keys to reach an internal or leaf node (variadic) If an internal node is reached, will return the internal map

Returns: (Exactly one of these will be nil) rval: the target node (if found) err: an error created by fmt.Errorf

https://gist.github.com/ChristopherThorpe/fd3720efe2ba83c929bf4105719ee967 Licensed under the CC by 4.0 https://creativecommons.org/licenses/by/4.0/

func OpenInBrowser added in v0.3.0

func OpenInBrowser(url string) error

OpenInBrowser opens the url in a web browser based on OS

func RandomInt added in v0.20.0

func RandomInt(min, max int64) int64

RandomInt - returns an int >= min, < max

func RandomString added in v0.3.0

func RandomString(length int) (string, error)

RandomString generates a random string of a given length

func ReadTokenFromStdin added in v0.31.0

func ReadTokenFromStdin(r TokenReader) error

ReadTokenFromStdin - attempts to read token from stdin or any io.ReaderCloser provided. This function will timeout after 1 second(s) if no input is detected from the reader. Note that this short timeout is due to expectation that the token will be presented instantly via stdin

func Reverse added in v0.15.0

func Reverse(s interface{})

Reverse a slice

func StringInArray

func StringInArray(str string, list []string) bool

StringInArray indicates whether a string value is contained in an array or slice

func SumFloat64 added in v0.11.0

func SumFloat64(array []float64) float64

SumFloat64 returns the sum of all float64 values of the slice

func SumInt added in v0.11.0

func SumInt(array []int) int

SumInt returns the sum of all int values of the slice

func ToplevelDir added in v0.4.0

func ToplevelDir() (string, error)

ToplevelDir returns the top-level directory path of the current repository

func TruncateString added in v0.6.0

func TruncateString(s string, num int) string

TruncateString truncates the given string to the num provided and adds an ellipsis (...).

Types

type GitRemoteURL added in v0.4.0

type GitRemoteURL struct {
	Proto string // https , ssh , git
	User  string // optional
	Host  string // host or host:port
	Path  string // full path to the repo (without .git ext)
	// contains filtered or unexported fields
}

func ParseGitRemoteOriginURL added in v0.4.0

func ParseGitRemoteOriginURL(url string) (*GitRemoteURL, error)

ParseGitRemoteOriginURL parses a git remote origin url into a struct that can be uniquely identified regardless the used protocol

func (*GitRemoteURL) Hash added in v0.4.0

func (grURL *GitRemoteURL) Hash() string

Hash computes the hash for a git remote URL only on non changing fields: different protocol or user can still refer to the same remote origin

func (*GitRemoteURL) String added in v0.4.0

func (grURL *GitRemoteURL) String() string

String returns the original raw URL of the git remote

type HttpTraceLog added in v0.42.1

type HttpTraceLog struct {
	StatusCode *int
	Response   *string
	Request    *string
	Method     string
	URL        string
}

HTTPTraceLog used to fill in traceTemplate

func (*HttpTraceLog) String added in v0.42.1

func (h *HttpTraceLog) String() string

type TokenReader added in v0.31.0

type TokenReader interface {
	GetToken() string
	SetToken(string)
	Reader() io.ReadCloser
}

Jump to

Keyboard shortcuts

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