Documentation
¶
Overview ¶
Package utils provides shared utility functions for the stackit codebase.
Index ¶
- Constants
- Variables
- func BuildEditorCommand(editor, filePath string) (*exec.Cmd, error)
- func CleanCommitMessage(message string) string
- func GenerateBranchNameFromMessage(message string) string
- func IsDemoMode() bool
- func IsInteractive() bool
- func IsTTY() bool
- func NonInteractiveEnv() bool
- func OpenBrowser(url string) error
- func ProcessBranchNamePattern(pattern string, username, date, message string) string
- func ReadFromStdin() (string, error)
- func Run[T any](items []T, worker func(item T))
- func RunWithWorkers[T any](items []T, numWorkers int, worker func(item T))
- func SanitizeBranchName(name string) string
- func SetInteractive(interactive bool)
- func ShortRevision(rev string, maxLen int) string
- func TerminalDetected() bool
- func ValidateBranchName(name string) error
- type BranchNameError
- type LazyHeaders
Constants ¶
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 ¶
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
BuildEditorCommand parses an editor command and appends the target file path.
func CleanCommitMessage ¶
CleanCommitMessage removes comments and trailing whitespace from a commit message
func GenerateBranchNameFromMessage ¶
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 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 ¶
OpenBrowser opens a URL in the default browser on Linux
func ProcessBranchNamePattern ¶
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 ¶
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 ¶
RunWithWorkers runs the given worker function for each item in the slice in parallel with a specified number of workers.
func SanitizeBranchName ¶
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 ¶
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 ¶
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 ¶
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.