util

package
v1.21.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package util provides utility functions for Watchtower operations. It includes tools for slice and map manipulation, random name generation, and SHA-256 hashing.

Key components:

  • SliceEqual: Compares string slices for equality.
  • SliceSubtract: Removes elements from string slices.
  • StringMapSubtract: Removes matching key-value pairs from string maps.
  • StructMapSubtract: Removes matching keys from struct maps.
  • MinInt: Returns the smaller of two integers.
  • RandName: Generates random 32-character container names.
  • GenerateRandomSHA256: Creates random 64-character SHA-256 hashes.

Usage example:

equal := util.SliceEqual(slice1, slice2)
name := util.RandName()
hash := util.GenerateRandomPrefixedSHA256()

The package uses crypto/rand for secure random generation and integrates with container and actions packages for configuration and naming tasks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterEmpty

func FilterEmpty(parts []string) []string

FilterEmpty removes empty strings from a slice, returning only non-empty elements.

It ensures the final formatted duration string excludes unnecessary parts, maintaining readability by filtering out zero-value units that were not explicitly included.

Parameters:

  • parts: A slice of strings representing formatted time units (e.g., "1 hour", "").

Returns:

  • []string: A new slice containing only the non-empty strings from the input.

func FormatDuration

func FormatDuration(duration time.Duration) string

FormatDuration converts a time.Duration into a human-readable string representation.

It breaks down the duration into months, weeks, days, hours, minutes, and seconds, formatting each unit with appropriate grammar (singular or plural) and returning a string like "1 week, 2 days, 3 hours" or "0 seconds" if the duration is zero.

Parameters:

  • duration: The time.Duration to convert into a readable string.

Returns:

  • string: A formatted string representing the duration, always including at least "0 seconds".

func FormatTimeUnit

func FormatTimeUnit(value int64, singular, plural string, forceInclude bool) string

FormatTimeUnit formats a single time unit into a string based on its value and context.

It applies singular or plural grammar, skipping leading zeros unless forced (e.g., for seconds as the last unit), returning an empty string for skippable zeros to maintain a concise output.

Parameters:

  • value: The numeric value of the unit (e.g., 2 for 2 hours).
  • singular: The singular form of the unit (e.g., "hour").
  • plural: The plural form of the unit (e.g., "hours").
  • forceInclude: A boolean indicating whether to include the unit even if zero (e.g., for seconds as fallback).

Returns:

  • string: The formatted unit (e.g., "1 hour", "2 minutes") or empty string if skipped.

func GenerateRandomPrefixedSHA256

func GenerateRandomPrefixedSHA256() string

GenerateRandomPrefixedSHA256 generates a prefixed SHA-256 hash.

Returns:

  • string: Random hash with "sha256:" prefix.

func GenerateRandomSHA256

func GenerateRandomSHA256() string

GenerateRandomSHA256 generates a 64-character SHA-256 hash.

Returns:

  • string: Random hash without prefix.

func MinInt

func MinInt(a, b int) int

MinInt returns the smaller of two integers.

Parameters:

  • a: First integer.
  • b: Second integer.

Returns:

  • int: The smaller of the two integers.

func NormalizeContainerName

func NormalizeContainerName(name string) string

NormalizeContainerName trims the leading "/" from container names.

Parameters:

  • name: Container name, potentially with leading "/".

Returns:

  • string: Normalized name without leading "/".

func ParseDuration

func ParseDuration(duration string) (time.Duration, error)

ParseDuration parses a duration string with extended unit support.

It supports all standard Go duration units (h, m, s, ms, us, ns) plus:

  • d: days (24 hours)
  • w: weeks (7 days)
  • M: months (30 days)

Units can be combined (e.g., "1w2d", "2M3w", "1M15d12h"). An empty string or "0" returns a zero duration without error.

Parameters:

  • s: The duration string to parse.

Returns:

  • time.Duration: The parsed duration.
  • error: Non-nil if the string cannot be parsed.

func SliceEqual

func SliceEqual(slice1, slice2 []string) bool

SliceEqual checks if two string slices are identical.

Parameters:

  • slice1: First slice.
  • slice2: Second slice.

Returns:

  • bool: True if equal, false otherwise.

func SliceSubtract

func SliceSubtract(slice, subtractFrom []string) []string

SliceSubtract returns elements in the first slice that are not in the second slice.

Parameters:

  • slice: Source slice.
  • subtractFrom: Slice containing elements to exclude.

Returns:

  • []string: Slice containing elements unique to the source slice.

func StringMapSubtract

func StringMapSubtract(map1, map2 map[string]string) map[string]string

StringMapSubtract removes matching key-value pairs.

Parameters:

  • map1: Source map.
  • map2: Map to subtract.

Returns:

  • map[string]string: New map with unique or differing entries.

func StructMapSubtract

func StructMapSubtract(map1, map2 map[string]struct{}) map[string]struct{}

StructMapSubtract removes matching keys.

Parameters:

  • map1: Source map.
  • map2: Map to subtract.

Returns:

  • map[string]struct{}: New map with unique keys.

Types

This section is empty.

Jump to

Keyboard shortcuts

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