mttools

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 25 Imported by: 11

Documentation

Index

Constants

View Source
const SystemdServiceDirPath = "/etc/systemd/system"

Variables

This section is empty.

Functions

func AbsInt

func AbsInt(value int) int

Absolute value for integer

func AbsInt64

func AbsInt64(value int64) int64

Absolute value for integer

func AnyToBool

func AnyToBool(v any) bool

Any value to bool.

func AnyToInt64Ok

func AnyToInt64Ok(v any) (int64, bool)

Convert any value to Int64. Ok = true if it was converted

func AnyToInt64OrZero

func AnyToInt64OrZero(v any) int64

Try best to convert any value to int64. Returns 0 if value can not be converted.

func AnyToString

func AnyToString(v any) string

Try best to convert any value to string, even if it is not a string at all.

func AnyToStringOk

func AnyToStringOk(v any) (s string, b bool)

Convert any value to string. Ok = true if it is a string or Stringer, false if not. https://stackoverflow.com/questions/72267243/unioning-an-interface-with-a-type-in-golang

func AskUserChoiceMultiple added in v1.0.7

func AskUserChoiceMultiple(prompt string, options_list []string, unique bool) (choice_list []int, err error)

Prints options list and asks user for choice in console. Multiple options can be chosen. Input of single "0" (zero) means "Cancel" and [-1] slice returned

func AskUserChoiceSingle added in v1.0.7

func AskUserChoiceSingle(prompt string, options_list []string) (choice int, err error)

Prints options list and asks user for choice in console. First chosen option is returned. -1 returned if nothing was selected or error occurred.

func AssertEmpty

func AssertEmpty(v any, message ...string)

func AssertEqual

func AssertEqual[T comparable](a T, b T, message ...string)

func AssertNil

func AssertNil(v any, message ...string)

func AssertNotEmpty

func AssertNotEmpty(v any, message ...string)

func AssertNotNil

func AssertNotNil(v any, message ...string)

func CallerSignature added in v1.0.2

func CallerSignature(skip int) string

func CountValues

func CountValues(value interface{}, values_list ...interface{}) (count int)

Returns number of `value` values found in `values_list`

func ExecCmd

func ExecCmd(cmd_path string, args []string) (string, error)

Executes cmd and returns its output or err

func ExecCmdPrint

func ExecCmdPrint(cmd_path string, args []string) error

Executes cmd and prints its output

func ExecCmdWaitAndPrint

func ExecCmdWaitAndPrint(cmd_path string, args []string) (output string, err error)

Executes cmd printing its stdout+stderr to current stdout during execution and returns its output after

func ExecCommandLine

func ExecCommandLine(command_line string) (string, error)

Executes whole string in command-line shell. Returns its output and/or error.

func FileSha256

func FileSha256(path string) (string, error)

func FormatFileSize

func FormatFileSize(size int64) string

Convert fille size to human-readable string

func GetDirAbsolutePath

func GetDirAbsolutePath(path string) (abs_path string, err error)

Returns absolute path for directory. If `path` is "" current working directory is used. Checks if directory exists.

func HideConsole

func HideConsole()

Hides console (only under Windows)

Thanks to SyncThing! https://github.com/syncthing/syncthing/blob/main/lib/osutil/hidden_windows.go

func InSlice added in v1.0.5

func InSlice[E comparable, S ~[]E](needle E, slice S) bool

