utils

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package utils has all the utils required for hulak, including but not limited to CreateFilePath, CreateDir, CreateFiles, ListMatchingFiles, MergeMaps and more..

Index

Constants

View Source
const (
	Red        = "\033[91m"
	Green      = "\033[92m"
	Yellow     = "\033[93m"
	Grey       = "\033[90m"
	ColorReset = "\033[0m"
	Blue       = "\033[94m"
)

Colors

View Source
const (
	EnvironmentFolder    = "env"
	EnvKey               = "hulakEnv"
	DefaultEnvVal        = "global"
	DefaultEnvFileSuffix = ".env"
)

Environment

View Source
const (
	UnResolvedVariable = "unresolved variable "
	EmptyVariables     = "variable string can't be empty"
	IndexOutOfBounds   = "array index out of bounds: "
	KeyNotFound        = "key not found: "
)

Errors message

View Source
const (
	YAML = ".yaml"
	YML  = ".yml"
	JSON = ".json"
)

acceptable file patterns

View Source
const (
	ResponseBase       = "_response"
	ResponseFileSuffix = ".json"
	ResponseFileName   = ResponseBase + ResponseFileSuffix
)

response pattern for files saved

View Source
const (
	JSONString = "string"
	JSONNumber = "number"
	JSONInt    = "int"
	JSONBool   = "bool"
	JSONNull   = "null"
)

JSON supported types

View Source
const (
	DirPer  fs.FileMode = 0755
	FilePer fs.FileMode = 0644
)

Permissions for creating directory and files

View Source
const (
	CheckMark = "\u2713"
	CrossMark = "\u2717"
)

tick mark and x for success and failure

View Source
const ResponseType = "code"

ResponseType is Auth2.0 ResponseType

Variables

This section is empty.

Functions

func ColorError

func ColorError(errMsg string, errs ...error) error

ColorError Creates an error message that optionally includes an additional error. If an error is provided, it formats the message with the error appended. The returned error is colored for console output.

func ConvertKeysToLowerCase

func ConvertKeysToLowerCase(dict map[string]any) map[string]any

ConvertKeysToLowerCase converts all keys in a map to lowercase recursively except "variables" as Graphql variables is case-sensitive

func CopyEnvMap

func CopyEnvMap(original map[string]any) map[string]any

CopyEnvMap Copies the Environment map[string]any and returns a map[string]string EnvMap is a simple JSON without any nested properties. Mostly used for goroutines.

func CreateDir

func CreateDir(dirPath string) error

CreateDir checks for the existence of a directory at the given path, and creates it with permissions 0755 if it does not exist.

func CreateFile

func CreateFile(filePath string) error

CreateFile checks for the existence of a file at the given filePath, and creates it if it does not exist.

func CreatePath

func CreatePath(filePath string) (string, error)

CreatePath creates and returns file or directory path by joining the project root with provided filePath

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists and is accessible at the given path Returns true if the file exists and is readable, false otherwise

func FileNameWithoutExtension

func FileNameWithoutExtension(path string) string

FileNameWithoutExtension takes in filepath and returns the name of the file

func GetEnvFiles

func GetEnvFiles() ([]string, error)

GetEnvFiles returns a list of environment file names from the env folder

func ListFiles

func ListFiles(dirPath string, options ...ListFilesOption) ([]string, error)

ListFiles lists all .yaml, .yml, or .json files in a directory with configurable directory exclusion Files are added as they are discovered so it does not guarantee any files are run before the other

func ListMatchingFiles

func ListMatchingFiles(matchFile string, initialPath ...string) ([]string, error)

ListMatchingFiles searches for files matching the "matchFile" name (case-insensitive, .yaml/.yml or .json only) in the specified directory and its subdirectories. If no directory is specified, it starts from the project root. Includes all directories in traversal, including hidden ones. Returns slice of matched file paths and an error if no matching files are found or if there are file system errors.

func LookupValue

func LookupValue(key string, data map[string]any) (any, error)

LookupValue retrieves the value for a given key or path from the map. A key can be a simple string like "name", or a path like "user.name", where "user" is an object and "name" is a key inside it. Use {} to escape dots in keys (e.g., "{user.name}"). For arrays, reference an index with square brackets (e.g., myArr[0] for the first element). You can also access nested properties like myArr[0].name for the "name" key of the first array element.

func MarshalToJSON

func MarshalToJSON(value any) (any, error)

MarshalToJSON is basically JSON.stringify equivalent for go

func MergeMaps

func MergeMaps(main, sec map[string]string) map[string]string

MergeMaps merges the secondary map into the main map. If keys are repeated, values from the secondary map replace those in the main map.

func PanicRedAndExit

func PanicRedAndExit(msg string, args ...any)

PanicRedAndExit Print message in Red and os.Exit(1)

func ParseArrayKey

func ParseArrayKey(segment string) (bool, string, int)

ParseArrayKey checks if array has proper syntax

func PrintGreen

func PrintGreen(msg string)

PrintGreen Prints Success Message

func PrintInfo

func PrintInfo(msg string)

PrintInfo prints the info for the user in blue

func PrintJSONColored added in v0.1.3

func PrintJSONColored(data []byte) error

PrintJSONColored Pretty-prints JSON using the fatih/color package

func PrintRed

func PrintRed(msg string)

PrintRed is used mostly for errors

func PrintWarning

func PrintWarning(msg string)

PrintWarning Inform or Warn the user

func SanitizeDirPath

func SanitizeDirPath(dirPath string) (string, error)

SanitizeDirPath cleans up the directory path to avoid traversals

Types

type ListFilesOption

type ListFilesOption func(*listFilesOptions)

ListFilesOption lists the options we should skip

func WithRespectDotDirs

func WithRespectDotDirs(respect bool) ListFilesOption

WithRespectDotDirs controls whether to traverse dot directories

func WithSkipDirs

func WithSkipDirs(dirs []string) ListFilesOption

WithSkipDirs specifies directories to skip

Jump to

Keyboard shortcuts

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