func debugCmd()

in cmd/policy-tool/debug/debug.go [39:76]


func debugCmd(cmd *cobra.Command, args []string) error {
	validator, err := gcv.NewValidator(flags.policies, flags.libs, gcv.DisableBuiltins(flags.disabledBuiltins...))
	if err != nil {
		fmt.Printf("Errors Loading Policies:\n%s\n", err)
		os.Exit(1)
	}

	ctx := context.Background()

	// TODO: streaming read
	for _, fileName := range flags.files {
		fileBytes, err := os.ReadFile(fileName)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to read %s: %v\n", fileName, err)
			continue
		}
		lines := strings.Split(string(fileBytes), "\n")
		for idx, line := range lines {
			if len(line) == 0 {
				continue
			}
			result, err := validator.ReviewJSON(ctx, line)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error processing input at %s[%d]: %v\n", fileName, idx, err)
				continue
			}
			vs, err := result.ToViolations()
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error processing violations for input at %s[%d]: %v\n", fileName, idx, err)
				continue
			}
			for _, v := range vs {
				fmt.Printf("%s: %s [%s]\n", v.Resource, v.Message, v.Constraint)
			}
		}
	}
	return nil
}