Пример #1
0
// runCommand runs all the BQL statements available in the file.
func runCommand(ctx context.Context, cmd *command.Command, args []string, store storage.Store, chanSize int) int {
	if len(args) < 3 {
		fmt.Fprintf(os.Stderr, "[ERROR] Missing required file path. ")
		cmd.Usage()
		return 2
	}
	file := strings.TrimSpace(args[len(args)-1])
	lines, err := io.GetStatementsFromFile(file)
	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR] Failed to read file %s\n\n\t%v\n\n", file, err)
		return 2
	}
	fmt.Printf("Processing file %s\n\n", args[len(args)-1])
	for idx, stm := range lines {
		fmt.Printf("Processing statement (%d/%d):\n%s\n\n", idx+1, len(lines), stm)
		tbl, err := BQL(ctx, stm, store, chanSize)
		if err != nil {
			fmt.Printf("[FAIL] %v\n\n", err)
			continue
		}
		fmt.Println("Result:")
		if tbl.NumRows() > 0 {
			fmt.Println(tbl)
		}
		fmt.Printf("OK\n\n")
	}
	return 0
}
Пример #2
0
// assertCommand runs all the BQL statements available in the file.
func assertCommand(ctx context.Context, cmd *command.Command, args []string, store storage.Store, builder literal.Builder, chanSize int) int {
	if len(args) < 3 {
		fmt.Fprintf(os.Stderr, "Missing required folder path. ")
		cmd.Usage()
		return 2
	}
	// Open the folder.
	folder := strings.TrimSpace(args[len(args)-1])
	f, err := os.Open(folder)
	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR] Failed to open folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fis, err := f.Readdir(0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR] Failed to read folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fmt.Println("-------------------------------------------------------------")
	fmt.Printf("Processing folder %q...\n", folder)
	var stories []*compliance.Story
	empty := true
	for _, fi := range fis {
		if !strings.Contains(fi.Name(), "json") {
			continue
		}
		fmt.Printf("\tProcessing file %q... ", fi.Name())
		lns, err := io.ReadLines(path.Join(folder, fi.Name()))
		if err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to read file content with error %v\n\n", err)
			return 2
		}
		rawStory := strings.Join(lns, "\n")
		s := &compliance.Story{}
		if err := s.Unmarshal(rawStory); err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to unmarshal story with error %v\n\n", err)
			return 2
		}
		empty = false
		stories = append(stories, s)
		fmt.Println("done.")
	}
	if empty {
		fmt.Println("No stories found!")
		fmt.Println("-------------------------------------------------------------")
		return 2
	}
	fmt.Println("-------------------------------------------------------------")
	fmt.Printf("Evaluating %d stories... ", len(stories))
	results := compliance.RunStories(ctx, store, builder, stories, chanSize)
	fmt.Println("done.")
	fmt.Println("-------------------------------------------------------------")
	for i, entry := range results.Entries {
		fmt.Printf("(%d/%d) Story %q...\n", i+1, len(stories), entry.Story.Name)
		if entry.Err != nil {
			fmt.Fprintf(os.Stderr, "\tFailed to run story %q with error %v\n\n", entry.Story.Name, entry.Err)
			return 2
		}
		for aid, aido := range entry.Outcome {
			if aido.Equal {
				fmt.Printf("\t%s [Assertion=TRUE]\n", aid)
			} else {
				fmt.Printf("\t%s [Assertion=FALSE]\n\nGot:\n\n%s\nWant:\n\n%s\n", aid, aido.Got, aido.Want)
			}
		}
		fmt.Println()
	}
	fmt.Println("-------------------------------------------------------------")
	fmt.Println("\ndone")
	return 0
}