utils

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatTable = "table"
	FormatJSON  = "json"
	FormatCSV   = "csv"
	FormatXML   = "xml"
)

Variables

View Source
var (
	Env          string
	OutputFormat string
	NoColor      bool
	ValidFormats = []string{FormatTable, FormatJSON, FormatCSV, FormatXML}
)
View Source
var ConfirmPrompt = func(question string, noColor bool) bool {
	var response string

	if !noColor {

		fmt.Print(color.New(color.FgHiRed).Sprint("⚠️  " + question + " "))
		fmt.Print(color.New(color.FgHiWhite, color.Bold).Sprint("[y/N]") + " ")
	} else {
		fmt.Printf("⚠️  %s [y/N] ", question)
	}

	_, err := fmt.Scanln(&response)
	if err != nil {

		if err.Error() == "unexpected newline" {
			return false
		}
		return false
	}

	response = strings.ToLower(strings.TrimSpace(response))
	return response == "y" || response == "yes"
}
View Source
var Prompt = func(msg string, noColor bool) (string, error) {
	if !noColor {

		fmt.Print(color.New(color.FgHiRed, color.Bold).Sprint("❯ " + msg + " "))
	} else {
		fmt.Print("❯ " + msg + " ")
	}

	reader := bufio.NewReader(os.Stdin)
	input, err := reader.ReadString('\n')
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(input), nil
}
View Source
var ResourcePrompt = func(resourceType string, msg string, noColor bool) (string, error) {

	icon := "❯"
	switch strings.ToLower(resourceType) {
	case "port":
		icon = "🔌"
	case "mve":
		icon = "🌐"
	case "mcr":
		icon = "🛰️"
	case "vxc":
		icon = "🔗"
	case "location":
		icon = "📍"
	}

	if !noColor {
		fmt.Print(color.New(color.FgHiRed, color.Bold).Sprint(icon + " " + msg + " "))
	} else {
		fmt.Print(icon + " " + msg + " ")
	}

	reader := bufio.NewReader(os.Stdin)
	input, err := reader.ReadString('\n')
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(input), nil
}

Functions

func GetCurrentEnv

func GetCurrentEnv() string

func ShouldDisableColors added in v0.3.2

func ShouldDisableColors() bool

func WrapColorAwareRunE

func WrapColorAwareRunE(fn func(cmd *cobra.Command, args []string, noColor bool) error) func(cmd *cobra.Command, args []string) error

WrapColorAwareRunE combines WrapRunE and WrapCommandFunc to handle both error formatting and passing the noColor flag to command functions.

func WrapOutputFormatRunE

func WrapOutputFormatRunE(fn func(cmd *cobra.Command, args []string, noColor bool, format string) error) func(cmd *cobra.Command, args []string) error

WrapOutputFormatRunE handles both output format and color settings in command functions. This wrapper takes a function that needs both output format and noColor parameters.

func WrapRunE

func WrapRunE(runE func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error

WrapRunE wraps a RunE function to set SilenceUsage to true if an error occurs and formats the error message.

Types

This section is empty.

Jump to

Keyboard shortcuts

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