func affectedCmd()

in custard/cmd/custard/main.go [84:126]


func affectedCmd(configFile string, diffsFile string, pathsFile string) {
	config, err := c.LoadConfig(configFile)
	if err != nil {
		log.Fatalln("❌ error loading the config file: ", configFile, "\n", err)
	}

	diffsBytes, err := os.ReadFile(diffsFile)
	if err != nil {
		log.Fatalln("❌ error getting the diffs: ", diffsFile, "\n", err)
	}
	// Trim whitespace to remove extra newline from diff output.
	diffs := strings.Split(strings.TrimSpace(string(diffsBytes)), "\n")

	// Log to stderr since GitHub Actions expects the output on stdout.
	paths, err := config.Affected(os.Stderr, diffs)
	if err != nil {
		log.Fatalln("❌ error finding the affected packages.\n", err)
	}
	if len(paths) > 256 {
		log.Fatalln(
			"❌ Error: GitHub Actions only supports up to 256 packages, got ",
			len(paths),
			" packages, for more details see:\n",
			"https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow",
		)
	}

	if pathsFile != "" {
		file, err := os.Create(pathsFile)
		if err != nil {
			log.Fatalln("❌ eror creating output file.\n", err)
		}
		for _, path := range paths {
			fmt.Fprintf(file, "%v\n", path)
		}
	}

	output, err := json.Marshal(paths)
	if err != nil {
		log.Fatalln("❌ error marshaling paths to JSON.\n", err)
	}
	fmt.Println(string(output))
}