go log repl

时间:2020-12-28 20:31:45
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
}