util

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 15 Imported by: 0

README

Util Package

The util package provides a collection of utility functions and types for common operations in the ClusterCockpit library.

Overview

This package contains utilities for:

  • Array operations - Generic helper functions for slices
  • File compression - Gzip compression and decompression
  • File/directory operations - Copying files and directories
  • Disk usage - Calculating directory size
  • Custom types - Float type with JSON NaN support, Selector types
  • File system watcher - Event-based file system monitoring
  • Statistics - Basic statistical functions (mean, median, min, max)

Key Features

Float Type with NaN Support

Go's standard JSON encoder doesn't support NaN values (see golang/go#3480). This package provides a Float type that properly handles NaN values in JSON by converting them to/from null.

import "github.com/ClusterCockpit/cc-lib/util"

// Create a Float value
f := util.Float(3.14)

// Use NaN to represent missing data
missing := util.NaN

// JSON marshaling - NaN becomes null
data, _ := json.Marshal(missing) // Returns: null
File Operations
// Compress a file
err := util.CompressFile("input.txt", "output.txt.gz")

// Decompress a file
err = util.UncompressFile("input.txt.gz", "output.txt")

// Copy a file
err = util.CopyFile("source.txt", "destination.txt")

// Copy a directory recursively
err = util.CopyDir("/path/to/source", "/path/to/dest")
Disk Usage
// Get disk usage in megabytes for a directory
usage := util.DiskUsage("/path/to/directory")
fmt.Printf("Directory uses %.2f MB\n", usage)
Array Utilities
// Check if a slice contains an element (works with any comparable type)
numbers := []int{1, 2, 3, 4, 5}
contains := util.Contains(numbers, 3) // true

strs := []string{"apple", "banana", "orange"}
contains = util.Contains(strs, "grape") // false
Statistics
data := []float64{1.0, 2.0, 3.0, 4.0, 5.0}

// Calculate mean
mean, err := util.Mean(data) // 3.0

// Calculate median
median, err := util.Median(data) // 3.0

// Min/Max (works with any ordered type)
minVal := util.Min(5, 3) // 3
maxVal := util.Max(5, 3) // 5
File System Watcher
// Implement the Listener interface
type MyListener struct{}

func (l *MyListener) EventCallback() {
    fmt.Println("File changed!")
}

func (l *MyListener) EventMatch(event string) bool {
    return strings.Contains(event, "myfile.txt")
}

// Add a listener
listener := &MyListener{}
util.AddListener("/path/to/watch", listener)

// Don't forget to shutdown when done
defer util.FsWatcherShutdown()
Selector Types

The SelectorElement and Selector types support flexible JSON marshaling for configuration:

// Can be a single string
var sel util.SelectorElement
json.Unmarshal([]byte(`"value"`), &sel)

// Can be an array of strings
json.Unmarshal([]byte(`["val1", "val2"]`), &sel)

// Can be a wildcard
json.Unmarshal([]byte(`"*"`), &sel)

Documentation

For complete API documentation, see the godoc.

Testing

The package includes comprehensive unit tests. Run them with:

go test ./util/...

For coverage information:

go test -cover ./util/...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddListener added in v0.3.0

func AddListener(path string, l Listener)

AddListener registers a new file system watcher for the specified path. The watcher is initialized on the first call to AddListener. The listener will be notified of file system events matching its EventMatch criteria.

func CheckFileExists

func CheckFileExists(filePath string) bool

CheckFileExists checks if a file or directory exists at the given path. Returns true if the file exists, false otherwise.

func CompressFile

func CompressFile(fileIn string, fileOut string) error

CompressFile compresses a file using gzip compression. It reads the input file (fileIn), creates a gzip-compressed output file (fileOut), and removes the original file upon successful compression. Returns an error if any operation fails.

func Contains

func Contains[T comparable](items []T, item T) bool

Contains checks if an item exists in a slice of comparable items. It is a generic wrapper around slices.Contains that works with any comparable type T.

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir recursively copies a directory from src to dst, preserving file modes. It creates the destination directory with the same permissions as the source, and recursively copies all files and subdirectories. Symbolic links are skipped during the copy operation. Returns an error if the source is not a directory, if the destination already exists, or if any file operation fails.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies a file from src to dst, preserving the file mode. It creates the destination file, copies the content, syncs to disk, and sets the same permissions as the source file. Returns an error if any operation fails.

func DiskUsage

func DiskUsage(dirpath string) float64

DiskUsage calculates the total disk usage of a directory in megabytes (MB). It sums up the sizes of all files in the specified directory (non-recursive) and returns the result in MB (multiplied by 1e-6). Returns 0 if the directory cannot be opened or read.

func FsWatcherShutdown added in v0.3.0

func FsWatcherShutdown()

FsWatcherShutdown closes the file system watcher. This should be called during application shutdown to clean up resources.

func GetFilecount

func GetFilecount(path string) int

GetFilecount returns the number of entries (files and directories) in a directory. Returns 0 if the directory cannot be read.

func GetFilesize

func GetFilesize(filePath string) int64

GetFilesize returns the size of a file in bytes. Returns 0 if the file cannot be accessed or does not exist.

func Max

func Max[T cmp.Ordered](a, b T) T

Max returns the maximum of two values of any ordered type.

func Mean

func Mean(input []float64) (float64, error)

Mean calculates the arithmetic mean (average) of a float64 slice. Returns NaN and an error if the input slice is empty.

func Median

func Median(input []float64) (median float64, err error)

Median calculates the median value of a float64 slice. For even-length slices, it returns the mean of the two middle values. For odd-length slices, it returns the middle value. Returns NaN and an error if the input slice is empty.

func Min

func Min[T cmp.Ordered](a, b T) T

Min returns the minimum of two values of any ordered type.

func UncompressFile

func UncompressFile(fileIn string, fileOut string) error

UncompressFile decompresses a gzip-compressed file. It reads the gzip-compressed input file (fileIn), creates an uncompressed output file (fileOut), and removes the compressed file upon successful decompression. Returns an error if any operation fails.

Types

type Float added in v0.5.0

type Float float64

Float is a custom float64 type that properly handles NaN values in JSON. Go's JSON encoder for floats does not support NaN (https://github.com/golang/go/issues/3480). This program uses NaN as a signal for missing data. For the HTTP JSON API to be able to handle NaN values, Float implements encoding/json.Marshaler and encoding/json.Unmarshaler, converting NaN to/from JSON null.

var (
	// NaN is a Float constant representing Not-a-Number, used to signal missing data.
	NaN Float = Float(math.NaN())
)

func ConvertToFloat added in v0.5.0

func ConvertToFloat(input float64) Float

ConvertToFloat converts a float64 to a Float. It treats -1.0 as a special value and converts it to NaN.

func (Float) Double added in v0.5.0

func (f Float) Double() float64

Double converts a Float to a native float64 value.

func (Float) IsNaN added in v0.5.0

func (f Float) IsNaN() bool

IsNaN returns true if this Float value represents NaN (Not-a-Number).

func (Float) MarshalJSON added in v0.5.0

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It converts NaN values to JSON null, and normal float values to JSON numbers with 3 decimal places.

func (*Float) UnmarshalJSON added in v0.5.0

func (f *Float) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler. It converts JSON null to NaN, and normal JSON numbers to Float values.

type FloatArray added in v0.5.0

type FloatArray []Float

FloatArray is a slice of Float values with optimized JSON marshaling. It can be marshaled to JSON with fewer allocations than []Float.

func (FloatArray) MarshalJSON added in v0.5.0

func (fa FloatArray) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for FloatArray. It efficiently marshals a slice of Float values, converting NaN to null.

type Listener added in v0.3.0

type Listener interface {
	EventCallback()
	EventMatch(event string) bool
}

Listener is an interface for file system event callbacks. Implementations should define what events to match and how to respond to them.

type Selector added in v0.5.0

type Selector []SelectorElement

Selector is a slice of SelectorElements used for matching and filtering.

type SelectorElement added in v0.5.0

type SelectorElement struct {
	String string
	Group  []string
	Any    bool
}

SelectorElement represents a selector that can be either a single string, an array of strings, or a wildcard. It supports special JSON marshaling/unmarshaling behavior: - A single string (e.g., "value") is stored in the String field - An array of strings (e.g., ["val1", "val2"]) is stored in the Group field - The wildcard "*" is represented by setting Any to true

func (*SelectorElement) MarshalJSON added in v0.5.0

func (se *SelectorElement) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for SelectorElement. It converts the selector back to JSON: - Any=true becomes "*" - String field becomes a JSON string - Group field becomes a JSON array

func (*SelectorElement) UnmarshalJSON added in v0.5.0

func (se *SelectorElement) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler for SelectorElement. It handles three formats: - A JSON string (converted to String field, or Any if "*") - A JSON array (converted to Group field) - Any other format returns an error

Jump to

Keyboard shortcuts

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