From f64c0847f861c4baf8554cbf4758ca6f2155ec59 Mon Sep 17 00:00:00 2001 From: cipheras <37106224+cipheras@users.noreply.github.com> Date: Sun, 20 Sep 2020 13:27:49 +0530 Subject: [PATCH] helper module --- go.mod | 3 ++ gohelper.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 go.mod create mode 100755 gohelper.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9d4e9c9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/cipheras/gohelper + +go 1.15 diff --git a/gohelper.go b/gohelper.go new file mode 100755 index 0000000..67c2554 --- /dev/null +++ b/gohelper.go @@ -0,0 +1,98 @@ +package gohelper + +import ( + "fmt" + "log" + "os" +) + +/* +RESET +RED +GREEN +YELLOW +BLUE +PURPLE +CYAN +WHITE +BGBLACK +BOLD +UNDERLINE +BLINK +*/ +const ( + // ANSI color codes + RESET = "\033[0m" + RED = "\033[31m" + GREEN = "\033[32m" + YELLOW = "\033[33m" + BLUE = "\033[34m" + PURPLE = "\033[35m" + CYAN = "\033[36m" + WHITE = "\033[37m" + BGBLACK = "\033[40m" + BOLD = "\033[1m" + UNDERLINE = "\033[4m" + BLINK = "\033[5m" + CLEAR = "\033[2J\033[H" +) + +const ( + // N : + N = "normal" + // E : + E = "error" + // W : + W = "warning" + // T : + T = "text" + // I : + I = "info" + // S : + S = "shell" +) + +// Try ... +func Try(msg string, err error, mode bool) { //msg,err,exit/noexit + if err != nil { + if msg != "" { + if mode == true { + Cprint(E, msg) + log.Println(msg) + os.Exit(0) + } + Cprint(W, msg) + log.Println(msg) + return + } + if mode == true { + Cprint(E, err) + log.Println(err) + os.Exit(0) + } + Cprint(W, err) + log.Println(err) + } +} + +// Cprint ... +func Cprint(mode string, msg ...interface{}) { + var msgs string + for _, v := range msg { + msgs = msgs + fmt.Sprintf("%v ", v) + } + switch mode { + case N: //normal + fmt.Println("\n" + CYAN + "[" + GREEN + "+" + CYAN + "] " + GREEN + msgs + RESET) + case E: //error + fmt.Println("\n" + CYAN + "[" + RED + BLINK + "-" + RESET + CYAN + "] " + RED + BGBLACK + BOLD + "ERROR" + RESET + " " + RED + msgs + RESET) + case W: //warning + fmt.Println("\n" + CYAN + "[" + YELLOW + BLINK + "!" + RESET + CYAN + "] " + YELLOW + BGBLACK + BOLD + "WARN" + RESET + " " + YELLOW + msgs + RESET) + case T: //text + fmt.Println("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + RESET) + case I: //info + fmt.Println("\n" + CYAN + "[" + BLUE + "i" + CYAN + "] " + BLUE + msgs + RESET) + case S: //shell + fmt.Print("\n" + CYAN + "[" + PURPLE + "*" + CYAN + "] " + PURPLE + msgs + "\n" + GREEN + ">> " + RESET) + } +}