예제 #1
0
파일: cli.go 프로젝트: essentialkaos/mockka
func runServer() {
	observer := rules.NewObserver(knf.GetS(DATA_RULE_DIR))
	observer.AutoHead = knf.GetB(PROCESSING_AUTO_HEAD)
	observer.Start(knf.GetI(DATA_CHECK_DELAY))

	err := server.Start(observer, APP+"/"+VER, arg.GetS(ARG_PORT))

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}
}
예제 #2
0
파일: cli.go 프로젝트: essentialkaos/mockka
func Init() {
	var err error
	var errs []error

	runtime.GOMAXPROCS(1)

	if len(os.Args) <= 1 {
		showUsage()
		return
	}

	args, errs := arg.Parse(argMap)

	if len(errs) != 0 {
		for _, err := range errs {
			fmt.Println(err.Error())
		}

		os.Exit(1)
	}

	if arg.GetB(ARG_NO_COLOR) {
		fmtc.DisableColors = true
	}

	if arg.GetB(ARG_VER) {
		showAbout()
		return
	}

	if len(args) == 0 || arg.GetB(ARG_HELP) {
		showUsage()
		return
	}

	// Slice with valid config paths
	confPaths := []string{
		arg.GetS(ARG_CONFIG),
		"/etc/mockka.conf",
		"~/mockka.conf",
		"mockka.conf",
	}

	err = knf.Global(fsutil.ProperPath("FRS", confPaths))

	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	errs = validateConfig()

	if len(errs) != 0 {
		for _, err := range errs {
			fmt.Println(err.Error())
		}

		os.Exit(1)
	}

	setupLog()

	if arg.GetB(ARG_DAEMON) {
		registerSignalHandlers()
	}

	execCommand(args)
}
예제 #3
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)
	}
}