go log repl

路過的世界發表於2016-12-05
package main

import (
	"fmt"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func main() {
	info()

	for {
		var line string
		fmt.Scanf("%s\n", &line)
		isFinish, message := preprocess(line)
		if isFinish {
			msg(message)
			continue
		}
		echo, url, err := newLogHelper(line).process()
		if err != nil {
			msg(err.Error())
			continue
		}
		msg(echo)
		args := []string{"cmd", "/c", "start", "chrome"}
		cmd := exec.Command(args[0], append(args[1:], url)...)
		cmd.Start()
	}
}

func msg(str string) {
	fmt.Println(str)
}

func info() {
	titleList = []string{"可定檢查", "中間產品層", "建立訂單"}
	keyList = []string{"member_hotel_check_avail", "get_corp_hotel_data", "member_hotel_reservation", "contract_hotel_check_avail", "get_corp_hotel_data", "soa2__create_order_log"}
	app150120 = []string{""}
	helpText = "k+數字(預設是k1):\n\r "
	for i := 0; i < len(titleList); i++ {
		helpText += fmt.Sprintf("%d", i+1) + "." + titleList[i] + "\n\r "
	}
	helpText += "預設會員,協議在數字前加0\n\rd/h+數字(預設是d7):\n\r 1.一天(h是小時)內日誌\n\r 2.兩天(h是小時)內日誌\n\r 以此類推...\n\ro+數字:\n\r 開啟ordeidr=數字的日誌\n\rr+數字:\n\r 開啟roomId=數字的日誌\n\ru+卡號:\n\r 開的uid=卡號的日誌\n\rc+數字:\n\r 開啟cityID=數字的日誌\n\rhelp:檢視命令\n\ree切換生產/測試環境"
	msg(helpText)
}

func preprocess(str string) (isFinish bool, msg string) {
	switch {
	case str == "help":
		return true, helpText
	case str == "ee":
		isPro = !isPro
		if isPro {
			return true, "切換為生產環境日誌查詢..."
		} else {
			return true, "切換為測試環境日誌查詢..."
		}

	default:
		return false, ""

	}
}

var titleList []string
var keyList []string
var helpText string
var isPro bool
var app150120 []string

const support = 3

type LogHelper struct {
	cmd, o, r, u, c string //o==orderid ,r == roomid,u=uid,c=cityid
	k, d, h         int
}

func newLogHelper(line string) *LogHelper {
	return &LogHelper{cmd: strings.ToLower(line)}
}

func (h *LogHelper) process() (string, string, error) {
	if strings.Contains(h.cmd, "ee") {
		isPro = !isPro
	}
	h.k = h.getNumber("k")
	h.d = h.getNumber("d")
	h.h = h.getNumber("h")

	h.o = h.getStr("o")
	h.r = h.getStr("r")
	h.u = h.getStr("u")
	h.c = h.getStr("c")
	var dateStr, title, key string
	var beginTime time.Time
	endTime := time.Now()
	echo := "正在開啟"

	switch {
	case h.d > 0:
		beginTime = endTime.AddDate(0, 0, -h.d)
		echo += to_s(h.d) + "天內的"
	case h.h > 0:
		beginTime = endTime.Add(time.Hour * time.Duration(-h.h))
		echo += to_s(h.h) + "小時內的"
	default:
		beginTime = endTime.AddDate(0, 0, -7)
		echo += "7天內的"
	}
	dateStr = h.getDateStr(beginTime, endTime)
	tags := "~tags="
	switch {
	case h.k > 0 && h.k < support:
		key = "key=" + keyList[h.k-1]
		title = "會員" + titleList[h.k-1]
	case h.k >= support:
		key = "key=" + keyList[h.k-1]
		title = "協議" + keyList[h.k-support]
	default:
		// if !strings.Contains(h.cmd, "o") {
		//     key = "key=" + keyList[0]
		//     title = "會員" + titleList[0]
		// }
	}
	if len(key) > 4 {
		tags += key + ";"
	}
	echo += title
	if h.o != "" {
		ss := "orderId=" + h.o
		echo += "&" + ss
		tags += ss + ";"
	}

	if h.r != "" {
		ss := "roomId=" + h.r
		echo += "&" + ss
		tags += ss + ";"
	}

	if h.u != "" {
		ss := "uid=" + h.u
		echo += "&" + ss
		tags += ss + ";"
	}

	if h.c != "" {
		ss := "cityId=" + h.c
		echo += "&" + ss
		tags += ss + ";"
	}
	if isPro {
		echo += "(生產環境)"
	} else {
		echo += "(測試環境)"
	}

	echo += "日誌..."
	var appid string
	if contains(app150120, strings.Replace(key, "key=", "", 1)) {
		appid = "~app=150120"
	}
	url := "http://logging.fws.qa.nt.ctripcorp.com/#?"

	if isPro {
		url = "http://logging.ctripcorp.com/#?"
	}

	url += dateStr + appid + tags
	return echo, url, nil
}

func (helper *LogHelper) getStr(prefix string) string {
	reg := regexp.MustCompile(prefix + `(\d+)`)
	slice := reg.FindStringSubmatch(helper.cmd)
	if len(slice) > 1 {
		return slice[1]
	}
	return ""
}

func (h *LogHelper) getNumber(prefix string) int {
	str := h.getStr(prefix)
	if str != "" && str[0] == '0' {
		return to_i(str) + support - 1
	}
	return to_i(str)
}
func (h *LogHelper) getDateStr(beginTime time.Time, endTime time.Time) string {
	return fmt.Sprintf("fromDate=%s~toDate=%s", beginTime.Format("2006-01-02_15:04:05"), endTime.Format("2006-01-02_15:04:05"))
}

func to_s(num int) string {
	return strconv.Itoa(num)
}

func to_i(str string) int {
	num, err := strconv.Atoi(str)
	if err != nil {
		return 0
	}
	return num
}

func contains(slice []string, item string) bool {
	set := make(map[string]struct{}, len(slice))
	for _, s := range slice {
		set[s] = struct{}{}
	}

	_, ok := set[item]
	return ok
}

  

相關文章