From 77cd6aac14fc7aa6f4e67c24a0649ba06981d212 Mon Sep 17 00:00:00 2001 From: oscar Date: Sun, 21 Aug 2022 17:03:53 +1200 Subject: [PATCH] go(utils): support to print message with color --- go/utils/print.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ go/utils/prompt.go | 22 ++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 go/utils/print.go create mode 100644 go/utils/prompt.go diff --git a/go/utils/print.go b/go/utils/print.go new file mode 100644 index 0000000..7e24429 --- /dev/null +++ b/go/utils/print.go @@ -0,0 +1,65 @@ +package utils + +import "fmt" + +const ( + colorReset string = "\033[0m" + + colorRed string = "\033[31m" + colorGreen string = "\033[32m" + colorYellow string = "\033[33m" + colorBlue string = "\033[34m" + colorPurple string = "\033[35m" + colorCyan string = "\033[36m" + colorWhite string = "\033[37m" +) + +func PrintOutput(message string, output []byte) { + if message != "" { + HighlightPrint(message) + } + fmt.Println(string(output)) +} + +func HighlightPrint(message string) { + fmt.Println() + fmt.Println(colorBlue, message, colorReset) +} + +func SuccessPrint(message string) { + fmt.Println() + fmt.Println(colorGreen, message, colorReset) +} + +func ErrorPrint(message string) { + fmt.Println() + fmt.Println(colorRed, message, colorReset) +} + +func InputPrint(message string) { + fmt.Println() + fmt.Println(colorYellow, message, colorReset) +} + +func MenuPrint() { + InputPrint("Which repository or action do you want to operate:") + + // menu := ` + // 1. Build Portainer EE/CE All + // 2. Build Portainer EE/CE Frontend + // 3. Build Portainer EE/CE Backend + // 4. Generate Portainer EE/CE JWT + // 5. Run Before Commit [Portainer EE/CE] + // 6. Get Portainer CE API Reference + // 7. Run Before Commit [k8s] + // 8. Build Portainer Agent + // 9. Cleanup Temporary Volume + // ` + + menu := `1. Portainer EE Repository + 2. Portainer CE Repository + 3. Portainer Agent Repository + 4. Others` + + fmt.Println(colorCyan, menu, colorReset) +} diff --git a/go/utils/prompt.go b/go/utils/prompt.go new file mode 100644 index 0000000..5d7465d --- /dev/null +++ b/go/utils/prompt.go @@ -0,0 +1,22 @@ +package utils + +import ( + "fmt" + "strings" +) + +func PromptContinue() bool { + ret := strings.ToLower(prompt("Continue (y/n)")) + if ret == "y" || ret == "yes" { + return true + } + + return false +} + +func prompt(question string) string { + fmt.Printf("%s %s :%s", colorYellow, question, colorReset) + var ret string + fmt.Scanf("%s", &ret) + return ret +}