<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package build

import (
	"errors"
	"fmt"

	"github.com/r3labs/composable/yaml"
	"github.com/spf13/cobra"
)

func Generate(cmd *cobra.Command, args []string) {
	environment, _ := cmd.Flags().GetString("environment")
	overrides, _ := cmd.Flags().GetString("overrides")
	excludes, _ := cmd.Flags().GetString("excludes")
	global, _ := cmd.Flags().GetString("global-branch")
	output, _ := cmd.Flags().GetString("compose-file")
	buildpath, _ := cmd.Flags().GetString("build-path")
	edition, _ := cmd.Flags().GetString("edition")

	if buildpath == "" {
		fatal(errors.New("no build path specified"))
	}

	if len(args) < 2 {
		fatal(errors.New("generate must specify a definition and template file"))
	}

	d, err := yaml.LoadDefinition(args[0])
	if err != nil {
		fatal(err)
	}

	d.Template = args[1]
	d.BuildPath = buildpath
	d.Environment(environment)
	d.Overrides(overrides, excludes, global)

	for _, repo := range d.Repos {
		repo.SetEnv("ERNEST_EDITION", edition)
	}

	if edition == "community" {
		for i := len(d.Repos) - 1; i >= 0; i-- {
			if d.Repos[i]["edition"] == "enterprise" {
				d.ExcludeRepo(d.Repos[i].Name())
			}
		}
	}

	fmt.Println("cloning repos:")
	err = CloneRepos(d, buildpath)
	if err != nil {
		fatal(err)
	}

	fmt.Println("generating output")
	err = d.GenerateOutput(output)
	if err != nil {
		fatal(err)
	}
}<commit_msg>[#ERNEST-860] : Generate to gracefully fail if destination folder does not exist<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package build

import (
	"errors"
	"fmt"
	"os"

	"github.com/r3labs/composable/yaml"
	"github.com/spf13/cobra"
)

func Generate(cmd *cobra.Command, args []string) {
	environment, _ := cmd.Flags().GetString("environment")
	overrides, _ := cmd.Flags().GetString("overrides")
	excludes, _ := cmd.Flags().GetString("excludes")
	global, _ := cmd.Flags().GetString("global-branch")
	output, _ := cmd.Flags().GetString("compose-file")
	buildpath, _ := cmd.Flags().GetString("build-path")
	edition, _ := cmd.Flags().GetString("edition")

	if buildpath == "" {
		fatal(errors.New("no build path specified"))
	}

	if len(args) < 2 {
		fatal(errors.New("generate must specify a definition and template file"))
	}

	d, err := yaml.LoadDefinition(args[0])
	if err != nil {
		fatal(err)
	}

	d.Template = args[1]
	d.BuildPath = buildpath
	d.Environment(environment)
	d.Overrides(overrides, excludes, global)

	for _, repo := range d.Repos {
		repo.SetEnv("ERNEST_EDITION", edition)
	}

	if edition == "community" {
		for i := len(d.Repos) - 1; i >= 0; i-- {
			if d.Repos[i]["edition"] == "enterprise" {
				d.ExcludeRepo(d.Repos[i].Name())
			}
		}
	}

	fmt.Println("cloning repos:")
	// Check if buildpath exists
	if _, err := os.Stat(buildpath); os.IsNotExist(err) {
		fatal(errors.New("Specified '" + buildpath + "' folder does not exist"))
	}
	err = CloneRepos(d, buildpath)
	if err != nil {
		fatal(err)
	}

	fmt.Println("generating output")
	err = d.GenerateOutput(output)
	if err != nil {
		fatal(err)
	}
}
{'fraction_non_alphanumeric': 0.16884661117717004, 'fraction_numerical': 0.00802615933412604, 'mean_word_length': 3.9195906432748537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3552358', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1227, 'n_words': 285}