utils

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 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
}
View Source
var ResourceTagsPrompt = func(noColor bool) (map[string]string, error) {
	addTags := ConfirmPrompt("Would you like to add resource tags?", noColor)
	if !addTags {
		return nil, nil
	}

	tags := make(map[string]string)
	fmt.Println("Enter tags (key and value). Enter empty key to finish.")

	for {

		key, err := Prompt("Tag key:", noColor)
		if err != nil {
			return nil, err
		}

		if key == "" {
			break
		}

		value, err := Prompt("Tag value for '"+key+"':", noColor)
		if err != nil {
			return nil, err
		}

		tags[key] = value
	}

	if len(tags) > 0 {
		fmt.Println("Tags added:")
		for k, v := range tags {
			fmt.Printf("  %s: %s\n", k, v)
		}
	}

	return tags, nil
}
View Source
var UpdateResourceTagsPrompt = func(existingTags map[string]string, noColor bool) (map[string]string, error) {

	if !noColor {
		fmt.Println(color.New(color.FgHiYellow).Sprint("⚠️  Warning: This operation will replace all existing tags with the new set of tags you define."))
	} else {
		fmt.Println("⚠️  Warning: This operation will replace all existing tags with the new set of tags you define.")
	}

	if len(existingTags) > 0 {
		fmt.Println("Current tags:")
		for k, v := range existingTags {
			fmt.Printf("  %s: %s\n", k, v)
		}
	} else {
		fmt.Println("No existing tags found.")
	}

	proceed := ConfirmPrompt("Do you want to continue with updating tags?", noColor)
	if !proceed {
		return nil, fmt.Errorf("tag update cancelled by user")
	}

	if len(existingTags) > 0 {
		fmt.Println("\nChoose how you want to update tags:")
		fmt.Println("1. Start with a clean slate (remove all existing tags)")
		fmt.Println("2. Start with existing tags and modify them")

		choice, err := Prompt("Enter choice (1 or 2):", noColor)
		if err != nil {
			return nil, err
		}

		tags := make(map[string]string)
		if choice == "2" {

			for k, v := range existingTags {
				tags[k] = v
			}

			fmt.Println("\nYou can now modify, add, or remove tags.")
			fmt.Println("To remove a tag, enter its key and an empty value.")
		} else if choice != "1" {
			return nil, fmt.Errorf("invalid choice: %s", choice)
		}

		fmt.Println("\nEnter tags (key and value). Enter empty key to finish.")
		for {

			key, err := Prompt("Tag key:", noColor)
			if err != nil {
				return nil, err
			}

			if key == "" {
				break
			}

			value, err := Prompt(fmt.Sprintf("Tag value for '%s':", key), noColor)
			if err != nil {
				return nil, err
			}

			if value == "" && tags[key] != "" {
				delete(tags, key)
				fmt.Printf("  Removed tag: %s\n", key)
			} else if value != "" {
				tags[key] = value
				fmt.Printf("  Updated tag: %s: %s\n", key, value)
			}
		}

		fmt.Println("\nFinal tags that will be applied:")
		if len(tags) > 0 {
			for k, v := range tags {
				fmt.Printf("  %s: %s\n", k, v)
			}
		} else {
			fmt.Println("  No tags - all existing tags will be removed")
		}

		confirmApply := ConfirmPrompt("Apply these changes?", noColor)
		if !confirmApply {
			return nil, fmt.Errorf("tag update cancelled by user")
		}

		return tags, nil
	} else {

		return ResourceTagsPrompt(noColor)
	}
}

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