goutil

package module
v5.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 26 Imported by: 1

README

Go Utility Methods

donation link

A simple utility package for golang.

This module simply adds a variety of useful functions in an easy to use way.

Installation

  go get github.com/AspieSoft/goutil/v5

Usage


import (
  "github.com/AspieSoft/goutil/v5"
)

func main(){
  goutil.FS.JoinPath("root", "file") // a safer way to join 2 file paths without backtracking

  goutil.Contains([]any, any) // checks if an array contains a value

  // simple AES-CFB Encryption
  encrypted := goutil.Crypt.CFB.Encrypt([]byte("my message"), []byte("password"))
  goutil.Crypt.CFB.Decrypt(encrypted, []byte("password"))

  // simple gzip compression for strings
  // (also supports brotli and smaz)
  compressed := goutil.GZIP.Zip([]byte("my long string"))
  goutil.GZIP.UnZip(compressed)

  // watch a directory recursively
  watcher := goutil.FS.FileWatcher()

  watcher.OnFileChange = func(path, op string) {
    // do something when a file changes
    path // the file path the change happened to
    op // the change operation
  }

  watcher.OnDirAdd = func(path, op string) {
    // do something when a directory is added
    // return false to prevent that directory from being watched
    return true
  }

  watcher.OnRemove = func(path, op string) {
    // do something when a file or directory is removed
    // return false to prevent that directory from no longer being watched
    return true
  }

  watcher.OnAny = func(path, op string) {
    // do something every time something happenes
  }

  // add a directory to the watch list
  watcher.WatchDir("my/folder")

  // close a specific watcher
  watcher.CloseWatcher("my/folder")

  // close all watchers
  watcher.CloseWatcher("*")

  // wait for all watchers to finish closing
  watcher.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BROTLI *compBrotli = &compBrotli{}
View Source
var Clean *clean = &clean{}
View Source
var Conv *typeConv = &typeConv{}
View Source
var Crypt *crypt = &crypt{
	cryptCFB{},
	cryptHash{},
}

Encryption

View Source
var FS *fileSystem = &fileSystem{}
View Source
var GZIP *compGzip = &compGzip{}
View Source
var HTML *encodeHtml = &encodeHtml{}
View Source
var JSON *encodeJson = &encodeJson{}
View Source
var SMAZ *compSmaz = &compSmaz{}
View Source
var VarType map[string]reflect.Type

Functions

func ArrayEqual added in v5.1.0

func ArrayEqual[T any](arr1 []T, arr2 []T, ignoreLength ...bool) bool

ArrayEqual returns true if 2 arrays are equal and of the same length (even if they are in a different order)

func Contains

func Contains[T any](search []T, value T) bool

Contains returns true if an array contains a value

func ContainsMap

func ContainsMap[T Hashable, J any](search map[T]J, value J) bool

ContainsMap returns true if a map contains a value

func ContainsMapKey

func ContainsMapKey[T Hashable, J any](search map[T]J, key T) bool

ContainsMapKey returns true if a map contains a key

func FormatMemoryUsage

func FormatMemoryUsage(b uint64) float64

FormatMemoryUsage converts bytes to megabytes

func IndexOf

func IndexOf[T any](search []T, value T) (int, error)

IndexOf returns the index of a value in an array

returns -1 and an error if the value is not found

func IndexOfMap

func IndexOfMap[T Hashable, J any](search map[T]J, value J) (T, error)

IndexOfMap returns the index of a value in a map

returns an error if the value is not found

func IsZeroOfUnderlyingType

func IsZeroOfUnderlyingType(x interface{}) bool

IsZeroOfUnderlyingType can be used to determine if an interface{} in null or empty

func MapArgs

func MapArgs(args ...[]string) map[string]string

MapArgs will convert a bash argument array ([]string) into a map (map[string]string)

When @args is left blank with no values, it will default to os.Args[1:]

-- Arg Convertions:

"--Key=value" will convert to "key:value"

"--boolKey" will convert to "boolKey:true"

"-flags" will convert to "f:true, l:true, a:true, g:true, s:true" (only if its alphanumeric [A-Za-z0-9]) if -flags is not alphanumeric (example: "-test.paniconexit0" "-test.timeout=10m0s") it will be treated as a --flag (--key=value --boolKey)

keys that match a number ("--1" or "-1") will start with a "-" ("--1=value" -> "-1:value", "-1" -> -1:true) this prevents a number key from conflicting with an index key

everything else is given a number value index starting with 0

this method will not allow --args to have their values modified after they have already been set

func MapArgsByte

func MapArgsByte(args ...[][]byte) map[string][]byte

MapArgs is just like MapArgs, but it excepts and outputs using []byte instead of string

func MapEqual added in v5.1.0

func MapEqual[T Hashable, J any](map1 map[T]J, map2 map[T]J, ignoreLength ...bool) bool

MapEqual returns true if 2 maps are equal and of the same length (even if they are in a different order)

func ToType

func ToType[T SupportedType](val interface{}) T

ToType attempts to converts an interface{} from the many possible types in golang, to a specific type of your choice

if it fails to convert, it will return a nil/zero value for the appropriate type

func ToVarType added in v5.1.0

func ToVarType[T SupportedType](val interface{}, ref T) T

ToVarType grabs the type from another var as a reference, and runs the `ToType` with the ref type

func ToVarTypeInterface added in v5.1.0

func ToVarTypeInterface(val interface{}, ref interface{}) interface{}

ToVarTypeInterface attempts to convert an interface to match the unknown type of another interface

this method is similar to the ToType method, but it simply returns nil if it cannot find the proper var type

func TrimRepeats

func TrimRepeats(b []byte, chars []byte) []byte

TrimRepeats trims repeating adjacent characters and reduces them to one character

@b: byte array to trim

@chars: list of bytes to trim repeats of

func TypeEqual added in v5.1.0

func TypeEqual(val1 interface{}, val2 interface{}) bool

ToVarTypeInterface attempts to convert an interface to match the unknown type of another interface

this method is similar to the ToType method, but it simply returns nil if it cannot find the proper var type

Types

type FileWatcher

type FileWatcher struct {

	// when a file changes
	//
	// @path: the file path the change happened to
	//
	// @op: the change operation
	OnFileChange func(path string, op string)

	// when a directory is added
	//
	// @path: the file path the change happened to
	//
	// @op: the change operation
	//
	// return false to prevent that directory from being watched
	OnDirAdd func(path string, op string) (addWatcher bool)

	// when a file or directory is removed
	//
	// @path: the file path the change happened to
	//
	// @op: the change operation
	//
	// return false to prevent that directory from no longer being watched
	OnRemove func(path string, op string) (removeWatcher bool)

	// every time something happenes
	//
	// @path: the file path the change happened to
	//
	// @op: the change operation
	OnAny func(path string, op string)
	// contains filtered or unexported fields
}

A watcher instance for the `FS.FileWatcher` method

func (*FileWatcher) CloseWatcher

func (fw *FileWatcher) CloseWatcher(root string) error

CloseWatcher will close the watcher by the root name you used

@root pass a file path for a specific watcher or "*" for all watchers that exist

func (*FileWatcher) Wait

func (fw *FileWatcher) Wait()

Wait for all Watchers to close

func (*FileWatcher) WatchDir

func (fw *FileWatcher) WatchDir(root string) error

WatchDir watches the files in a directory and its subdirectories for changes

type Hashable

type Hashable interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | string | complex64 | complex128
}

