utils

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package utils provides shared utility functions for the stackit codebase.

Index

Constants

View Source
const (
	// MaxBranchNameByteLength is the maximum length for a branch name
	// Git refs have a max length of 256 bytes, minus 22 for "refs/stackit/metadata/"
	MaxBranchNameByteLength = 234
)

Variables

View Source
var (
	// BranchNameReplaceRegex matches characters that are not valid in branch names
	// Valid characters: letters, numbers, -, _, /, .
	BranchNameReplaceRegex = regexp.MustCompile(`[^-_/.a-zA-Z0-9]+`)

	// BranchNameIgnoreRegex matches trailing slashes and dots that should be removed
	BranchNameIgnoreRegex = regexp.MustCompile(`[/.]*$`)
)

Functions

func BuildEditorCommand added in v0.18.0

func BuildEditorCommand(editor, filePath string) (*exec.Cmd, error)

BuildEditorCommand parses an editor command and appends the target file path.

func CleanCommitMessage

func CleanCommitMessage(message string) string

CleanCommitMessage removes comments and trailing whitespace from a commit message

func GenerateBranchNameFromMessage

func GenerateBranchNameFromMessage(message string) string

GenerateBranchNameFromMessage generates a branch name from a commit message

func IsDemoMode

func IsDemoMode() bool

IsDemoMode returns true if STACKIT_DEMO environment variable is set

func IsInteractive

func IsInteractive() bool

IsInteractive checks if we're in an interactive terminal

func IsTTY

func IsTTY() bool

IsTTY returns true if we can use a TTY for interactive TUI

func NonInteractiveEnv added in v0.19.0

func NonInteractiveEnv() bool

NonInteractiveEnv reports whether STACKIT_NO_INTERACTIVE requests non-interactive mode. Any value other than empty/"0"/"false"/"no" enables it, letting agents and scripts opt out of prompts once instead of passing --no-interactive on every command.

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowser opens a URL in the default browser on Linux

func ProcessBranchNamePattern

func ProcessBranchNamePattern(pattern string, username, date, message string) string

ProcessBranchNamePattern processes a branch name pattern by replacing placeholders Supported placeholders:

  • {username}: The sanitized Git username
  • {date}: Current date and time in yyyyMMddHHmmss format in UTC
  • {message}: The sanitized commit message subject (required)

The pattern must contain {message} placeholder. The pattern is processed and then sanitized to ensure it's a valid branch name.

func ReadFromStdin

func ReadFromStdin() (string, error)

ReadFromStdin reads all content from standard input

func Run

func Run[T any](items []T, worker func(item T))

Run runs the given worker function for each item in the slice in parallel. It uses runtime.GOMAXPROCS(0) as the default number of workers.

func RunWithWorkers

func RunWithWorkers[T any](items []T, numWorkers int, worker func(item T))

RunWithWorkers runs the given worker function for each item in the slice in parallel with a specified number of workers.

func SanitizeBranchName

func SanitizeBranchName(name string) string

SanitizeBranchName sanitizes a branch name by replacing invalid characters

func SetInteractive

func SetInteractive(interactive bool)

SetInteractive sets whether the TUI should be interactive

func ShortRevision

func ShortRevision(rev string, maxLen int) string

ShortRevision returns a shortened version of a git revision hash. Returns at most maxLen characters, defaulting to 7 if maxLen <= 0.

func TerminalDetected added in v0.19.0

func TerminalDetected() bool

TerminalDetected reports whether a usable interactive terminal is attached, independent of the configured interactive mode. It checks the TERM environment, stdin/stdout isatty, and /dev/tty availability. Use this when resolving the default interactivity before the interactive mode is set (e.g. in the root command), where IsTTY's mode gate would not yet apply.

func ValidateBranchName

func ValidateBranchName(name string) error

ValidateBranchName checks if a branch name is valid according to Git rules. Returns an error describing the issue if invalid, nil if valid.

Types

type BranchNameError

type BranchNameError struct {
	Name   string
	Reason string
}

BranchNameError represents an invalid branch name error

func (*BranchNameError) Error

func (e *BranchNameError) Error() string

type LazyHeaders added in v0.19.2

type LazyHeaders[K comparable] struct {
	// contains filtered or unexported fields
}

LazyHeaders decides when a keyed section's header should be emitted to a streaming display: at most once, on the section's first item, and only for sections that were explicitly started. Successive emitted headers are separated from the previous one by a blank line.

It holds no output of its own — callers emit based on its decisions — so the same logic drives both the non-TTY sync handler (which writes lines straight to output) and the bubbletea sync model (which accumulates tea.Cmds). Keeping the rule in one place stops the two from drifting apart.

The zero value is not ready for use; construct with NewLazyHeaders.

func NewLazyHeaders added in v0.19.2

func NewLazyHeaders[K comparable]() *LazyHeaders[K]

NewLazyHeaders returns a ready-to-use tracker keyed by K.

func (*LazyHeaders[K]) Any added in v0.19.2

func (l *LazyHeaders[K]) Any() bool

Any reports whether any header has been emitted, e.g. to decide whether a trailing separator is needed before a summary line.

func (*LazyHeaders[K]) CommitOnItem added in v0.19.2

func (l *LazyHeaders[K]) CommitOnItem(key K) (emit, separate bool)

CommitOnItem is called as a section produces an item. It reports whether the section's header should be emitted now (emit) — true exactly once, for the first item of a started, not-yet-committed section — and whether that header needs a blank-line separator from the previous section (separate). Items for a section that was never started return (false, false), so callers that drive items without starting a phase stay headerless.

func (*LazyHeaders[K]) Committed added in v0.19.2

func (l *LazyHeaders[K]) Committed(key K) bool

Committed reports whether the given section's header has been emitted.

func (*LazyHeaders[K]) Start added in v0.19.2

func (l *LazyHeaders[K]) Start(key K)

Start records that a section has begun. Its header is not emitted yet — that happens lazily on the first item via CommitOnItem — so a section that starts but never produces an item stays silent.

Jump to

Keyboard shortcuts

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