utils

package module
v2.0.12 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 14 Imported by: 0

README

GoUtils

Documentation

Testing codecov Go Report Card

## This is a collection of useful packages including:
  • Config
  • Environment
  • Logging
  • Signals
  • Testing

Config

package main

import (
    "github.com/nano-interactive/go-utils/v2/config
)

// Defaults

var DefaultConfig = Config {
    Env: "development",
    Name: "config",
    Type: "yaml",
}

func main() {
    config, err := config.New(config.Config)

    if err != nil {
        // Failed to load configuration
    }
}

Environment

Logging

Signals

Testing

Documentation

Index

Constants

View Source
const (
	UnknownIp           = "UNKNOWN IP"
	HeaderXForwardedFor = "X-Forwarded-For"
	HeaderXRealIP       = "X-Real-IP"
)
View Source
const (
	// RequestIdLength
	RequestIdLength = 32

	// MaxEncodedLength
	MaxEncodedLength = (RequestIdLength*8 + 5) / 6
)

Variables

View Source
var (
	GooglePlay  = []byte("play.google")
	ITunesApple = []byte("itunes.apple")

	HTTPS         = []byte("https://")
	HTTP          = []byte("http://")
	HTTPSProtocol = []byte("https:")
	HTTPProtocol  = []byte("http:")

	ErrInvalidUrl = errors.New("invalid url")
)
View Source
var ErrEscape = errors.New("invalid URL escape")

Functions

func AnonymizeIp

func AnonymizeIp(ip []byte) []byte

Slices IP address byte slice and writes 0 to last octet Example: AnonymizeIp([]byte{'1','9','2','.','1','7','2','.','9','0','.','7','0'}) Returns []byte{'1','9','2','.','1','7','2','.','9','0','.','0'}

func CopyBytes

func CopyBytes(input []byte) []byte

Provides a way of copying bytes into new byte slice Returns byte slice

func CreateDirectory

func CreateDirectory(path string, perm fs.FileMode) (string, error)

Provides a way of creating directory with permissions for a given path Returns string path of created directory and error if fails

func CreateDirectoryFromFile

func CreateDirectoryFromFile(path string, perm fs.FileMode) (string, error)

Provides a way of creating a directory from a path Returns created directory path and error if fails

func CreateFile

func CreateFile(path string, flags int, dirMode, mode fs.FileMode) (file *os.File, err error)

Creates file for given directory with flags and permissions for directory and file Returns file instance and error if it fails

func CreateLogFile

func CreateLogFile(path string) (file *os.File, err error)

Creates write only appendable file with permission 0o744 for directory and file for given path

func DecodeQuery

func DecodeQuery(dst []byte, src []byte) (int, error)

func DecodeQueryUnsafe

func DecodeQueryUnsafe(src []byte) ([]byte, error)

func DecodeQueryUnsafeString

func DecodeQueryUnsafeString(src []byte) (string, error)

func FileExists

func FileExists(path string) bool

Provides a way of checking if file exists. Returns bool

func GetAbsolutePath

func GetAbsolutePath(path string) (string, error)

Returns absolute path string for a given directory and error if directory doesent exist

func GetBrokenImageBytes

func GetBrokenImageBytes() []byte

func GetDomainFromUrl

func GetDomainFromUrl(fullUrl string) (string, error)

func GetLocalIP

func GetLocalIP() string

Returns IP address of local machine, empty string if fails

func GetLocalIPs

func GetLocalIPs() []string

Returns strinngs slice of IP found on local machine

func GetRequestId

func GetRequestId() (string, error)

Returns 32 character string of random bytes

func Getenv

func Getenv(env string, def ...string) string

Returns ENV string

func IsHex

func IsHex(c byte) bool

func IsInt

func IsInt(s string) bool

Provides a way of testing if type is integer Returns bool value depending if its integer or not

func IsSuccess

func IsSuccess(status int) bool

Returns bool value of HTTP status code if its failure or success

func RandomString

func RandomString(n int32) string

Returns random string of given length

func RealIp

func RealIp(peekable Peekable) []byte

Extracts first ip address from Peekable interface seperated by coma Returns nil if no values are presemt

func TrimUrlForScylla

func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err error)

func TrimUrlForScyllaOld added in v2.0.11

func TrimUrlForScyllaOld(fullUrl string) (scyllaUrl string, hostName string, err error)

func TruncateUrl

func TruncateUrl(value []byte) []byte

Truncates a given url

func UnHex

func UnHex(c byte) byte

func UniqueSliceElements

func UniqueSliceElements[T comparable](inputSlice []T) []T

Returns unique slice back

func UnsafeBytes

func UnsafeBytes(s string) []byte

#nosec G103

func UnsafeString

func UnsafeString(b []byte) string

#nosec G103

Types

type Peekable

type Peekable interface {
	Peek(key string) []byte
}

Directories

Path Synopsis
__mocks__
io
cmd

Jump to

Keyboard shortcuts

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