goodies

package
v0.0.0-...-a0dc343 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: GPL-3.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupQueue

type CleanupQueue struct {
	// contains filtered or unexported fields
}

CleanupQueue is a priority queue that runs cleanup tasks in order of priority and uses the defined error handler to handle errors.

func NewCleanupQueue

func NewCleanupQueue() *CleanupQueue

NewCleanupQueue creates a new cleanup queue.

func (*CleanupQueue) Add

func (q *CleanupQueue) Add(task func(args ...interface{}) error, args []interface{}, priority int, errorHandler ErrorHandler, ignoreErrorHandlerFailure bool)

Add adds a new task to the cleanup queue.

func (*CleanupQueue) Run

func (q *CleanupQueue) Run() error

Run runs the cleanup queue and executes all the tasks in order of priority. It returns an error if any of the tasks encounter an error, including the error handler function, unless the task is marked to ignore error handler failures.

type CleanupTask

type CleanupTask struct {
	Priority                  int
	Task                      func(args ...interface{}) error
	Args                      []interface{}
	ErrorHandler              ErrorHandler
	IgnoreErrorHandlerFailure bool
	// contains filtered or unexported fields
}

CleanupTask is a task that defines a cleanup task to be run in the cleanup queue. It has a priority, a task to run, a list of arguments, an error handler to handle errors, and a flag to ignore error handler failures.

type ErrorHandler

type ErrorHandler interface {
	// HandleError triggers the error handling logic
	HandleError(args ...interface{}) error
}

ErrorHandler is the protocol for handling errors, use this to implement custom error handling logic.

type ErrorHandlerFn

type ErrorHandlerFn func(args ...interface{}) error

ErrorHandlerFn is an error handler that implements the ErrorHandler protocol, use this to create custom error handling logic.

func (ErrorHandlerFn) HandleError

func (f ErrorHandlerFn) HandleError(args ...interface{}) error

type EventHandler

type EventHandler func(interface{})

EventHandler is the protocol for handling events, use this to implement custom event handling logics for your application.

type EventManager

type EventManager struct {
	// contains filtered or unexported fields
}

EventManager is a simple event manager that allows subscribing to events and notifying them, so that multiple parts of the code can be notified when an event happens and act accordingly.

func NewEventManager

func NewEventManager() *EventManager

NewEventManager creates a new event manager.

Example:

eventManager := goodies.NewEventManager()
eventManager.Subscribe("testEvent", func(data interface{}) {
	fmt.Println("Event happened:", data)
})
eventManager.Notify("testEvent", "testData")

func (*EventManager) Notify

func (em *EventManager) Notify(eventType string, data interface{})

Notify notifies an event type with some data, all the handlers subscribed to the event type will be called with the data.

Example:

eventManager.Notify("testEvent", "testData")

func (*EventManager) Subscribe

func (em *EventManager) Subscribe(eventType string, handler EventHandler) int

Subscribe subscribes to an event type and returns a subscription ID, use this ID to unsubscribe from the event later.

Example:

subID := eventManager.Subscribe("testEvent", func(data interface{}) {
	fmt.Println("Event happened:", data)
})
fmt.Println("Subscribed with ID:", subID)

func (*EventManager) Unsubscribe

func (em *EventManager) Unsubscribe(eventType string, id int)

Unsubscribe unsubscribes from an event type using its subscription ID

Example:

subID := eventManager.Subscribe("testEvent", func(data interface{}) {
	fmt.Println("Event happened:", data)
})
fmt.Println("Subscribed with ID:", subID)
eventManager.Unsubscribe("testEvent", subID)

type FailedCheck

type FailedCheck struct {
	ResourcePath  string
	RequestedHash string
	DetectedHash  string
}

FailedCheck represents a failed integrity check

type IntegrityCheckResult

type IntegrityCheckResult struct {
	TotalRequested int
	Passed         int
	Failed         int
	FailedChecks   []FailedCheck
}

IntegrityCheckResult represents the result of an integrity check

func CheckIntegrity

func CheckIntegrity(data []byte, method ValidatorType, prefix string) (IntegrityCheckResult, error)

CheckIntegrity performs an integrity check of the provided data using the provided validation method and returns the result. If a validation fails the result will contain the failed checks.

The prefix parameter specifies the base path to be used for each file check.

Example:

data := []byte("batman.txt 8c555f537cd1fe3f1239ebf7b6d639bc0d576bda\nrobin.txt 4e58c66d2ef2b9a60c7ea2bc03253d8b01874b52")
result, err := CheckIntegrity(data, SHA1Validator{}, "files/")

if err != nil {
	fmt.Printf("Error: %v\n", err)
}

fmt.Printf("Total requested: %d\n", result.TotalRequested)
fmt.Printf("Passed: %d\n", result.Passed)
fmt.Printf("Failed: %d\n", result.Failed)
for _, failedCheck := range result.FailedChecks {
	fmt.Printf("Resource: %s\n", failedCheck.ResourcePath)
	fmt.Printf("Requested: %s\n", failedCheck.RequestedHash)
	fmt.Printf("Detected: %s\n", failedCheck.DetectedHash)
}

type MD5Validator

type MD5Validator struct{}

MD5Validator is an implementation of the validation method using MD5

func (MD5Validator) Hash

func (m MD5Validator) Hash(data io.Reader) (string, error)

type NoErrorHandler

type NoErrorHandler struct{}

NoErrorHandler is a default error handler that does nothing, useful when you don't need to handle errors.

func (*NoErrorHandler) HandleError

func (n *NoErrorHandler) HandleError(args ...interface{}) error

type SHA1Validator

type SHA1Validator struct{}

SHA1Validator is an implementation of the validation method using SHA-1

func (SHA1Validator) Hash

func (s SHA1Validator) Hash(data io.Reader) (string, error)

type SHA256Validator

type SHA256Validator struct{}

SHA256Validator is an implementation of the validation method using SHA-256

func (SHA256Validator) Hash

func (s SHA256Validator) Hash(data io.Reader) (string, error)

type ValidatorType

type ValidatorType interface {
	Hash(data io.Reader) (string, error)
}

ValidatorType defines the protocol for a validation method. Implement this to create custom validation methods for your integrity checks.

Jump to

Keyboard shortcuts

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