Skip to content

go-libs : utils

This is the foundation package imported by all other go-libs packages. It provides display helpers, argument parsing, thread-safe debug control, and utility functions.


Thread-safe debug flag

sxUtils.SetDebug(true)   // enable debug output
sxUtils.GetDebug()       // returns current bool value
Function Signature Return Description
SetDebug v bool - Enable or disable debug output (thread-safe)
GetDebug - bool Return the current debug flag (thread-safe)

Backed by sync/atomic.Bool. Never access the internal debugEnabled variable directly.


ExitFunc

// Override in tests to avoid os.Exit
utils.ExitFunc = func(code int) { panic(code) }

ExitFunc is the function called by CmdDisplay.ExitError. It defaults to os.Exit. Override it in tests to prevent the process from terminating.


CmdDisplay

CmdDisplay provides unified console output for CLI tools.

Constructor

display := sxUtils.NewCmdDisplay("mygroup")
Parameter Kind Description
groupName string Label shown in every log line

Returns *CmdDisplay.

Methods

Method Signature Return Description
Debug msg string - Print debug line (only if SetDebug(true))
Info msg string - Print info line
Warning msg string - Print warning line
Error msg string - Print error line
ExitError msg string, errorCode int - Print error and call ExitFunc(errorCode)

ArgParser

ArgParser wraps os.Args (or any []string) with a fluent API for flag inspection and manipulation.

Constructor

parser := sxUtils.NewArgParser(os.Args)

Returns *ArgParser.

Methods

Method Signature Return Description
Debug - *ArgParser Print current args in debug mode
HasFlag flag string bool Return true if the flag is present
GetFlagPos flag string int Return index of the flag (-1 if absent)
GetFlagNextVal flag string string Return the value immediately after the flag
RemoveFlag flag string bool Remove the flag from the args list
RemoveFlagFromPos pos int bool Remove the arg at the given position

Utility functions

Function Signature Return Description
RemoveFlag args []string, flagToRemove string []string Return args without the named flag
RemoveFlagFromPos args []string, pos int []string Return args without the element at pos
ExecuteCommand command string - Execute a shell command and print output
WriteFile filePath string, content string error Write content to a file (create if absent)