// Run will handle the functionallity.
func run(cli *cli.Context) {
	// Load specified json configuration file
	configPath := cli.GlobalString("config")
	configuration, err := loadConfig(configPath)
	if err != nil {
		fmt.Println(err)
	}

	contributions := gontrib.ScanContributions(configuration)

	// Get users template selection
	templateName := cli.GlobalString("template")

	// Build path to template
	templatesPath := os.Getenv(templatesFolderEnv)
	if templatesPath == "" {
		templatesPath, err = os.Getwd()
		if err != nil {
			panic(err)
		}
		templatesPath = filepath.Join(templatesPath, templateFolderName)
	}
	absoluteTemplatePath := filepath.Join(templatesPath, templateName)

	outputPath := cli.GlobalString("output")
	f, err := os.Create(outputPath)
	if err != nil {
		fmt.Println(err)
	}
	defer f.Close()

	writer := bufio.NewWriter(f)
	fillTemplate(contributions, absoluteTemplatePath, writer)
	writer.Flush()
}
// Run will handle the functionallity.
func run(ctx *cli.Context) error {
	// Load specified json configuration file
	configPath := ctx.GlobalString("config")
	configuration, err := loadConfig(configPath)
	if err != nil {
		// if config cant be loaded because default one is used
		// (set in StringFlag) and is not available, then show the usage.
		if !ctx.IsSet("config") {
			cli.ShowAppHelp(ctx)
			return nil
		}
		return cli.NewExitError(err.Error(), 1)
	}

	// Get users template selection
	templateName := ctx.GlobalString("template")

	var templateData string

	// Get Template as templateData string
	templatesPath := os.Getenv(templatesFolderEnv)
	if templatesPath == "" {
		// Use asset
		data, err := Asset(filepath.Join(templateFolderName, templateName))
		if err != nil {
			return cli.NewExitError(err.Error(), 1)
		}
		templateData = string(data)
	} else {
		// Use template from user defined folder
		absoluteTemplatePath := filepath.Join(templatesPath, templateName)
		if !util.FileExists(absoluteTemplatePath) {
			var s string
			fmt.Sprintf(s, "Template file %s does not exist\n", absoluteTemplatePath)
			return cli.NewExitError(s, 1)
		}
		data, err := ioutil.ReadFile(absoluteTemplatePath)
		if err != nil {
			return cli.NewExitError(err.Error(), 1)
		}
		templateData = string(data)
	}

	gontrib.PullSources = !ctx.GlobalBool("no-pull")

	contributions, err := gontrib.ScanContributions(configuration)
	if err != nil {
		util.PrintInfo(err.Error(), util.PI_ERROR)
		return cli.NewExitError(err.Error(), 1)
	}

	outputPath := ctx.GlobalString("output")
	f, err := os.Create(outputPath)
	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}
	defer f.Close()

	writer := bufio.NewWriter(f)
	fillTemplate(contributions, templateData, writer)
	if err := writer.Flush(); err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	util.PrintInfoF("\nReport saved in: %s", util.PI_INFO, outputPath)
	return nil
}