common

package
v1.24.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: GPL-3.0 Imports: 31 Imported by: 6

Documentation

Overview

Package common collection of small utils

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPorts is a map of default ports for common services
	DefaultPorts = map[string]int{
		"http":           80,
		"https":          443,
		"ftp":            21,
		"ftps":           990,
		"ssh":            22,
		"ldap":           389,
		"ldaps":          636,
		"imap":           143,
		"imaps":          993,
		"smtp":           25,
		"smtps":          465,
		"pop3":           110,
		"pop3s":          995,
		"mysql":          3306,
		"postgresql":     5432,
		"oracle":         1521,
		"mssql":          1433,
		"mongodb":        27017,
		"redis":          6379,
		"memcached":      11211,
		"couchdb":        5984,
		"couchbase":      8091,
		"cassandra":      9042,
		"elasticsearch":  9200,
		"kibana":         5601,
		"grafana":        3000,
		"prometheus":     9090,
		"alertmanager":   9093,
		"consul":         8500,
		"vault":          8200,
		"nomad":          4646,
		"etcd":           2379,
		"zookeeper":      2181,
		"kafka":          9092,
		"rabbitmq":       5672,
		"nats":           4222,
		"nats-streaming": 4223,
		"mqtt":           1883,
		"coap":           5683,
		"dns":            53,
		"dhcp":           67,
		"tftp":           69,
		"ntp":            123,
		"snmp":           161,
		"syslog":         514,
		"radius":         1812,
		"radius-acct":    1813,
	}
)
View Source
var InputReader = os.Stdin

Functions

func CanRead added in v1.9.5

func CanRead(filename string) bool

