General

package
v0.3.22 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 8 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FinalizeResult added in v0.2.20

func FinalizeResult(logger *log.Logger, result error, isSetup bool) int

FinalizeResult is a function that finalizes the result of a program and logs the error message to a log file.

If an error occurs during the execution of the program, this function will log the error message to the specified log file and exit the program with the Error exit code. If no error occurs, the function will log a success message to the log file and exit the program with the Success exit code.

Parameters:

  • logger: The logger to use for logging the error message. If nil, the error message is logged to the console.
  • result: The result of the program. If nil, the program is considered to have finished successfully.

Return:

  • int: The exit code of the program.

Example:

var logger *ers.FileLogger

func main() {
	// Set up the logger
	var err error
	logger, err = ers.NewFileLogger("log.txt")
	if err != nil {
		os.Exit(FinalizeResult(logger, err, true))
	}
	defer logger.Close()

	defer RecoverFromPanic(logger) // handle panics gracefully

	os.Exit(FinalizeResult(logger, mainBody(), false)) // handle errors gracefully
}

func mainBody() error {
	// Perform the main logic of the program
}

func IsNil added in v0.2.9

func IsNil[T any](value T) bool

IsNil is a function that checks if a value is nil.

Parameters:

  • value: The value to check.

Return:

  • bool: True if the value is nil, false otherwise.

func RecoverFromPanic added in v0.2.20

func RecoverFromPanic(logger *log.Logger)

RecoverFromPanic is a function that recovers from a panic and logs the error message to a log file.

If a panic occurs during the execution of the program, this function will recover from the panic, log the error message to the specified log file, and exit the program with the Panic exit code.

Parameters:

  • logger: The logger to use for logging the error message. If nil, the error message is logged to the console.

func RunInPowerShell added in v0.2.20

func RunInPowerShell(program string, args ...string) uc.MainFunc

RunInPowerShell is a function that returns a function that runs a program in a new PowerShell process.

Upon calling the returned function, a new PowerShell process is started with the specified program and arguments. The function returns an error if the process cannot be started.

Parameters:

  • program: The path to the program to run.
  • args: The arguments to pass to the program.

Return:

  • MainFunc: A function that runs the program in a new PowerShell process.

func SplitIntoGroups

func SplitIntoGroups[T any](slice []T, n int) ([][]T, error)

SplitIntoGroups splits the slice into n groups and returns a 2D slice where each inner slice represents a group.

Parameters:

  • slice: The slice to split.
  • n: The number of groups to split the slice into.

Return:

  • [][]T: A 2D slice where each inner slice represents a group.
  • error: An error of type *ers.ErrInvalidParameter if n is less than or equal to 0.

Example:

slice := []int{1, 2, 3, 4, 5}
n := 3
groups := SplitIntoGroups(slice, n)
fmt.Println(groups) // Output: [[1 4] [2 5] [3]]

Types

type ExitCode added in v0.2.20

type ExitCode int8

ExitCode is a custom type that represents the exit code of a program.

const (
	// Success indicates that the program has finished successfully.
	Success ExitCode = iota

	// Panic indicates that a panic occurred during the execution of the program.
	Panic

	// SetupFailed indicates that the program could not be set up.
	SetupFailed

	// Error indicates that an error occurred during the execution of the program.
	Error
)

func (ExitCode) String added in v0.2.20

func (ec ExitCode) String() string

String is a method of fmt.Stringer interface.

Return:

  • string: A string representation of the exit code.

Jump to

Keyboard shortcuts

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