type LinuxPKG

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

linux package installer

func (*LinuxPKG) GetLinuxInstaller

func (linuxPKG *LinuxPKG) GetLinuxInstaller(man []string) string

GetLinuxInstaller attempt to find out what package manager a linux distro is using or has available

func (*LinuxPKG) HasLinuxPkg

func (linuxPKG *LinuxPKG) HasLinuxPkg(pkg []string) bool

HasLinuxPkg attempt to check if a linux package is installed

func (*LinuxPKG) InstallLinuxPkg

func (linuxPKG *LinuxPKG) InstallLinuxPkg(pkg []string, man ...string)

InstallLinuxPkg attempts to install a linux package

this method will also resolve the sudo command and ask for a user password if needed

this method will not attempt to run an install, if it finds the package is already installed

type NullType

type NullType[T any] struct {
	Null T
}

type SupportedType

type SupportedType interface {
	string | []byte | byte | bool |
		int | int64 | int32 | int16 | int8 |
		uint | uint64 | uint32 | uint16 | uintptr |
		float64 | float32 |
		[]interface{} | []string | [][]byte | []bool |
		[]int | []int64 | []int32 | []int16 | []int8 |
		[]uint | []uint64 | []uint32 | []uint16 | []uintptr |
		[]float64 | []float32 |
		map[string]interface{} | map[byte]interface{} |
		map[int]interface{} | map[int64]interface{} | map[int32]interface{} | map[int16]interface{} | map[int8]interface{} |
		map[uint]interface{} | map[uint64]interface{} | map[uint32]interface{} | map[uint16]interface{} | map[uintptr]interface{} |
		map[float64]interface{} | map[float32]interface{}
}

SupportedType is an interface containing the types which are supported by the ToType method

type ToInterface

type ToInterface struct {
	Val interface{}
}

Jump to

Keyboard shortcuts

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