gate

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCommentStyles = map[string]CommentStyle{
	".go":    {LinePrefix: "//"},
	".js":    {LinePrefix: "//"},
	".ts":    {LinePrefix: "//"},
	".tsx":   {LinePrefix: "//"},
	".jsx":   {LinePrefix: "//"},
	".java":  {LinePrefix: "//"},
	".c":     {LinePrefix: "//"},
	".h":     {LinePrefix: "//"},
	".cpp":   {LinePrefix: "//"},
	".rs":    {LinePrefix: "//"},
	".swift": {LinePrefix: "//"},
	".cs":    {LinePrefix: "//"},
	".zig":   {LinePrefix: "//"},
	".py":    {LinePrefix: "#"},
	".sh":    {LinePrefix: "#"},
	".rb":    {LinePrefix: "#"},
	".yml":   {LinePrefix: "#"},
	".yaml":  {LinePrefix: "#"},
	".toml":  {LinePrefix: "#"},
	".env":   {LinePrefix: "#"},
	".sql":   {LinePrefix: "--"},
	".html":  {LinePrefix: "<!--", BlockEnd: "-->", Space: true},
	".md":    {LinePrefix: "<!--", BlockEnd: "-->", Space: true},
	".xml":   {LinePrefix: "<!--", BlockEnd: "-->", Space: true},
}

Global registry for comment styles overrideable at runtime.

Functions

func BuildGatedBody

func BuildGatedBody(content string, opts ...Option) string

BuildGatedBody returns a gated body snippet (unnamed section) with start/end markers surrounding the provided content. Newlines are normalized so the snippet always ends with a trailing newline. The content is placed exactly between markers. Example (markdown): <!-- CANARY:START -->\n<content>\n<!-- CANARY:END -->\n

func BuildGatedBodyKey

func BuildGatedBodyKey(key, content string, opts ...Option) string

BuildGatedBodyKey returns a gated body snippet for a keyed section. Example (markdown): <!-- CANARY:mykey:START -->\n<content>\n<!-- CANARY:mykey:END -->\n

func EndMarker

func EndMarker(section string, opts ...Option) string

EndMarker returns the end marker string for a keyed or unnamed section given options.

func RegisterCommentStyle

func RegisterCommentStyle(ext string, style CommentStyle)

RegisterCommentStyle allows external code to override or add a comment style for extension.

func RemoveSection

func RemoveSection(filePath, key string, opts ...Option) error

RemoveSection removes a keyed section.

func StartMarker

func StartMarker(section string, opts ...Option) string

StartMarker returns the start marker string for a keyed or unnamed section given options.

func UpdateMultiple

func UpdateMultiple(filePath string, sections map[string]string, opts ...Option) error

UpdateMultiple updates or inserts multiple keyed sections.

func UpdateSection added in v0.3.3

func UpdateSection(filePath, key, sectionContent string, opts ...Option) error

UpdateSection updates or inserts the gated section named key in filePath.

key must be non-empty. An unkeyed section cannot be created any more: two tools writing unkeyed sections into the same file have no way to tell their sections apart, which is how a file ends up with duplicates. An unkeyed section that already exists is still adopted -- its body is replaced in place and its markers left as they are -- so files written by older versions keep updating rather than growing a second section beside them.

Types

type CommentStyle

type CommentStyle struct {
	// LinePrefix denotes a single-line comment marker (e.g. "//", "#", "--").
	LinePrefix string
	// BlockStart and BlockEnd denote multi-line comment delimiters (e.g. "/*" and "*/").
	BlockStart string
	BlockEnd   string
	// Optional space insertion between delimiters and content for readability.
	Space bool
}

CommentStyle describes how comments are represented for a language/file type. Either single-line (LinePrefix) or multi-line (BlockStart/BlockEnd) or both may be set.

func DetectStyleFromExtension

func DetectStyleFromExtension(path string) CommentStyle

DetectStyleFromExtension returns a reasonable default style based on file extension.

type Option

type Option func(*Options)

Option functional option.

func WithBlankLineBefore

func WithBlankLineBefore() Option

WithBlankLineBefore enables blank line insertion.

func WithKey

func WithKey(k string) Option

WithKey sets gating key.

func WithStyle

func WithStyle(cs CommentStyle) Option

WithStyle sets the comment style.

func WithTokens

func WithTokens(start, end string) Option

WithTokens sets start/end tokens.

type Options

type Options struct {
	Key        string // The logical gating namespace, e.g. "CANARY"
	Style      CommentStyle
	StartToken string // e.g. "START"
	EndToken   string // e.g. "END"
	// If true, enforce blank line preceding an appended section.
	EnsureBlankBefore bool
}

Options configures gating behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns a baseline configuration targeting HTML-style comments used in markdown.

Jump to

Keyboard shortcuts

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