utils

package
v1.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

utils

Shared utilities for console output, file operations, downloads, and script parsing.

Architecture

console.go   Styled console output, logging, user prompts
download.go  HTTP downloads with progress bars
files.go     File and directory utilities
parser.go    Build script metadata parsing

Console Output

Print Functions: PrintMessage, PrintSuccess, PrintWarning, PrintError, PrintNote, PrintDebug

Style Functions: StyleError (red), StyleSuccess (green), StyleWarning (yellow), StyleHint (cyan), StyleInfo (magenta), StylePath, StyleName, StyleNumber, StyleAction, StyleCommand

Modes: DebugMode, QuietMode, YesMode

utils.PrintMessage("Installing %s", utils.StyleName("package"))
utils.PrintSuccess("Build completed")
utils.PrintDebug("Path: %s", utils.StylePath(path))
yes, err := utils.AskYesNo("Continue?", defaultYes=true)

File Operations

Checks: FileExists, DirExists, IsImg, IsSqf, IsOverlay
Operations: EnsureDir, CopyFile, MoveFile, DeleteFile
Permissions: PermFile (0664), PermDir (0775), PermExec (0775)

utils.FileExists("/path/file")
utils.IsImg("env.img")
utils.EnsureDir("/path/dir")

Downloads

utils.DownloadFile(url, destPath)           // With progress bar
utils.DownloadExecutable(url, destPath)     // Sets exec permissions

Script Parsing

Dependencies:

deps, err := utils.GetDependenciesFromScript(scriptPath)
// Extracts #DEP:name/version and module load commands

Interactive Prompts:

prompts, err := utils.GetInteractivePromptsFromScript(scriptPath)
// Extracts #INTERACTIVE:prompt directives

Scheduler Specs (from internal/scheduler):

specs, err := scheduler.ReadScriptSpecsFromPath(scriptPath)
// Extracts #SBATCH/#PBS/#BSUB directives and resource requirements (HTCondor uses native .sub files)

Documentation

Index

Constants

View Source
const PermDir os.FileMode = 0775

Dir: u=rwx, g=rwx, o=rx (Requires +x to traverse)

View Source
const PermExec os.FileMode = 0775

Exec: u=rwx, g=rwx, o=rx (Executable files with group write access)

View Source
const PermFile os.FileMode = 0664

Standard default permissions File: u=rw, g=rw, o=r

Variables

View Source
var DebugMode = false

DebugMode controls whether PrintDebug output is visible.

View Source
var QuietMode = false

QuietMode controls whether verbose messages are suppressed (errors/warnings still shown)

View Source
var YesMode = false

YesMode controls whether to automatically answer yes to all prompts

Functions

func DirExists

func DirExists(path string) bool

DirExists checks if a path exists and is a directory.

func DownloadExecutable

func DownloadExecutable(url, destPath string) error

DownloadExecutable downloads a file and sets it as executable (PermExec).

func DownloadFile

func DownloadFile(url, destPath string) error

DownloadFile downloads a file from url to destPath.

func EnsureDir

func EnsureDir(path string) error

EnsureDir checks if a directory exists, and creates it if it doesn't.

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists and is not a directory.

func FixPermissions

func FixPermissions(path string, filePerm, dirPerm os.FileMode) error

FixPermissions automatically adjusts permissions for a path using the provided modes. If 'path' is a file, it sets it to 'filePerm'. If 'path' is a directory, it recursively sets files to 'filePerm' and subdirs to 'dirPerm'.

func FixPermissionsDefault

func FixPermissionsDefault(path string) error

FixPermissionsDefault is a helper that applies standard permissions (0664/0775). It is a shorthand for FixPermissions(path, PermFile, PermDir).

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes formats bytes into human-readable format (e.g., "1.50 GB").

func GetDependenciesFromScript

func GetDependenciesFromScript(scriptPath string) ([]string, error)

GetDependenciesFromScript parses a build script and extracts dependencies from #DEP: comments and module load commands. Returns a list of normalized dependency names with duplicates removed.

func GetInteractivePromptsFromScript

func GetInteractivePromptsFromScript(scriptPath string) ([]string, error)

GetInteractivePromptsFromScript parses a build script and extracts interactive prompt lines beginning with "#INTERACTIVE:". Returns a list of prompt strings (without the prefix) or an error if the file cannot be read.

func GetWhatIsFromScript added in v1.2.0

func GetWhatIsFromScript(scriptPath string) string

GetWhatIsFromScript reads a script and extracts the first #WHATIS: line. Returns the trimmed description string, or empty string if not found.

func IsApptainerImage

func IsApptainerImage(path string) bool

IsApptainerImage checks if the file matches any supported Apptainer image format. Returns true for .sif, .img, .sqf, .sqsh, .squashfs.

func IsImg

func IsImg(path string) bool

IsImg checks if the path has an ext3 overlay extension (.img). Note: In Apptainer context, .img usually implies a writable ext3 overlay.

func IsInteractiveShell

func IsInteractiveShell() bool

