123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package com
- import (
- "bytes"
- "fmt"
- "os/exec"
- "runtime"
- "strings"
- )
- func ExecCmdDirBytes(dir, cmdName string, args ...string) ([]byte, []byte, error) {
- bufOut := new(bytes.Buffer)
- bufErr := new(bytes.Buffer)
- cmd := exec.Command(cmdName, args...)
- cmd.Dir = dir
- cmd.Stdout = bufOut
- cmd.Stderr = bufErr
- err := cmd.Run()
- return bufOut.Bytes(), bufErr.Bytes(), err
- }
- func ExecCmdBytes(cmdName string, args ...string) ([]byte, []byte, error) {
- return ExecCmdDirBytes("", cmdName, args...)
- }
- func ExecCmdDir(dir, cmdName string, args ...string) (string, string, error) {
- bufOut, bufErr, err := ExecCmdDirBytes(dir, cmdName, args...)
- return string(bufOut), string(bufErr), err
- }
- func ExecCmd(cmdName string, args ...string) (string, string, error) {
- return ExecCmdDir("", cmdName, args...)
- }
- const (
- Gray = uint8(iota + 90)
- Red
- Green
- Yellow
- Blue
- Magenta
-
- EndColor = "\033[0m"
- )
- func getColorLevel(level string) string {
- level = strings.ToUpper(level)
- switch level {
- case "TRAC":
- return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
- case "ERRO":
- return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
- case "WARN":
- return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level)
- case "SUCC":
- return fmt.Sprintf("\033[%dm%s\033[0m", Green, level)
- default:
- return level
- }
- }
- func ColorLogS(format string, a ...interface{}) string {
- log := fmt.Sprintf(format, a...)
- var clog string
- if runtime.GOOS != "windows" {
-
- i := strings.Index(log, "]")
- if log[0] == '[' && i > -1 {
- clog += "[" + getColorLevel(log[1:i]) + "]"
- }
- log = log[i+1:]
-
- log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1)
- log = strings.Replace(log, " ]", EndColor+"]", -1)
-
- log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1)
- log = strings.Replace(log, " )", EndColor+")", -1)
-
- log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1)
- log = strings.Replace(log, " #", EndColor, -1)
- } else {
-
- i := strings.Index(log, "]")
- if log[0] == '[' && i > -1 {
- clog += "[" + log[1:i] + "]"
- }
- log = log[i+1:]
-
- log = strings.Replace(log, "[ ", "[", -1)
- log = strings.Replace(log, " ]", "]", -1)
-
- log = strings.Replace(log, "( ", "(", -1)
- log = strings.Replace(log, " )", ")", -1)
-
- log = strings.Replace(log, "# ", "", -1)
- log = strings.Replace(log, " #", "", -1)
- }
- return clog + log
- }
- func ColorLog(format string, a ...interface{}) {
- fmt.Print(ColorLogS(format, a...))
- }
|