util

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package bastion Network-related utilities (hostname extraction and ctx-aware DNS).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdjustPaginationInfo

func AdjustPaginationInfo(pagination *PaginationInfo)

AdjustPaginationInfo adjusts the pagination information to ensure that the total count is correctly displayed. It calculates the total records displayed so far and updates the TotalCount field of the pagination object to match this value.

func BuildIndex

func BuildIndex[T any](items []T, mapToIndexable func(T) any) (bleve.Index, error)

BuildIndex creates an in-memory Bleve index from a slice of items using a provided mapping function. The mapping function converts each item into an indexable structure for searching. Returns the built index or an error if any indexing operation fails.

func ConvertOciTagsToResourceTags added in v0.0.13

func ConvertOciTagsToResourceTags(freeformTags map[string]string, definedTags map[string]map[string]interface{}) domain.ResourceTags

ConvertOciTagsToResourceTags converts OCI FreeformTags and DefinedTags to domain.ResourceTags.

func ExtractHostname added in v0.0.11

func ExtractHostname(endpoint string) string

ExtractHostname removes schema/port/path and returns just the host portion.

func ExtractTagValues

func ExtractTagValues(freeform map[string]string, defined map[string]map[string]interface{}) (string, error)

ExtractTagValues extracts only the values from freeform and defined tags into a single space-separated string. This is useful for making tag values directly searchable without requiring the key prefix. Returns the extracted values string or an empty string if no valid tag values are found.

func FlattenTags

func FlattenTags(freeform map[string]string, defined map[string]map[string]interface{}) (string, error)

FlattenTags flattens freeform and defined tags into a single string with a specific format suitable for indexing. Freeform tags are processed as key:value pairs, while defined tags include namespace, key, and value. Returns the flattened string or an empty string if no valid tags are found.

func FormatBool added in v0.0.20

func FormatBool(b bool) string

FormatBool returns a consistent string representation for booleans used in table outputs. It mirrors the style used elsewhere in the codebase (e.g., fmt with %t), yielding "true"/"false".

func FormatColoredTitle

func FormatColoredTitle(appCtx *app.ApplicationContext, name string) string

FormatColoredTitle builds a colorized title string with tenancy, compartment, and cluster.

func FuzzySearchIndex

func FuzzySearchIndex(index bleve.Index, pattern string, fields []string) ([]int, error)

FuzzySearchIndex performs a fuzzy search on a Bleve index for a given pattern across specified fields. It combines fuzzy, prefix, and wildcard queries, limits the results, and returns matched indices or an error.

func HumanizeBytesIEC added in v0.1.0

func HumanizeBytesIEC(b int64) string

HumanizeBytesIEC converts a byte size into a human‑readable string using IEC units (powers of 1024). Examples:

  • 0 -> "0 B"
  • 1023 -> "1023 B"
  • 1024 -> "1.00 KiB"
  • 2.72 * 1024 * 1024 -> "2.72 MiB"

func IsLocalTCPPortInUse added in v0.0.11

func IsLocalTCPPortInUse(port int) bool

IsLocalTCPPortInUse checks if something is already listening on 127.0.0.1:port. It uses a short dial attempt; if successful, the port is in use.

func LogPaginationInfo

func LogPaginationInfo(pagination *PaginationInfo, appCtx *app.ApplicationContext)

LogPaginationInfo logs pagination information if available and prints it to the output.

func MarshalDataToJSONResponse

func MarshalDataToJSONResponse[T any](p *printer.Printer, items []T, pagination *PaginationInfo) error

MarshalDataToJSONResponse now accepts a printer and returns an error.

func PaginateSlice added in v0.1.0

func PaginateSlice[T any](all []T, limit, pageNum int) ([]T, int, string)

PaginateSlice returns a page of items from the full slice, along with the total count and next page token. If pageNum <= 0, it is treated as 1. If the start index exceeds the total count, an empty page is returned. The next page token is a string page number (e.g., "2") or empty when there is no next page.

func PromptPassword added in v0.1.9

func PromptPassword(question string) (string, error)

PromptPassword prompts the user to enter a password. The input is masked.

func PromptPort added in v0.0.10

func PromptPort(question string, defaultPort int) (int, error)

PromptPort prompts the user to enter a TCP port. If the user enters empty input, the defaultPort is returned. It validates the port is in range [1, 65535].

func PromptString added in v0.0.10

func PromptString(question string, defaultVal string) (string, error)

PromptString prompts the user to enter a string. If the user enters empty input and defaultVal is provided, defaultVal is returned.

func PromptYesNo added in v0.0.5

func PromptYesNo(question string) bool

PromptYesNo prompts the user with a yes or no question and returns true for 'yes' and false for 'no'.

func ResolveHostToIP added in v0.0.11

func ResolveHostToIP(ctx context.Context, hostname string) (string, error)

ResolveHostToIP resolves the hostname to the first IP (IPv4/IPv6). It uses ctx so cancellation/timeouts propagate.

func ShowConstructionAnimation added in v0.0.10

func ShowConstructionAnimation()

ShowConstructionAnimation displays a placeholder animation indicating that a feature is under construction.

func SplitTextByMaxWidth added in v0.0.2

func SplitTextByMaxWidth(text string) []string

SplitTextByMaxWidth splits a space-separated string into multiple lines to ensure they are all visible in the table output with a maximum width per line.

func ValidateAndReportEmpty

func ValidateAndReportEmpty[T any](items []T, pagination *PaginationInfo, out io.Writer) bool

ValidateAndReportEmpty handles the case when a generic list is empty and provides pagination hints.

Types

type JSONResponse

type JSONResponse[T any] struct {
	Items      []T             `json:"items"`
	Pagination *PaginationInfo `json:"pagination,omitempty"`
}

JSONResponse represents a generic JSON structure containing a list of items and optional pagination information.

type PaginationInfo

type PaginationInfo struct {
	CurrentPage   int    `json:"CurrentPage"`
	TotalCount    int    `json:"TotalCount"`
	Limit         int    `json:"Limit"`
	NextPageToken string `json:"NextPageToken"`
}

PaginationInfo holds information about the current page and total results

type ResourceTags

type ResourceTags struct {
	FreeformTags map[string]string
	DefinedTags  map[string]map[string]interface{}
}

ResourceTags represents a collection of user-defined and predefined tags associated with a resource. FreeformTags is a simple key-value pair map defined by the user for tagging purposes. DefinedTags is a nested map containing namespace and keys with associated values for structured tagging.

Jump to

Keyboard shortcuts

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