utils

package
v0.19.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 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

Jump to

Keyboard shortcuts

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