utils

package
v0.14.11 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ZipMagicBytes = []byte{0x50, 0x4B, 0x03, 0x04}

ZipMagicBytes are the first 4 bytes of a ZIP file

Functions

func AddFileToZip

func AddFileToZip(zipData []byte, filename string, content []byte) ([]byte, error)

AddFileToZip adds or updates a file in a zip archive

func CleanupMarkdownSpacing added in v0.9.0

func CleanupMarkdownSpacing(content string) string

CleanupMarkdownSpacing reduces multiple consecutive blank lines to a maximum of two

func CompareZipContents

func CompareZipContents(zipData1, zipData2 []byte) (bool, error)

CompareZipContents compares two zip files by computing and comparing their hashes Excludes metadata.toml from comparison to focus on actual content

func ComputeFileSHA256

func ComputeFileSHA256(filePath string) (string, error)

ComputeFileSHA256 computes the SHA256 hash of a file

func ComputeFileSHA512

func ComputeFileSHA512(filePath string) (string, error)

ComputeFileSHA512 computes the SHA512 hash of a file

func ComputeSHA256

func ComputeSHA256(data []byte) string

ComputeSHA256 computes the SHA256 hash of data

func ComputeSHA512

func ComputeSHA512(data []byte) string

ComputeSHA512 computes the SHA512 hash of data

func ComputeZipHash

func ComputeZipHash(zipData []byte) ([]byte, error)

ComputeZipHash computes an MD5 hash of all files in a zip archive Files are hashed individually, then combined in alphabetical order by filename

func CreateZip

func CreateZip(sourceDir string) ([]byte, error)

CreateZip creates a zip archive from a directory

func CreateZipFromContent added in v0.5.5

func CreateZipFromContent(filename string, content []byte) ([]byte, error)

CreateZipFromContent creates a zip archive containing a single file with the given content

func EnsureDir

func EnsureDir(path string) error

EnsureDir ensures that a directory exists, creating it if necessary

func ExpandTilde

func ExpandTilde(path string) (string, error)

ExpandTilde expands a tilde (~) at the beginning of a path to the user's home directory

func ExtractZip

func ExtractZip(zipData []byte, targetDir string) error

ExtractZip extracts a zip file to a target directory

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists

func GetClaudeDir

func GetClaudeDir() (string, error)

GetClaudeDir returns the path to the .claude directory for asset installation This is where global assets are installed

func GetConfigDir

func GetConfigDir() (string, error)

GetConfigDir returns the path to the sx config directory Uses platform-specific config directories: - Linux: ~/.config/sx (or $XDG_CONFIG_HOME/sx) - macOS: ~/Library/Application Support/sx - Windows: %AppData%/sx

func GetConfigFile

func GetConfigFile() (string, error)

GetConfigFile returns the path to the config.json file

func HasContentFiles added in v0.10.0

func HasContentFiles(zipData []byte) (bool, error)

HasContentFiles returns true if the zip contains files beyond metadata.toml. This is used to distinguish packaged assets (with server code) from config-only assets.

func IsDirectory

func IsDirectory(path string) bool

IsDirectory checks if a path is a directory

func IsZipFile

func IsZipFile(data []byte) bool

IsZipFile checks if data starts with ZIP magic bytes

func ListZipFiles

func ListZipFiles(zipData []byte) ([]string, error)

ListZipFiles returns a list of all files in a zip archive

func NormalizePath

func NormalizePath(path string) (string, error)

NormalizePath normalizes a file path, expanding tilde and cleaning it

func Portabilize added in v0.14.9

func Portabilize(path string) string

Portabilize replaces the user's home directory prefix with $HOME so paths remain valid across different environments.

func ReadZipFile

func ReadZipFile(zipData []byte, filename string) ([]byte, error)

ReadZipFile reads a specific file from a zip archive without extracting

func RemoveFileFromZip

func RemoveFileFromZip(zipData []byte, filename string) ([]byte, error)

RemoveFileFromZip removes a file from a zip archive

func RemoveMarkdownSections added in v0.9.0

func RemoveMarkdownSections(content string, headingsToRemove []string) string

RemoveMarkdownSections removes specified sections from markdown content

func ReplaceFileInZip

func ReplaceFileInZip(zipData []byte, filename string, content []byte) ([]byte, error)

ReplaceFileInZip replaces an existing file in a zip archive This is an alias for AddFileToZip which already handles replacement

func ResolveArgs added in v0.12.4

func ResolveArgs(args []string, installPath string) []string

ResolveArgs resolves args for a packaged MCP server. Each arg is converted to an absolute path only if it corresponds to an actual file or directory within installPath. Subcommands (e.g. "run" for "uv run"), flags, and other non-file args are left as-is.

func ResolveCommand added in v0.12.4

func ResolveCommand(command string, installPath string) string

ResolveCommand resolves a command for a packaged MCP server. Bare command names (e.g. "node", "uv", "python") are left as-is to be resolved via PATH. Relative paths containing a directory separator (e.g. "./bin/server") are made absolute relative to installPath.

func ResolveCommandAndArgs added in v0.12.4

func ResolveCommandAndArgs(command string, args []string, installPath string) (string, []any)

ResolveCommandAndArgs resolves a command and its args for a packaged MCP server, returning them as []any suitable for JSON serialization into client configs.

func Slugify added in v0.9.0

func Slugify(s string) string

Slugify converts a string to a valid asset name. The result contains only lowercase alphanumeric characters and hyphens, matching the asset name validation pattern ^[a-zA-Z0-9_-]+$.

Transformations:

  • Convert to lowercase
  • Replace spaces and underscores with hyphens
  • Remove all other special characters
  • Collapse consecutive hyphens
  • Trim leading/trailing hyphens

func StringsToAny added in v0.12.4

func StringsToAny(s []string) []any

StringsToAny converts a []string to []any.

func URLHash

func URLHash(url string) string

URLHash computes a short hash of a URL for directory naming

func UnmarshalJSONC added in v0.14.4

func UnmarshalJSONC(data []byte, v any) error

UnmarshalJSONC parses JSONC (JSON with Comments and trailing commas) into v. VS Code-based editors (Cursor, Kiro, Cline, etc.) treat JSON config files as JSONC, so user-edited config files may contain comments and trailing commas that Go's encoding/json rejects. This function standardizes the input first. For valid JSON input the standardize step is a no-op.

func VerifyHash

func VerifyHash(data []byte, algorithm, expected string) error

VerifyHash verifies that data matches the expected hash

Types

type MarkdownSection added in v0.9.0

type MarkdownSection struct {
	Heading string // The heading text (without ## prefix)
	Content string // The section content (including any sub-headings)
	Level   int    // Heading level (2 for ##, 3 for ###, etc.)
}

MarkdownSection represents a parsed section from a markdown file

func ParseMarkdownSections added in v0.9.0

func ParseMarkdownSections(content string) []MarkdownSection

ParseMarkdownSections extracts ## sections from markdown content. Returns empty slice for empty content or content without ## headings. Sub-headings (###, ####, etc.) are included in their parent section's content.

Jump to

Keyboard shortcuts

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