view.go 2.7 KB
Newer Older
m0_58228130's avatar
view  
m0_58228130 已提交
1 2 3 4
package action

import (
	"fmt"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
5 6 7
	commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
	langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
	scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
8
	"github.com/easysoft/zentaoatf/pkg/consts"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
9 10 11 12
	commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
	fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
	i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
	logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
m0_58228130's avatar
view  
m0_58228130 已提交
13 14 15 16 17 18 19 20 21
	"github.com/fatih/color"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func View(files []string, keywords string) {
	var cases []string
aaronchen2k2k's avatar
aaronchen2k2k 已提交
22 23 24 25
	for _, f := range files {
		group := scriptHelper.LoadScriptByWorkspace(f)
		for _, item := range group {
			cases = append(cases, item)
m0_58228130's avatar
view  
m0_58228130 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
		}
	}

	view(cases, keywords)
}

func view(cases []string, keywords string) {
	keywords = strings.TrimSpace(keywords)
	count := 0

	arrs := make([][]string, 0)
	for _, file := range cases {
		pass, arr := brief(file, keywords)

		if pass {
			arrs = append(arrs, arr)
			count++
		}
	}

	total := len(arrs)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
48
	logUtils.Info("\n" + time.Now().Format(consts.DateTimeFormat) + " " +
49
		i118Utils.Sprintf("found_scripts_no_ztf_dir", total, commConsts.WorkDir))
m0_58228130's avatar
view  
m0_58228130 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

	width := len(strconv.Itoa(len(arrs)))
	for idx, arr := range arrs {
		numb := fmt.Sprintf("#%0"+strconv.Itoa(width)+"d", idx+1)

		logUtils.Infof(logUtils.GetWholeLine(numb+" "+arr[3], "="))
		logUtils.ExecConsole(color.FgCyan, fmt.Sprintf("%s. %s", arr[0], arr[1]))

		fmt.Printf("Steps: \n%s \n", arr[2])

		logUtils.Info("")
	}
}

func brief(file string, keywords string) (bool, []string) {
	content := fileUtils.ReadFile(file)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
66
	lang := langHelper.GetLangByFile(file)
m0_58228130's avatar
view  
m0_58228130 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	isOldFormat := strings.Index(content, "[esac]") > -1

	regStr := ""
	if isOldFormat {
		regStr = `\[case\][\S\s]*` +
			`title=([^\n]*)\n+` +
			`cid=([^\n]*)\n+` +
			`pid=([^\n]*)\n+` +
			`([\S\s]*)\n*` +
			`\[esac\]`
	} else {
		regStr = fmt.Sprintf(`(?sm)%s[\S\s]*`+
			`title=([^\n]*)\n+`+
			`cid=([^\n]*)\n+`+
			`pid=([^\n]*)\n+`+
			`([\S\s]*)\n*%s`,
aaronchen2k2k's avatar
aaronchen2k2k 已提交
83
			commConsts.LangCommentsRegxMap[lang][0], commConsts.LangCommentsRegxMap[lang][1])
m0_58228130's avatar
view  
m0_58228130 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}
	myExp := regexp.MustCompile(regStr)
	arr := myExp.FindStringSubmatch(content)

	if len(arr) > 2 {
		title := commonUtils.RemoveBlankLine(arr[1])
		caseId := commonUtils.RemoveBlankLine(arr[2])

		//productId := commonUtils.RemoveBlankLine(arr[3])
		steps := commonUtils.RemoveBlankLine(arr[4])

		_, err := strconv.Atoi(keywords)
		var pass bool

		if err == nil && keywords == caseId { // int
			pass = true
		} else if strings.Index(title, keywords) > -1 {
			pass = true
		}

		if pass {
			return true, []string{caseId, title, steps, file}
		} else {
			return false, nil
		}
	}

	return false, nil
}