Documentation
¶
Overview ¶
Package stringutil provides utility functions for working with strings.
Index ¶
- func ExtractDomainFromURL(urlStr string) string
- func GetPATTypeDescription(token string) string
- func IsAgenticWorkflow(path string) bool
- func IsClassicPAT(token string) bool
- func IsFineGrainedPAT(token string) bool
- func IsLockFile(path string) bool
- func IsOAuthToken(token string) bool
- func IsPositiveInteger(s string) bool
- func LockFileToMarkdown(lockPath string) string
- func MarkdownToLockFile(mdPath string) string
- func NormalizePath(path string) string
- func NormalizeSafeOutputIdentifier(identifier string) string
- func NormalizeWhitespace(content string) string
- func NormalizeWorkflowName(name string) string
- func ParseVersionValue(version any) string
- func SanitizeErrorMessage(message string) string
- func SanitizeParameterName(name string) string
- func SanitizePythonVariableName(name string) string
- func SanitizeToolID(toolID string) string
- func StripANSIEscapeCodes(s string) string
- func Truncate(s string, maxLen int) string
- func ValidateCopilotPAT(token string) error
- type PATType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractDomainFromURL ¶
ExtractDomainFromURL extracts the domain name from a URL string. Handles various URL formats including full URLs with protocols, URLs with ports, and plain domain names.
This function uses net/url.Parse for proper URL parsing when a protocol is present, and falls back to string manipulation for other formats.
Examples:
ExtractDomainFromURL("https://mcp.tavily.com/mcp/") // returns "mcp.tavily.com"
ExtractDomainFromURL("http://api.example.com:8080/path") // returns "api.example.com"
ExtractDomainFromURL("mcp.example.com") // returns "mcp.example.com"
ExtractDomainFromURL("github.com:443") // returns "github.com"
ExtractDomainFromURL("http://sub.domain.com:8080/path") // returns "sub.domain.com"
ExtractDomainFromURL("localhost:8080") // returns "localhost"
func GetPATTypeDescription ¶ added in v0.45.1
GetPATTypeDescription returns a human-readable description of the PAT type
func IsAgenticWorkflow ¶
IsAgenticWorkflow returns true if the file path is an agentic workflow file. Agentic workflows end with .md.
Examples:
IsAgenticWorkflow("test.md") // returns true
IsAgenticWorkflow("weekly-research.md") // returns true
IsAgenticWorkflow(".github/workflows/workflow.md") // returns true
IsAgenticWorkflow("test.lock.yml") // returns false
func IsClassicPAT ¶ added in v0.45.1
IsClassicPAT returns true if the token is a classic personal access token
func IsFineGrainedPAT ¶ added in v0.45.1
IsFineGrainedPAT returns true if the token is a fine-grained personal access token
func IsLockFile ¶
IsLockFile returns true if the file path is a compiled lock file. Lock files end with .lock.yml and are compiled from agentic workflows.
Examples:
IsLockFile("test.lock.yml") // returns true
IsLockFile(".github/workflows/workflow.lock.yml") // returns true
IsLockFile("test.md") // returns false
func IsOAuthToken ¶ added in v0.45.1
IsOAuthToken returns true if the token is an OAuth token (not a PAT)
func IsPositiveInteger ¶
IsPositiveInteger checks if a string is a positive integer. Returns true for strings like "1", "123", "999" but false for:
- Zero ("0")
- Negative numbers ("-5")
- Numbers with leading zeros ("007")
- Floating point numbers ("3.14")
- Non-numeric strings ("abc")
- Empty strings ("")
func LockFileToMarkdown ¶
LockFileToMarkdown converts a compiled lock file path back to its markdown source path. This is used when navigating from compiled workflows back to source files.
The function removes the .lock.yml extension and adds .md extension. If the input already has a .md extension, it returns the path unchanged.
Examples:
LockFileToMarkdown("weekly-research.lock.yml") // returns "weekly-research.md"
LockFileToMarkdown(".github/workflows/test.lock.yml") // returns ".github/workflows/test.md"
LockFileToMarkdown("workflow.md") // returns "workflow.md" (unchanged)
LockFileToMarkdown("my.workflow.lock.yml") // returns "my.workflow.md"
func MarkdownToLockFile ¶
MarkdownToLockFile converts a workflow markdown file path to its compiled lock file path. This is the standard transformation for agentic workflow files.
The function removes the .md extension and adds .lock.yml extension. If the input already has a .lock.yml extension, it returns the path unchanged.
Examples:
MarkdownToLockFile("weekly-research.md") // returns "weekly-research.lock.yml"
MarkdownToLockFile(".github/workflows/test.md") // returns ".github/workflows/test.lock.yml"
MarkdownToLockFile("workflow.lock.yml") // returns "workflow.lock.yml" (unchanged)
MarkdownToLockFile("my.workflow.md") // returns "my.workflow.lock.yml"
func NormalizePath ¶
NormalizePath normalizes a file path by resolving . and .. components. It splits the path on "/" and processes each component: - Empty parts and "." are skipped - ".." moves up one directory (if possible) - Other parts are added to the result
This is useful for resolving relative paths in bundler operations and other file path manipulations where . and .. components need to be resolved.
Examples:
NormalizePath("a/b/../c") // returns "a/c"
NormalizePath("./a/./b") // returns "a/b"
NormalizePath("a/b/../../c") // returns "c"
NormalizePath("../a/b") // returns "a/b" (leading .. is ignored)
NormalizePath("a//b") // returns "a/b" (empty parts removed)
func NormalizeSafeOutputIdentifier ¶
NormalizeSafeOutputIdentifier converts dashes to underscores for safe output identifiers. This standardizes identifier format from the user-facing dash-separated format to the internal underscore-separated format used in safe outputs configuration.
Both dash-separated and underscore-separated formats are valid inputs. This function simply standardizes to the internal representation.
This function performs normalization only - it assumes the input is already a valid identifier and does NOT perform character validation or sanitization.
Examples:
NormalizeSafeOutputIdentifier("create-issue") // returns "create_issue"
NormalizeSafeOutputIdentifier("create_issue") // returns "create_issue" (unchanged)
NormalizeSafeOutputIdentifier("add-comment") // returns "add_comment"
NormalizeSafeOutputIdentifier("update-pr") // returns "update_pr"
func NormalizeWhitespace ¶
NormalizeWhitespace normalizes trailing whitespace and newlines to reduce spurious conflicts. It trims trailing whitespace from each line and ensures exactly one trailing newline.
func NormalizeWorkflowName ¶
NormalizeWorkflowName removes .md and .lock.yml extensions from workflow names. This is used to standardize workflow identifiers regardless of the file format.
The function checks for extensions in order of specificity: 1. Removes .lock.yml extension (the compiled workflow format) 2. Removes .md extension (the markdown source format) 3. Returns the name unchanged if no recognized extension is found
This function performs normalization only - it assumes the input is already a valid identifier and does NOT perform character validation or sanitization.
Examples:
NormalizeWorkflowName("weekly-research") // returns "weekly-research"
NormalizeWorkflowName("weekly-research.md") // returns "weekly-research"
NormalizeWorkflowName("weekly-research.lock.yml") // returns "weekly-research"
NormalizeWorkflowName("my.workflow.md") // returns "my.workflow"
func ParseVersionValue ¶
ParseVersionValue converts version values of various types to strings. Supports string, int, int64, uint64, and float64 types. Returns empty string for unsupported types.
func SanitizeErrorMessage ¶
SanitizeErrorMessage removes potential secret key names from error messages to prevent information disclosure via logs. This prevents exposing details about an organization's security infrastructure by redacting secret key names that might appear in error messages.
func SanitizeParameterName ¶
SanitizeParameterName converts a parameter name to a safe JavaScript identifier by replacing non-alphanumeric characters with underscores.
This function ensures that parameter names from workflows can be used safely in JavaScript code by: 1. Replacing any non-alphanumeric characters (except $ and _) with underscores 2. Prepending an underscore if the name starts with a number
Valid characters: a-z, A-Z, 0-9 (not at start), _, $
Examples:
SanitizeParameterName("my-param") // returns "my_param"
SanitizeParameterName("my.param") // returns "my_param"
SanitizeParameterName("123param") // returns "_123param"
SanitizeParameterName("valid_name") // returns "valid_name"
SanitizeParameterName("$special") // returns "$special"
func SanitizePythonVariableName ¶
SanitizePythonVariableName converts a parameter name to a valid Python identifier by replacing non-alphanumeric characters with underscores.
This function ensures that parameter names from workflows can be used safely in Python code by: 1. Replacing any non-alphanumeric characters (except _) with underscores 2. Prepending an underscore if the name starts with a number
Valid characters: a-z, A-Z, 0-9 (not at start), _ Note: Python does not allow $ in identifiers (unlike JavaScript)
Examples:
SanitizePythonVariableName("my-param") // returns "my_param"
SanitizePythonVariableName("my.param") // returns "my_param"
SanitizePythonVariableName("123param") // returns "_123param"
SanitizePythonVariableName("valid_name") // returns "valid_name"
func SanitizeToolID ¶
SanitizeToolID removes common MCP prefixes and suffixes from tool IDs. This cleans up tool identifiers by removing redundant MCP-related naming patterns.
The function: 1. Removes "mcp-" prefix 2. Removes "-mcp" suffix 3. Returns the original ID if the result would be empty
Examples:
SanitizeToolID("notion-mcp") // returns "notion"
SanitizeToolID("mcp-notion") // returns "notion"
SanitizeToolID("some-mcp-server") // returns "some-server"
SanitizeToolID("github") // returns "github" (unchanged)
SanitizeToolID("mcp") // returns "mcp" (prevents empty result)
func StripANSIEscapeCodes ¶
StripANSIEscapeCodes removes ANSI escape sequences from a string. This prevents terminal color codes and other control sequences from being accidentally included in generated files (e.g., YAML workflows).
Common ANSI escape sequences that are removed:
- Color codes: \x1b[31m (red), \x1b[0m (reset)
- Text formatting: \x1b[1m (bold), \x1b[4m (underline)
- Cursor control: \x1b[2J (clear screen)
Example:
input := "Hello \x1b[31mWorld\x1b[0m" // "Hello [red]World[reset]" output := StripANSIEscapeCodes(input) // "Hello World"
This function is particularly important for:
- Workflow descriptions copied from terminal output
- Comments in generated YAML files
- Any text that should be plain ASCII
func Truncate ¶
Truncate truncates a string to a maximum length, adding "..." if truncated. If maxLen is 3 or less, the string is truncated without "...".
This is a general-purpose utility for truncating any string to a configurable length. For domain-specific workflow command identifiers with newline handling, see workflow.ShortenCommand instead.
func ValidateCopilotPAT ¶ added in v0.45.1
ValidateCopilotPAT validates that a token is a valid fine-grained PAT for Copilot. Returns an error if the token is not a fine-grained PAT with a descriptive error message.
Parameters:
- token: The token string to validate
Returns:
- error: An error with a descriptive message if the token is not valid, nil otherwise
Types ¶
type PATType ¶ added in v0.45.1
type PATType string
PATType represents the type of a GitHub Personal Access Token
const ( // PATTypeFineGrained is a fine-grained personal access token (starts with "github_pat_") PATTypeFineGrained PATType = "fine-grained" // PATTypeClassic is a classic personal access token (starts with "ghp_") PATTypeClassic PATType = "classic" // PATTypeOAuth is an OAuth token (starts with "gho_") PATTypeOAuth PATType = "oauth" // PATTypeUnknown is an unknown token type PATTypeUnknown PATType = "unknown" )
func ClassifyPAT ¶ added in v0.45.1
ClassifyPAT determines the type of a GitHub Personal Access Token based on its prefix.
Token prefixes:
- "github_pat_" = Fine-grained PAT (required for Copilot)
- "ghp_" = Classic PAT (not supported for Copilot)
- "gho_" = OAuth token (not a PAT at all)
Parameters:
- token: The token string to classify
Returns:
- PATType: The type of the token
func (PATType) IsFineGrained ¶ added in v0.45.1
IsFineGrained returns true if the token is a fine-grained PAT