189 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			189 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package utils
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 	"path/filepath"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/gookit/color"
 | |
| 	"github.com/spf13/cobra"
 | |
| )
 | |
| 
 | |
| func PrintHeader() {
 | |
| 	color.Yellow.Println(CreateLine(15))
 | |
| 	color.Green.Println("    __ __              __                ______                         __                ")
 | |
| 	color.Green.Println("   / //_/  ____   ____/ /  ___          / ____/   _____  ___   ____ _  / /_  ____    _____")
 | |
| 	color.Green.Println("  / ,<    / __ \\ / __  /  / _ \\ ______ / /       / ___/ / _ \\ / __ `/ / __/ / __ \\  / ___/")
 | |
| 	color.Green.Println(" / /| |  / /_/ // /_/ /  /  __//_____// /___    / /    /  __// /_/ / / /_  / /_/ / / /    ")
 | |
| 	color.Green.Println("/_/ |_|  \\____/ \\__,_/   \\___/        \\____/   /_/     \\___/ \\__,_/  \\__/  \\____/ /_/     ")
 | |
| 	color.Green.Println("")
 | |
| 	color.Yellow.Println(CreateLine(15))
 | |
| 
 | |
| }
 | |
| 
 | |
| func CreateLine(weight int) string {
 | |
| 	var line string
 | |
| 	for i := 0; i < weight; i++ {
 | |
| 		line += "---   "
 | |
| 	}
 | |
| 	return line
 | |
| }
 | |
| 
 | |
| func PrintHelpFormated(version string, author string, buildDate string, cmd *cobra.Command) {
 | |
| 
 | |
| 	fmt.Println("")
 | |
| 	cmd.Help()
 | |
| 	fmt.Println("")
 | |
| 	PrintVersion(version, author, buildDate)
 | |
| 
 | |
| }
 | |
| 
 | |
| func PrintVersion(version string, author string, buildDate string) {
 | |
| 	color.Yellow.Println(CreateLine(15))
 | |
| 	color.Green.Println("|   Version: " + version + "     ||  Author: " + author + "  ||  Build date:    " + buildDate + "     |")
 | |
| 	color.Yellow.Println(CreateLine(15))
 | |
| 	fmt.Println("")
 | |
| }
 | |
| 
 | |
| func BytesToStrings(bytes []byte) []string {
 | |
| 	var strings []string
 | |
| 	for _, b := range bytes {
 | |
| 		strings = append(strings, string(b))
 | |
| 	}
 | |
| 	return strings
 | |
| }
 | |
| 
 | |
| func GetEnvVarValueFromOsEnv(key string) string {
 | |
| 	value := os.Getenv(key)
 | |
| 	return value
 | |
| }
 | |
| 
 | |
| func GetGitTokenFromOsEnv() string {
 | |
| 	token := os.Getenv("GITHUB_TOKEN")
 | |
| 	return token
 | |
| }
 | |
| 
 | |
| func GetGitTeaTokenFromOsEnv() string {
 | |
| 	token := os.Getenv("GITEA_TOKEN")
 | |
| 	return token
 | |
| }
 | |
| 
 | |
| func IsValidToken(token string) bool {
 | |
| 	if len(token) != 40 {
 | |
| 		return false
 | |
| 	}
 | |
| 
 | |
| 	// Check if token contains only hexadecimal characters (0-9 and a-f)
 | |
| 	for _, c := range token {
 | |
| 		if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f')) {
 | |
| 			return false
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return true
 | |
| }
 | |
| 
 | |
| func IsValidOrg(org string) bool {
 | |
| 	// Check if organization is valid
 | |
| 	// ...
 | |
| 	return true
 | |
| }
 | |
| 
 | |
| func IsValidProjectName(name string) bool {
 | |
| 	// Check if project name is valid
 | |
| 	// ...
 | |
| 	return true
 | |
| }
 | |
| 
 | |
| // function that print error message and exit program with specific error code
 | |
| func ExitWithError(code int, msg string) {
 | |
| 	// Print error message
 | |
| 	fmt.Println(msg)
 | |
| 	// Exit program with specific error code
 | |
| 	os.Exit(code)
 | |
| }
 | |
| 
 | |
| func CreateDir(dirName string) {
 | |
| 	path, err := os.Getwd()
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	fmt.Println("Current working directory: ", path)
 | |
| 	fmt.Println("Directory to create: ", dirName)
 | |
| 
 | |
| 	dirPath := filepath.Join(path, dirName)
 | |
| 
 | |
| 	err = os.Mkdir(dirPath, 0755)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	fmt.Printf("Created directory '%s' \n", dirName)
 | |
| }
 | |
| 
 | |
| func PadString(s string, n int) string {
 | |
| 	if len(s) >= n {
 | |
| 		return s
 | |
| 	}
 | |
| 	return s + strings.Repeat(" ", n-len(s))
 | |
| }
 | |
| 
 | |
| func PadDotString(s string, n int) string {
 | |
| 	if len(s) >= n {
 | |
| 		return s
 | |
| 	}
 | |
| 	return s + strings.Repeat(".", n-len(s))
 | |
| }
 | |
| 
 | |
| // function to print 3 elements on a line
 | |
| func PrintTreeElement(e1 int, e2 string, e3 string) {
 | |
| 	fmt.Printf("[ %02d ]  %s [ %s ]\n", e1, ColorizeString(PadDotString(e2, 35), color.Green), ColorizeString(e3, color.LightWhite))
 | |
| 
 | |
| }
 | |
| 
 | |
| // function format 3 elements on a line
 | |
| func FormatTreeElement(e1 int, e2 string, e3 string) string {
 | |
| 	return fmt.Sprintf("[ %02d ]  %s [ %s ]\n", e1, ColorizeString(PadDotString(e2, 35), color.Green), ColorizeString(e3, color.LightWhite))
 | |
| }
 | |
| 
 | |
| // function that return a string with a specific color
 | |
| func ColorizeString(s string, color color.Color) string {
 | |
| 	return color.Sprint(s)
 | |
| }
 | |
| 
 | |
| // function that color in red sub-trings in a string
 | |
| func ColorizeSubStrRed(s string, sub string) string {
 | |
| 	return strings.Replace(s, sub, color.Red.Sprint(sub), -1)
 | |
| }
 | |
| 
 | |
| // function that double backslash in a string
 | |
| func DoubleBackSlash(s string) string {
 | |
| 	return strings.Replace(s, "\\", "\\\\", -1)
 | |
| }
 | |
| 
 | |
| // function  that return the program current directory
 | |
| func GetProgramDir() string {
 | |
| 
 | |
| 	// get the absolut path of the executable
 | |
| 	ex, err := os.Executable()
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	// get the directory of the executable
 | |
| 	exPath := filepath.Dir(ex)
 | |
| 
 | |
| 	return exPath
 | |
| }
 | |
| 
 | |
| // function check if  OS = Windows
 | |
| func IsWindows() bool {
 | |
| 	return os.PathSeparator == '\\' && os.PathListSeparator == ';'
 | |
| }
 | |
| 
 | |
| // function check if  OS = Linux
 | |
| func IsLinux() bool {
 | |
| 	return os.PathSeparator == '/' && os.PathListSeparator == ':'
 | |
| }
 |