IsInteractiveShell checks if stdout is connected to a TTY (interactive terminal). Returns true if the program is running in an interactive shell, false otherwise.

func IsOverlay

func IsOverlay(path string) bool

IsOverlay checks if the path is an overlay file (.img, .sqf, .sqsh, .squashfs). This is used for CondaTainer overlay detection.

func IsSif

func IsSif(path string) bool

IsSif checks if the path has a Singularity Image Format extension (.sif). This is the native format for Apptainer/Singularity.

func IsSqf

func IsSqf(path string) bool

IsSqf checks if the path has a SquashFS extension (.sqf, .sqsh, .squashfs). These are read-only compressed images.

func IsYaml

func IsYaml(path string) bool

IsYaml checks if the path has a YAML extension (.yaml, .yml). Useful for Conda environment definition files.

func NormalizeNameVersion

func NormalizeNameVersion(nameVersion string) string

NormalizeNameVersion normalizes package spec formats so that "name/version", "name=version", "name@version" are treated the same. Converts = and @ to /, and -- to /, then strips whitespace.

func ParseDuration

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

ParseDuration parses a duration string supporting multiple formats:

  • Go duration: "2h", "30m", "1h30m", "90s"
  • HH:MM:SS format: "02:00:00", "2:30:00", "00:30:00"
  • H:MM format: "2:30" (interpreted as hours:minutes)

Returns the duration in time.Duration format.

func ParseSizeToMB

func ParseSizeToMB(sizeStr string) (int, error)

ParseSizeToMB converts strings like "10G", "500M", "1024" into Megabytes (int). Default unit is MB if no suffix is provided.

func PrintDebug

func PrintDebug(format string, a ...interface{})

PrintDebug prints a debug message with a Gray tag (only if DebugMode is true). Output: [CNT] [DBG] Executing: rm -rf /tmp/foo

func PrintError

func PrintError(format string, a ...interface{})

PrintError prints an error message with a Red tag to Stderr. Output: [CNT][ERR] Something failed.

func PrintHint

func PrintHint(format string, a ...interface{})

PrintHint prints a helpful hint with a Cyan tag. Output: [CNT][HINT] Try running with --force.

func PrintMessage

func PrintMessage(format string, a ...interface{})

PrintMessage prints a standard info message. Output: [CNT] Message...

func PrintNote

func PrintNote(format string, a ...interface{})

PrintNote prints a note with a Magenta tag. Output: [CNT][NOTE] This might take a while.

func PrintSuccess

func PrintSuccess(format string, a ...interface{})

PrintSuccess prints a success message with a Green tag. Output: [CNT][SUCCESS] Operation complete.

func PrintWarning

func PrintWarning(format string, a ...interface{})

PrintWarning prints a warning with a Yellow tag to Stderr. Output: [CNT][WARN] Disk is almost full.

func ShareToUGORecursive

func ShareToUGORecursive(path string) error

ShareToUGORecursive recursively sets permissions to share files with user, group, and others. Files: ug+rw,o+r (0664) Directories: ug+rwx,o+rx (0775) This matches the Python implementation's share_to_ugo_recursive function.

func ShouldAnswerYes

func ShouldAnswerYes() bool

ShouldAnswerYes checks if we should automatically answer yes to prompts Returns true if --yes flag is set, false otherwise

func StripInlineComment added in v1.2.0

func StripInlineComment(s string) string

StripInlineComment removes everything after the first '#' character (inline comment). Returns the trimmed string without the comment.

func StyleAction

func StyleAction(act string) string

StyleAction formats verbs or active operations (Yellow).

func StyleCommand

func StyleCommand(cmd string) string

StyleCommand formats shell commands or flags (Gray/Faint).

func StyleDebug

func StyleDebug(msg string) string

StyleDebug formats low-level technical info (Gray).

func StyleError

func StyleError(msg string) string

StyleError formats critical failure messages (Red).

func StyleHighlight

func StyleHighlight(text string) string

StyleHighlight formats search matches or highlighted text (Yellow Bold).

func StyleHint

func StyleHint(msg string) string

StyleHint formats helpful tips or suggestions (Cyan).

func StyleInfo

func StyleInfo(msg string) string

StyleInfo formats status labels or properties (Magenta)

func StyleName

func StyleName(name string) string

StyleName formats names, identifiers, or keys (Yellow).

func StyleNote

func StyleNote(msg string) string

StyleNote formats neutral notes or annotations (Magenta).

func StyleNumber

func StyleNumber(num interface{}) string

StyleNumber formats counts, sizes, or IDs (Magenta).

func StylePath

func StylePath(path string) string

StylePath formats file paths with context-aware coloring.

func StyleSuccess

func StyleSuccess(msg string) string

StyleSuccess formats success messages (Green).

func StyleTitle

func StyleTitle(title string) string

StyleTitle

func StyleWarning

func StyleWarning(msg string) string

StyleWarning formats non-critical warnings (Yellow).

func URLExists

func URLExists(url string) bool

URLExists checks if a URL exists (returns HTTP 200) using a HEAD request.

Types

This section is empty.

Jump to

Keyboard shortcuts

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