Пример #1
0
// Check check rule or all rules for some service
func Check(target string) error {
	if target == "" {
		return errors.New("You must difine mock file or service name")
	}

	targetService, targetMock, targetDir := rules.ParsePath(target)

	serviceDir := path.Join(knf.GetS(DATA_RULE_DIR), targetService)

	if !fsutil.IsExist(serviceDir) {
		return fmtc.Errorf("Service %s is not exist", targetService)
	}

	var ruleInfoSlice []*RuleInfo

	if targetMock != "" {
		ruleInfoSlice = append(ruleInfoSlice, &RuleInfo{targetService, targetMock, targetDir})
	} else {
		mockFiles := fsutil.ListAllFiles(serviceDir, true,
			&fsutil.ListingFilter{MatchPatterns: []string{"*.mock"}},
		)

		for _, mockFile := range mockFiles {
			mockPath := path.Join(targetService, strings.Replace(mockFile, ".mock", "", -1))

			_, targetMock, targetDir := rules.ParsePath(mockPath)

			ruleInfoSlice = append(ruleInfoSlice, &RuleInfo{targetService, targetMock, targetDir})
		}
	}

	if len(ruleInfoSlice) == 0 {
		fmtc.Println("\n{y}No mock's were found{!}\n")
		return nil
	}

	var maxProblemType = PROBLEM_NONE

	for _, rule := range ruleInfoSlice {
		maxProblemType = mathutil.MaxU8(checkRule(rule), maxProblemType)
	}

	if maxProblemType > PROBLEM_NONE {
		fmtutil.Separator(false)
	}

	switch maxProblemType {
	case PROBLEM_NONE:
		fmtc.Printf("\n{g}%s{!}\n\n", okMessages[rand.Int(len(okMessages))])
	case PROBLEM_WARN:
		fmtc.Printf("{y}%s{!}\n\n", warnMessages[rand.Int(len(warnMessages))])
	case PROBLEM_ERR:
		fmtc.Printf("{r}%s{!}\n\n", errorMessages[rand.Int(len(errorMessages))])
	}

	return nil
}
Пример #2
0
// checkRule parse rule, run validators and print check results
func checkRule(ruleInfo *RuleInfo) uint8 {
	var problems []*Problem

	rule, err := rules.Parse(knf.GetS(DATA_RULE_DIR), ruleInfo.Service, ruleInfo.Dir, ruleInfo.Name)

	if err != nil {
		problems = append(problems,
			&Problem{
				Type: PROBLEM_ERR,
				Info: "Parsing error",
				Desc: err.Error(),
			},
		)
	} else {

		validators := []Validator{
			checkDescription,
			checkWildcard,
			checkMethod,
			checkStatusCode,
			checkContent,
		}

		problems = append(problems, execValidators(validators, rule)...)
	}

	if len(problems) == 0 {
		return PROBLEM_NONE
	}

	fmtutil.Separator(false, path.Join(ruleInfo.Service, ruleInfo.Dir, ruleInfo.Name))
	renderProblems(problems)

	var maxProblemType = PROBLEM_NONE

	for _, problem := range problems {
		maxProblemType = mathutil.MaxU8(problem.Type, maxProblemType)
	}

	return maxProblemType
}