github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zcli/utils.go (about) 1 package zcli 2 3 import ( 4 "errors" 5 "flag" 6 "fmt" 7 "strings" 8 9 "github.com/sohaha/zlsgo/zlog" 10 "github.com/sohaha/zlsgo/ztype" 11 "github.com/sohaha/zlsgo/zutil" 12 "github.com/sohaha/zlsgo/zutil/daemon" 13 ) 14 15 func errorText(msg string) string { 16 return zlog.ColorTextWrap(zlog.ColorRed, msg) 17 } 18 19 func tipText(msg string) string { 20 return zlog.ColorTextWrap(zlog.ColorGreen, msg) 21 } 22 23 func warnText(msg string) string { 24 return zlog.ColorTextWrap(zlog.ColorLightYellow, msg) 25 } 26 27 func showText(msg string) string { 28 return zlog.ColorTextWrap(zlog.ColorLightGrey, msg) 29 } 30 31 // Help show help 32 func Help() { 33 if matchingCmd != nil { 34 showSubcommandUsage(flag.CommandLine, matchingCmd) 35 } else { 36 flag.Usage() 37 } 38 osExit(0) 39 } 40 41 func numOfGlobalFlags() (count int) { 42 flag.VisitAll(func(_ *flag.Flag) { 43 count++ 44 }) 45 return 46 } 47 48 func Error(format string, v ...interface{}) { 49 Log.Errorf(format, v...) 50 if !HidePrompt { 51 tip := "\nPlease use " + tipText("%s --help") + " for more information\n" 52 Log.Printf(tip, FirstParameter) 53 } 54 osExit(1) 55 } 56 57 func showRequired(_ *flag.FlagSet, requiredFlags []string) { 58 flagMapLen := len(requiredFlags) 59 if flagMapLen > 0 { 60 Log.Printf("\n required flags:\n\n") 61 arr := make([]string, flagMapLen) 62 for i := 0; i < flagMapLen; i++ { 63 arr[i] = "-" + ztype.ToString(requiredFlags[i]) 64 } 65 Log.Printf(" %s\n\n\n", errorText(strings.Join(arr, ", "))) 66 } 67 } 68 69 func showSubcommandUsage(fs *flag.FlagSet, _ *cmdCont) { 70 fs.Usage() 71 } 72 73 func showLogo() bool { 74 if Logo != "" { 75 Log.Printf("%s\n\n", strings.Replace(Logo+"v"+Version, "\n", "", 1)) 76 return true 77 } 78 return false 79 } 80 81 func showFlagsHelp() { 82 if !HideHelp { 83 Log.Println(warnText(fmt.Sprintf(" -%-12s", "help")) + "\t" + GetLangText("help")) 84 } 85 } 86 87 func showDescription(logoOk bool) bool { 88 if Name != "" { 89 if !logoOk && Version != "" { 90 Log.Printf("%s v%s\n\n", Name, Version) 91 } else if !logoOk { 92 Log.Printf("%s\n\n", Name) 93 } 94 95 return true 96 } 97 return false 98 } 99 100 func showVersion() bool { 101 if Version != "" { 102 Log.Printf("Version: %s\n\n", Version) 103 return true 104 } 105 return false 106 } 107 108 func showVersionNum(info bool) { 109 if info { 110 Log.Printf("version=%s\n\n", Version) 111 //noinspection GoBoolExpressions 112 if BuildGoVersion != "" { 113 Log.Printf("goVersion=%s\n\n", BuildGoVersion) 114 } 115 //noinspection GoBoolExpressions 116 if BuildTime != "" { 117 Log.Printf("buildTime=%s\n\n", BuildTime) 118 } 119 //noinspection GoBoolExpressions 120 if BuildGitCommitID != "" { 121 Log.Printf("GitCommitID=%s\n\n", BuildGitCommitID) 122 } 123 } else { 124 Log.Println(Version) 125 } 126 } 127 128 func showHeadr() { 129 var ( 130 versionOk bool 131 descriptionOk bool 132 ) 133 logoOk := showLogo() 134 descriptionOk = showDescription(logoOk) 135 if !logoOk && !descriptionOk { 136 versionOk = showVersion() 137 } 138 if logoOk || versionOk || descriptionOk { 139 Log.Println("") 140 } 141 } 142 143 func argsIsHelp(args []string) { 144 if !*flagHelp { 145 for _, value := range args { 146 if value == "-h" || value == "-help" || value == "--help" { 147 *flagHelp = true 148 return 149 } 150 } 151 } 152 } 153 154 func CheckErr(err error, exit ...bool) { 155 if serviceErr == daemon.ErrNoServiceSystemDetected { 156 err = errors.New(zutil.GetOs() + " does not support process daemon") 157 exit = []bool{true} 158 } 159 if err != nil { 160 if len(exit) > 0 && exit[0] { 161 Log.Error(err) 162 osExit(1) 163 return 164 } 165 Log.Fatal(err) 166 } 167 } 168 169 func IsDoubleClickStartUp() bool { 170 return zutil.IsDoubleClickStartUp() 171 } 172 173 func isDetach(a string) bool { 174 for _, v := range []string{"D", "detach"} { 175 if strings.TrimLeft(a, "-") == v { 176 return true 177 } 178 } 179 return false 180 }