Example #1
0
func List(observer *rules.Observer, service string) error {
	if observer == nil {
		return errors.New("Observer is not created")
	}

	observer.Load()

	if service != "" {
		err := listService(observer, service)

		if err != nil {
			return err
		}
	} else {
		services := observer.GetServices()

		if len(services) == 0 {
			fmtc.Println("\n{y}No services and mocks are created{!}\n")
			return nil
		}

		for _, serviceName := range services {
			err := listService(observer, serviceName)

			if err != nil {
				return nil
			}
		}
	}

	fmt.Println("")

	return nil
}
Example #2
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
}
Example #3
0
// renderProblems print error and warn messages
func renderProblems(problems []*Problem) {
	for _, problem := range problems {
		switch problem.Type {
		case PROBLEM_WARN:
			fmtc.Printf("{y}WARNING →{!} {*}%s{!}\n\n", problem.Info)
		case PROBLEM_ERR:
			fmtc.Printf("{r}ERROR →{!} {*}%s{!}\n\n", problem.Info)
		}

		fmtc.Println(fmtutil.Wrap(problem.Desc, "  ", 86))
		fmtc.NewLine()
	}
}
Example #4
0
// filterFile read file and show records only between given time range
func filterFile(file string) {
	fd, err := os.OpenFile(file, os.O_RDONLY, 0644)

	if err != nil {
		printError("Can't open file %s: %v", file, err)
		os.Exit(1)
	}

	defer fd.Close()

	reader := bufio.NewReader(fd)

	var showSection = false
	var currentSection = ""
	var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"}

	fromDate := time.Date(2016, 1, 1, 0, 0, 0, 0, time.Local)
	toDate := time.Now()

	if arg.Has(ARG_DATE_FROM) {
		fromDate, err = parseRangeDate(arg.GetS(ARG_DATE_FROM))

		if err != nil {
			printError("Can't parse range start: %v", err)
			os.Exit(1)
		}
	}

	if arg.Has(ARG_DATE_TO) {
		toDate, err = parseRangeDate(arg.GetS(ARG_DATE_TO))

		if err != nil {
			printError("Can't parse range end: %v", err)
			os.Exit(1)
		}
	}

	for {
		line, err := reader.ReadString('\n')

		if err != nil {
			break
		}

		line = strings.TrimRight(line, "\n")
		line = strings.TrimRight(line, "\r")

		rt := getLineType(line)

		if rt == TYPE_SEPARATOR {
			recDateStr := extractTimeFromSeparator(line)
			recDate, _ := time.Parse(separatorTimeLayout, recDateStr)

			showSection = recDate.Unix() >= fromDate.Unix() && recDate.Unix() <= toDate.Unix()
		}

		if !showSection {
			continue
		}

		if rt == TYPE_HEADER {
			currentSection = extractHeaderName(line)
			renderLine(line, rt)
			continue
		}

		if sliceutil.Contains(dataSections, currentSection) {
			fmtc.Println(line)
			continue
		}

		renderLine(line, rt)
	}
}
Example #5
0
// readFile starts file reading loop
func readFile(file string) {
	fd, err := os.OpenFile(file, os.O_RDONLY, 0644)

	if err != nil {
		printError("Can't open file %s: %v", file, err)
		os.Exit(1)
	}

	defer fd.Close()

	stat, err := fd.Stat()

	if err != nil {
		printError("Can't read file stats %s: %v", file, err)
		os.Exit(1)
	}

	if stat.Size() > 2048 {
		fd.Seek(-2048, 2)
	}

	reader := bufio.NewReader(fd)

	nearRecordFound := false

	var currentSection = ""
	var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"}

	for {
		line, err := reader.ReadString('\n')

		if err != nil {
			time.Sleep(time.Millisecond * 250)
			continue
		}

		line = strings.TrimRight(line, "\n")
		line = strings.TrimRight(line, "\r")

		if !nearRecordFound {
			if strutil.Head(line, 3) != "-- " {
				continue
			}

			nearRecordFound = true
		}

		rt := getLineType(line)

		if rt == TYPE_HEADER {
			currentSection = extractHeaderName(line)
			renderLine(line, rt)
			continue
		}

		if sliceutil.Contains(dataSections, currentSection) {
			fmtc.Println(line)
			continue
		}

		renderLine(line, rt)
	}
}