util

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusOk      RowStatus = "[✔]"
	StatusWarning RowStatus = "[i]"
	StatusError   RowStatus = "[✘]"
	StatusUnknown RowStatus = ""

	ColorOk      = "#00AA00"
	ColorWarning = "#AAAA00"
	ColorError   = "#AA0000"
)

Constants for row statuses and their associated colors.

Variables

This section is empty.

Functions

func DistinctSlice

func DistinctSlice[T comparable](s []T) []T

DistinctSlice returns a new slice with duplicate entries removed. The input slice must contain elements of a comparable type.

func EnvMap

func EnvMap(environ []string) map[string]string

EnvMap converts a slice of "key=value" strings into a map.

func EqualsOrRegexMatchString

func EqualsOrRegexMatchString(pattern string, s string, insensitive bool) bool

EqualsOrRegexMatchString checks if a string `s` matches a pattern strictly or via a regex. If `insensitive` is true, the match is case-insensitive.

func Error

func Error(a ...any)

Error prints an error-formatted string to the console.

func Errorf

func Errorf(format string, a ...any)

Errorf prints a formatted error string to the console.

func GetenvOrDefault

func GetenvOrDefault(key string, defaultValue string) string

GetenvOrDefault retrieves the value of the environment variable specified by `key`. If the variable is not set, it returns the provided `defaultValue`.

func Hdr

func Hdr(a ...any)

Hdr prints a header-formatted string to the console.

func Hdrf

func Hdrf(format string, a ...any)

Hdrf prints a formatted header string to the console.

func MapContainsKey

func MapContainsKey[K comparable, V interface{}](m map[K]V, key K) bool

MapContainsKey checks if a map contains a specific key.

func MapIntKeysToSortedSlice

func MapIntKeysToSortedSlice[V interface{}](m map[int]V) []V

MapIntKeysToSortedSlice converts a map with integer keys into a slice of values, sorted by the integer keys in ascending order.

func MarshalKoanfStructToBytes

func MarshalKoanfStructToBytes[T any](o T, p koanf.Parser) []byte

MarshalKoanfStructToBytes marshals a struct of type `T` into bytes using the specified `koanf.Parser`. This function uses the Koanf library to handle the marshaling process.

func MarshalToJsonBytes

func MarshalToJsonBytes[T any](o T) []byte

MarshalToJsonBytes marshals a struct of type `T` into JSON-encoded bytes.

func MarshalToYamlBytes

func MarshalToYamlBytes[T any](o T) []byte

MarshalToYamlBytes marshals a struct of type `T` into YAML-encoded bytes.

func MergeMaps

func MergeMaps[K comparable, V interface{}](maps ...map[K]V) map[K]V

MergeMaps performs a shallow merge of two or more maps into a single map. Duplicate keys in subsequent maps will take precedence and overwrite earlier values.

func Msg

func Msg(a ...any)

Msg prints a message-formatted string to the console.

func Msgf

func Msgf(format string, a ...any)

Msgf prints a formatted message string to the console.

func OsEnvMap

func OsEnvMap() map[string]string

OsEnvMap converts the current environment variables into a map.

func Print

func Print(a ...any)

Print prints a formatted string to the console.

func PrintBanner

func PrintBanner()

PrintBanner prints the Quartz ASCII art banner to the console.

func PrintRowStatusTable

func PrintRowStatusTable(headers []string, rows [][]string, statusFunc func(i int, row []string) RowStatus)

PrintRowStatusTable prints a formatted table with a status indicator column to the console.

func PrintTable

func PrintTable(headers []string, rows [][]string)

PrintTable prints a formatted table to the console.

func Printf

func Printf(format string, a ...any)

Printf prints a formatted string to the console.

func PromptYesNo

func PromptYesNo(msg string) bool

PromptYesNo displays a yes/no prompt to the console and returns true if "yes" was selected.

func RunOnce

func RunOnce(key string, f func() error) error

RunOnce ensures that a function identified by the given key is only executed the first time. Subequent calls will return a cached response.

func SetWriter

func SetWriter(w io.Writer)

SetWriter sets the output writer for console messages.

func ToInterfaceSlice

func ToInterfaceSlice[T any](s []T) []interface{}

ToInterfaceSlice converts a typed slice to a slice of `interface{}`.

func ToTypedSlice

func ToTypedSlice[T any](s []interface{}) []T

ToTypedSlice converts a slice of `interface{}` to a typed slice.

func TruncateString

func TruncateString(s string, n int) string

TruncateString truncates a string to the specified length `n`. If the string is shorter than or equal to `n`, it is returned unchanged.

func TruncateStringE

func TruncateStringE(s string, n int) string

TruncateStringE truncates a string to the specified length `n` and appends "..." if truncation occurs. If `n` is less than or equal to 3, the string is truncated without appending ellipses.

func ValueOrDefault

func ValueOrDefault[T comparable](val T, def T) T

ValueOrDefault returns `val` if it is not the zero value of its type. Otherwise, it returns the provided default value `def`.

func WriteBytesToFile

func WriteBytesToFile(b []byte, path string) error

WriteBytesToFile writes bytes to a file at the given path. It creates any required directories as needed.

func WriteStringToFile

func WriteStringToFile(s string, path string) error

WriteStringToFile writes a string to a file at the given path. It creates any required directories as needed.

func Zero

func Zero[T any]() T

Zero returns the zero value of any type `T`.

Types

type HttpClientFactory

type HttpClientFactory interface {
	NewClient() *http.Client
}

IHttpClientFactory defines an interface for creating new HTTP clients.

func NewHttpClientFactory

func NewHttpClientFactory() HttpClientFactory

type HttpClientFactoryImpl

type HttpClientFactoryImpl struct{}

HttpClientFactory is a default implementation of IHttpClientFactory that creates standard HTTP clients.

func (HttpClientFactoryImpl) NewClient

func (f HttpClientFactoryImpl) NewClient() *http.Client

NewClient creates and returns a new instance of an HTTP client.

type HttpClientFactoryMock

type HttpClientFactoryMock struct {
	Callback RoundTripFunc
}

HttpClientFactoryMock is a mock implementation of IHttpClientFactory for testing purposes. It uses a custom RoundTripFunc to handle HTTP requests.

func (HttpClientFactoryMock) NewClient

func (f HttpClientFactoryMock) NewClient() *http.Client

NewClient creates and returns a new instance of an HTTP client with a custom transport that uses the provided RoundTripFunc.

type RoundTripFunc

type RoundTripFunc func(req *http.Request) *http.Response

RoundTripFunc defines a function type that processes HTTP requests and returns HTTP responses. It is used for mocking HTTP client behavior.

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the RoundTripFunc for the given HTTP request and returns the corresponding HTTP response.

type RowStatus

type RowStatus string

RowStatus represents the status of a row in a table.

Jump to

Keyboard shortcuts

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