toolutil

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CTJSON = "application/json"
	CTCBOR = "application/cbor"
	CTText = "text/plain"
)

Variables

This section is empty.

Functions

func AddAllowFileReadsFlag added in v1.1.0

func AddAllowFileReadsFlag(cmd *cobra.Command, allow *bool)

AddAllowFileReadsFlag provides a CLI flag to allow using {{file:...}} placeholders. Disabled by default for safety; the CLI flag should be used with care in untrusted environments.

func AddDestFlag

func AddDestFlag(cmd *cobra.Command, dest *string, def string, usage string, aliases ...string)

AddDestFlag adds a standardized destination flag (topic/path/subject/channel/table). Supports aliases for backward compatibility.

func AddFileCacheFlag added in v1.1.0

func AddFileCacheFlag(cmd *cobra.Command, cache *bool)

AddFileCacheFlag adds a --cache-files flag for enabling file content caching. AddFileCacheFlag adds a --cache-files flag for enabling file content caching.

func AddFileRootFlag added in v1.1.0

func AddFileRootFlag(cmd *cobra.Command, root *string)

AddFileRootFlag adds a --file-root flag to restrict file: placeholder reads.

func AddHeadersFlag

func AddHeadersFlag(cmd *cobra.Command, headers *[]string)

AddHeadersFlag adds a repeatable flag for metadata/headers in key=value format.

func AddIntervalFlag

func AddIntervalFlag(cmd *cobra.Command, interval *string, def string)

AddIntervalFlag adds a common interval flag for periodic actions.

func AddMethodFlag

func AddMethodFlag(cmd *cobra.Command, method *string, def string, usage string)

AddMethodFlag adds a common HTTP method flag.

func AddOnceFlag added in v1.1.0

func AddOnceFlag(cmd *cobra.Command, once *bool)

AddOnceFlag adds a flag to execute the action once and exit.

func AddPathFlag

func AddPathFlag(cmd *cobra.Command, path *string, def string, usage string)

AddPathFlag adds a common path/resource flag.

func AddPayloadFlags

func AddPayloadFlags(cmd *cobra.Command, payload *string, payloadDef string, mime *string, mimeDef string)

AddPayloadFlags adds payload, mime and testpayload flags.

func AddSeedFlag added in v1.1.0

func AddSeedFlag(cmd *cobra.Command, seed *int64)

AddSeedFlag provides a CLI flag to configure a deterministic seed for test payload generation to make output deterministic during tests or reproducible runs.

func AddServerFlag

func AddServerFlag(cmd *cobra.Command, server *string, def string, aliases ...string)

AddServerFlag adds a standardized server/broker/connection flag. Supports aliases for backward compatibility (e.g., --address, --broker).

func AddTemplateDelimiterFlags

func AddTemplateDelimiterFlags(cmd *cobra.Command, openDelim *string, closeDelim *string)

AddTemplateDelimiterFlags adds flags for customizing template variable delimiters.

func AddTemplateVarFlag added in v1.1.0

func AddTemplateVarFlag(cmd *cobra.Command, vars *[]string)

AddTemplateVarFlag adds a repeatable --template-var name=value flag

func BuildPayload

func BuildPayload(rawPayload string, mime string) ([]byte, string, error)

BuildPayload builds request payload bytes and content-type from either a testpayload type or a raw payload with MIME. Priority: if testType is provided, it's used; otherwise raw payload with MIME is used; returns (nil, "") if neither provided. Uses default template delimiters "{{" and "}}".

func BuildPayloadWithDelimiters

func BuildPayloadWithDelimiters(rawPayload string, mime string, openDelim string, closeDelim string) ([]byte, string, error)

BuildPayloadWithDelimiters builds request payload with custom template delimiters. Supports placeholders: {{json}}, {{cbor}}, {{sentiment}}, {{sentence}}, {{datetime}}, {{nowtime}}, {{counter}}, {{file:/path}}

func EncodeCBORFromJSON

func EncodeCBORFromJSON(jsonStr string) ([]byte, error)

EncodeCBORFromJSON parses a JSON string and encodes it as CBOR bytes.

func GuessMIME

func GuessMIME(body []byte) string

GuessMIME tries to guess a content type from raw body. It detects JSON by leading '{' or '[' and CBOR by first byte 0xA0-0xBF/0x80-0x9F/0x60-0x7F heuristics. Falls back to text/plain.

func Logger

func Logger() *slog.Logger

Logger returns a slog logger to stdout.

func ParseHeaders

func ParseHeaders(headers []string) (map[string]string, error)

ParseHeaders parses a slice of "key=value" strings into a map. Returns an error if any header is malformed. Uses default template delimiters "{{" and "}}".

func ParseHeadersWithDelimiters

func ParseHeadersWithDelimiters(headers []string, openDelim string, closeDelim string) (map[string]string, error)

ParseHeadersWithDelimiters parses headers with template interpolation using custom delimiters. Header values support template variables like {{nowtime}}, {{counter}}, {{file:/path}}, etc.

func ParseTemplateVars added in v1.1.0

func ParseTemplateVars(vars []string) (map[string]string, error)

ParseTemplateVars converts a slice of "name=value" into a map.

func PrettyBodyByMIME

func PrettyBodyByMIME(mime string, body []byte) []byte

PrettyBodyByMIME pretty-prints JSON/CBOR bodies based on MIME, otherwise returns original body.

func PrintColoredMessage

func PrintColoredMessage(title string, sections []MessageSection, body []byte, mime string)

PrintColoredMessage prints a colored, consistently formatted message with sections and body. Title and section titles are highlighted; items are aligned as key: value; body is pretty-printed by MIME.

func PrintError

func PrintError(format string, args ...interface{})

PrintError prints an error message with color to stderr.

func PrintHeader

func PrintHeader(format string, args ...interface{})

PrintHeader prints a bold header message.

func PrintInfo

func PrintInfo(format string, args ...interface{})

PrintInfo prints an informational message with color.

func PrintKeyValue

func PrintKeyValue(key string, value interface{})

PrintKeyValue prints a key-value pair with color.

func PrintSuccess

func PrintSuccess(format string, args ...interface{})

PrintSuccess prints a success message with color.

func PrintWarning

func PrintWarning(format string, args ...interface{})

PrintWarning prints a warning message with color.

Types

type KV

type KV struct {
	Key   string
	Value string
}

KV represents a single key-value pair to print under a section.

type MessageSection

type MessageSection struct {
	Title string
	Items []KV
}

MessageSection groups related key-value pairs under a titled section.

Jump to

Keyboard shortcuts

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