goutil

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 21 Imported by: 0

README

Go Util

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

Usage


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

func main(){
  goutil.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.Encrypt("my message", "password")
  goutil.Decrypt(encrypted, "password")

  // simple gzip compression for strings
  compressed := goutil.Compress("my long string")
  goutil.Decompress(compressed)

  // watch a directory recursively
  goutil.WatchDir("my/folder", &goutil.Watcher{
    FileChange: func(path string, op string){
      // do something when a file changes
      path // the file path the change happened to
      op // the change operation
    },

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

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

    Any: func(path string, op string){
      // do something every time something happenes
    },
  })
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VarType map[string]reflect.Type

Functions

func CleanArray

func CleanArray(data []interface{}) []interface{}

Runs CleanStr on an array CleanStr: Sanitizes a string to valid UTF-8

func CleanByte

func CleanByte(b []byte) []byte

Sanitizes a []byte to valid UTF-8

func CleanJSON

func CleanJSON(val interface{}) interface{}

Runs CleanStr on a complex json object recursively CleanStr: Sanitizes a string to valid UTF-8

func CleanMap

func CleanMap(data map[string]interface{}) map[string]interface{}

Runs CleanStr on a map[string] CleanStr: Sanitizes a string to valid UTF-8

func CleanStr

func CleanStr(str string) string

Sanitizes a string to valid UTF-8

func Compress

func Compress(msg string) (string, error)

Gzip compression for a string

func Contains

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

Returns true if an array contains a value

func ContainsMap

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

Returns true if a map contains a value

func ContainsMapKey

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

Returns true if a map contains a key

func DecodeJSON

func DecodeJSON(data io.Reader) (map[string]interface{}, error)

Useful for decoding a JSON output from the body of an http request goutil.DecodeJSON(r.Body)

func Decompress

func Decompress(str string) (string, error)

Gzip decompression for a string

func Decrypt

func Decrypt(text string, key string) ([]byte, error)

AES-CFB Decryption

func DecryptLocal added in v2.0.1

func DecryptLocal(ciphertext string, key string) ([]byte, error)

Non Standard AES-CFB Decryption Purposely incompatible with other libraries and programing languages This was made by accident, and this bug is now a feature Notice: This Feature Is Experimental

func DeepCopyJson

func DeepCopyJson(data map[string]interface{}) (map[string]interface{}, error)

func Encrypt

func Encrypt(text string, key string) (string, error)

AES-CFB Encryption

func EncryptLocal added in v2.0.1

func EncryptLocal(text []byte, key string) (string, error)

Non Standard AES-CFB Encryption Purposely incompatible with other libraries and programing languages This was made by accident, and this bug is now a feature Notice: This Feature Is Experimental

func EscapeHTML

func EscapeHTML(html []byte) []byte

Replaces HTML characters with html entities Also prevents & from results

func EscapeHTMLArgs

func EscapeHTMLArgs(html []byte) []byte

Escapes quotes and backslashes for use within HTML quotes

func FormatMemoryUsage

func FormatMemoryUsage(b uint64) float64

Converts bytes to megabytes

func GetFileFromParent

func GetFileFromParent(root string, start string, search string) (string, bool)

Checks if the parent (or sub parent) directory of a file contains a specific file or folder root: the highest grandparent to check before quitting start: the lowest level to start searching from (if a directory is passed, it will not be included in your search) search: what file you want to search fro

func GetLinuxInstaller

func GetLinuxInstaller(man []string) string

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

func HasLinuxPkg

func HasLinuxPkg(pkg []string) bool

Attempt to check if a linux package is installed

func IndexOf added in v2.0.1

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

Returns the index of a value in an array returns -1 and an error if the value is not found

func IndexOfMap added in v2.0.1

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

Returns the index of a value in a map returns an error if the value is not found

func InstallLinuxPkg

func InstallLinuxPkg(pkg []string, man ...string)

Attempt 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

func IsZeroOfUnderlyingType

func IsZeroOfUnderlyingType(x interface{}) bool

func JoinPath

func JoinPath(path ...string) (string, error)

Joins multiple file types with safety from backtracking

func ParseJson

func ParseJson(b []byte) (map[string]interface{}, error)

Converts a json string into a map of strings

func StringifyJSON

func StringifyJSON(data interface{}, ind ...int) ([]byte, error)

Converts a map or array to a JSON string

func ToByteArray

func ToByteArray(res interface{}) []byte

Converts multiple types to a []byte accepts: string, []byte, byte, int32, int, int64, float64, float32

func ToInt

func ToInt(res interface{}) int

Converts multiple types to an int accepts: int, int32, int64, float64, float32, string, []byte, byte

func ToString

func ToString(res interface{}) string

Converts multiple types to a string accepts: string, []byte, byte, int32, int, int64, float64, float32

func TrimRepeats added in v2.0.2

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

Trims repeating adjacent characters and reduces them to one character b: byte array to trim chars: list of bytes to trim repeats of

func WatchDir

func WatchDir(root string, cb *Watcher)

Watch the files in a directory and its subdirectories for changes

Types

type Hashable

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

type Watcher

type Watcher struct {
	FileChange func(path string, op string)
	DirAdd     func(path string, op string) (addWatcher bool)
	Remove     func(path string, op string) (removeWatcher bool)
	Any        func(path string, op string)
}

A watcher instance for the WatchDir function

Jump to

Keyboard shortcuts

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