CanRead checks if a file can be read(opened

func ChdirToFile

func ChdirToFile(file string) error

ChdirToFile change working directory to the filename

func CheckSkip

func CheckSkip(line string) (skip bool)

CheckSkip checks if a line can be skipped

func CheckType added in v1.9.3

func CheckType(t any, expected string) (ok bool, haveType string)

CheckType checks if an interface is of a certain type and returns if it matches expected or is Nil

func CmdFlagChanged added in v1.9.2

func CmdFlagChanged(cmd *cobra.Command, name string) (changed bool)

CmdFlagChanged checks if the named commandline flag has been supplied and changed

func CmdRun added in v1.9.2

func CmdRun(cmd *cobra.Command, args []string) (out string, err error)

CmdRun starts a command and returns captured output

func CommandExists added in v1.13.0

func CommandExists(command string) bool

CommandExists checks if a command exists in PATH

func DestroyDockerContainer added in v1.9.0

func DestroyDockerContainer(container *dockertest.Resource)

DestroyDockerContainer destroys a docker container

func ExecDockerCmd added in v1.9.0

func ExecDockerCmd(container *dockertest.Resource, cmd []string) (out string, code int, err error)

ExecDockerCmd executes an OS cmd within container and print output

func ExecuteOsCommand added in v1.4.0

func ExecuteOsCommand(cmdArgs []string, stdIn io.Reader) (stdOut string, stdErr string, err error)

ExecuteOsCommand runs an OS command and returns output

func FileExists added in v1.9.5

func FileExists(filename string) bool

FileExists checks if a file or directory exists

func FindCommand added in v1.19.0

func FindCommand(name string) string

FindCommand searches for a command in the PATH environment variable

func FindFileInPath added in v1.17.0

func FindFileInPath(filename string, dirs []string) string

FindFileInPath searches for a file with the given name in the provided directories. It returns the absolute path of the found file, or an empty string if not found.

func FormatUnixtsString added in v1.11.1

func FormatUnixtsString(ts string, layout string) string

FormatUnixtsString converts a unix timestamp string to a human readable

func GetBoolEnv added in v1.7.3

func GetBoolEnv(key string, fallback bool) bool

GetBoolEnv read an OS Env variable and convert to bool

func GetBoolVal added in v1.7.3

func GetBoolVal(value string) (bool, error)

GetBoolVal convert String to bool

func GetContainerHostAndPort added in v1.9.0

func GetContainerHostAndPort(container *dockertest.Resource, portID string) (server string, port int)

GetContainerHostAndPort returns the mapped host and port of a docker container for a given portID

func GetDockerAPIVersion added in v1.19.0

func GetDockerAPIVersion(client *docker.Client) (v string)

GetDockerAPIVersion returns the running supported docker API version

func GetDockerHost added in v1.19.0

func GetDockerHost(pool *dockertest.Pool) string

GetDockerHost returns the docker host from a pool

func GetDockerPool added in v1.9.0

func GetDockerPool() (*dockertest.Pool, error)

GetDockerPool initializes a docker pool

func GetEnv

func GetEnv(key, fallback string) string

GetEnv read an OS Env variable

func GetFloatEnv added in v1.7.3

func GetFloatEnv(key string, fallback float64) float64

GetFloatEnv read an OS Env variable and convert to float64

func GetFloatVal added in v1.7.3

func GetFloatVal(value string) (float64, error)

GetFloatVal convert String to float64

func GetGitRootDir added in v1.9.6

func GetGitRootDir(start string) (rootDir string, err error)

GetGitRootDir returns the root directory of a git repository

func GetGitlabJobURL added in v1.16.0

func GetGitlabJobURL() string

func GetGitlabPipelineURL added in v1.16.0

func GetGitlabPipelineURL() string

func GetHexInt64Val added in v1.14.0

func GetHexInt64Val(value string) (int64, error)

func GetHostPort added in v1.9.0

func GetHostPort(input string) (host string, port int, err error)

GetHostPort returns host and port from a string

func GetHostname added in v1.10.1

func GetHostname() string

GetHostname returns the hostname as full qualified domain name

func GetInt64Val added in v1.9.0

func GetInt64Val(value string) (int64, error)

GetInt64Val convert String to int64

func GetIntEnv added in v1.7.3

func GetIntEnv(key string, fallback int) int

GetIntEnv read an OS Env variable and convert to int

func GetIntVal added in v1.7.3

func GetIntVal(value string) (int, error)

GetIntVal convert String to int

func GetLastCommit added in v1.9.6

func GetLastCommit(gitDir string, filename string) (c *object.Commit, err error)

GetLastCommit returns a string with last commit details for given file

func GetStringEnv added in v1.7.3

func GetStringEnv(key string, fallback string) string

GetStringEnv read an OS Env variable

func GetVersionedDockerPool added in v1.19.0

func GetVersionedDockerPool(version string) (pool *dockertest.Pool, err error)

GetVersionedDockerPool returns a docker pool with a specific docker version, use running version if empty

func HTTPGet added in v1.14.0

func HTTPGet(url string, timeout int) (resp string, err error)

HTTPGet returns the body of a GET request

func InArray added in v1.9.6

func InArray(needle interface{}, haystack interface{}) (exists bool, index int)

InArray will search element inside array with any type. Will return boolean and index for matched element. True and index more than 0 if element is exist. needle is element to search, haystack is slice of value to be search. Taken from https://github.com/SimonWaldherr/golang-examples/blob/master/advanced/in_array.go

func IsDir added in v1.9.6

func IsDir(name string) bool

IsDir checks if a filename is a directory

func IsFile added in v1.9.6

func IsFile(name string) bool

IsFile checks if a filename is a file

func IsGitFile added in v1.9.6

func IsGitFile(filename string) (rootDir string, gitFileName string, err error)

IsGitFile returns nil if given file is part of a git repo or a meaningful message

func IsNil added in v1.9.3

func IsNil(i interface{}) bool

IsNil checks if an interface is nil

func IsNumeric added in v1.11.1

func IsNumeric(s string) bool

IsNumeric checks if a trimmed string is numeric using regexp

func MergeMaps added in v1.17.0

func MergeMaps(m1, m2 interface{}) (interface{}, error)

MergeMapsAny merges two maps of any types, inserting all key-value pairs from the second map into the first map, and returns the updated map. Note: Both input maps must be of the same type.

func PromptPassword added in v1.24.0

func PromptPassword(label string) (pw string, err error)

func RandString added in v1.14.4

func RandString(n int) string

RandString generates a pseudo random string with letters and digits with desired length

func ReadFileByLine

func ReadFileByLine(filename string) ([]string, error)

ReadFileByLine read a file and return array of lines

func ReadFileToString

func ReadFileToString(filename string) (string, error)

ReadFileToString read a file and return a string

func ReadFileToStruct added in v1.15.0

func ReadFileToStruct(filename string, data any) (err error)

ReadFileToStruct reads a file or from stdin and fills a given struct

func ReadStdinByLine added in v1.14.11

func ReadStdinByLine() ([]string, error)

ReadStdinByLine reads stdin and returns array of lines

func ReadStdinToString added in v1.14.11

func ReadStdinToString() (string, error)

ReadStdinToString reads stdin and returns a string

func RemoveSpace added in v1.4.0

func RemoveSpace(s string) string

RemoveSpace deletes all spaces and newlines from string

func ReverseMap added in v1.14.0

func ReverseMap[M ~map[K]V, K comparable, V comparable](m M) map[V]K

ReverseMap will reverse a map

func SetHostPort added in v1.9.0

func SetHostPort(host string, port int) string

SetHostPort returns host:port from a string host and port int

func StructToJSON added in v1.18.0

func StructToJSON(m interface{}) (string, error)

func StructToMap added in v1.14.8

func StructToMap(obj interface{}) (map[string]interface{}, error)

StructToMap converts a struct to a map

func StructToString added in v1.14.8

func StructToString(o interface{}, prefix string) (s string)

StructToString converts a struct to sorted key string

func WriteStringToFile added in v1.14.4

func WriteStringToFile(filename string, content string) error

WriteStringToFile saves a string to filename and assign rights 0600

Types

This section is empty.

Jump to

Keyboard shortcuts

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