Returns true if needle is in slice (like PHP's in_array() function).

func Int64Sign

func Int64Sign(value int64) int64

Returns [+1;0;-1] depending on value's sign.

func IntSign

func IntSign(value int) int

Returns [+1;0;-1] depending on value's sign.

func IsDirEmpty added in v1.0.6

func IsDirEmpty(path string) (bool, error)

Returns true if directory is empty. Directory should exist and be accessible (permissions).

func IsDirExists

func IsDirExists(path string) bool

Returns true if directory exists and access is not denied.

func IsEmpty

func IsEmpty(v any) bool

Returns true if v value considered as empty (nil, "", 0 for int, 0.0 for float, false for bool).

func IsFileExists

func IsFileExists(path string) bool

Returns true if file exists and access is not denied.

func IsLinux

func IsLinux() bool

func IsStructEmbeds

func IsStructEmbeds(s interface{}, embedded_type reflect.Type) bool

Checks if s - variable of struct type and it embeds embedded_type

func IsStructTypeEmbeds

func IsStructTypeEmbeds(s_type reflect.Type, embedded_type reflect.Type) bool

Checks if s_type - is a struct (or pointer to it) and it embeds embedded_type

func IsSystemdAvailable

func IsSystemdAvailable() bool

func IsWindows

func IsWindows() bool

func LoadYamlSettingFromFile

func LoadYamlSettingFromFile(path string, settings interface{}) error

func PanicWithSignature added in v1.0.2

func PanicWithSignature(message any) string

func PanicWithSignatureF added in v1.0.3

func PanicWithSignatureF(message string, args ...any) string

func PrintYamlSettings

func PrintYamlSettings(settings interface{})

func Ptr

func Ptr[T any](v T) *T

One-liner to get pointer to function returned value. Example: pointer_t := mttools.Ptr(time.Now()) // you can not do pointer_t := &time.Now()

func RandomLetter

func RandomLetter(upperOnly bool) string

Returns random letter. NOT cryptographically strong!

func RandomString

func RandomString(length int) string

Returns random string of given length. NOT cryptographically strong! https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

func RandomWord

func RandomWord(minLength, maxLength int, capitalize bool) string

Generate random word

func RandomWords

func RandomWords(count, minLength, maxLength int, capitalize bool) string

Generate sentence of random words

func SaveYamlSettingToFile

func SaveYamlSettingToFile(path string, comment string, settings interface{}) error

func SimpleGet

func SimpleGet(url string) (string, error)

Performs GET request, returns response body as a string.

func SliceWalk added in v1.0.2

func SliceWalk[S ~[]E, E any](originalSlice S, fn func(E) E) S

Returns new slice of same size and type, each element is result of calling fn() function.

func SlicesIntersection

func SlicesIntersection[S ~[]E, E comparable](slice_a S, slice_b S) S

Returns slice of values found in both slice_a and slice_b

func SplitArgumentsString

func SplitArgumentsString(str string) []string

Splits string into array of strings. Each argument in string is single word or number, or string in double quotes.

func UniqueSlice

func UniqueSlice[S ~[]E, E any](slice S) S

Returns slice of same type with just unique elements left in it

Types

type Lockable

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

func (*Lockable) Lock

func (o *Lockable) Lock()

func (*Lockable) TryLock

func (o *Lockable) TryLock() bool

func (*Lockable) Unlock

func (o *Lockable) Unlock()

type LockableI

type LockableI interface {
	Lock()
	TryLock() bool
	Unlock()
}

type ServiceData

type ServiceData struct {
	Name       string //service name
	User       string //user
	Group      string //group
	Executable string //path to executable file
	WorkingDir string //full working directory path
	Autostart  bool   //start service automatically
}

func (*ServiceData) InstallSystemdService

func (unit *ServiceData) InstallSystemdService() error

func (*ServiceData) UninstallSystemdService

func (unit *ServiceData) UninstallSystemdService() error

type Values

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

func NewValues

func NewValues() Values

func (*Values) CopyFrom

func (m *Values) CopyFrom(another *Values)

func (*Values) Get

func (m *Values) Get(key string) any

Gets value by key, returns nil if there is no such key. Use GetOk() if you need distinguish nil values from key absence.

func (*Values) GetNamesIterator

func (m *Values) GetNamesIterator() iter.Seq[string]

func (*Values) GetOk

func (m *Values) GetOk(key string) (v any, ok bool)

Gets value by key, returns ok=false if there is no such key.

func (*Values) Has added in v1.0.3

func (m *Values) Has(key string) bool

Returns true if value for key was set.

func (*Values) Set

func (m *Values) Set(key string, v any) *Values

Sets value

Jump to

Keyboard shortcuts

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