<commit_before>package commands

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"strings"
	"text/template"
)

func (c *Cmd) Output(v interface{}) error {
	if c.Template == "" {
		return c.OutputJSON(v, true)
	} else {
		return c.OutputTemplate(v)
	}
}

func (c *Cmd) OutputJSON(v interface{}, prettyFlag bool) error {
	var err error
	var jsonRaw []byte

	if prettyFlag {
		jsonRaw, err = json.MarshalIndent(v, "", "  ")
	} else {
		jsonRaw, err = json.Marshal(v)
	}

	if err != nil {
		return err
	}

	fmt.Fprintf(c.Out, string(jsonRaw))

	return nil
}

func (c *Cmd) OutputTemplate(v interface{}) error {
	if c.Template == "" {
		return fmt.Errorf("Empty output template")
	}

	if strings.HasPrefix(c.Template, "@") {
		v, err := ioutil.ReadFile(c.Template[1:])
		if err != nil {
			return err
		}
		c.Template = string(v)
	}

	template, err := template.New("").Parse(c.Template)<commit_msg>Terminate all JSON output with a newline<commit_after>package commands

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"strings"
	"text/template"
)

func (c *Cmd) Output(v interface{}) error {
	if c.Template == "" {
		return c.OutputJSON(v, true)
	} else {
		return c.OutputTemplate(v)
	}
}

func (c *Cmd) OutputJSON(v interface{}, prettyFlag bool) error {
	var err error
	var jsonRaw []byte

	if prettyFlag {
		jsonRaw, err = json.MarshalIndent(v, "", "  ")
	} else {
		jsonRaw, err = json.Marshal(v)
	}

	if err != nil {
		return err
	}

	fmt.Fprintf(c.Out, string(jsonRaw))

	jsonStr := string(jsonRaw)
	if strings.HasSuffix(jsonStr, "\n") {
		fmt.Fprintf(c.Out, jsonStr)
	} else {
		fmt.Fprintf(c.Out, jsonStr+"\n")
	}

	return nil
}

func (c *Cmd) OutputTemplate(v interface{}) error {
	if c.Template == "" {
		return fmt.Errorf("Empty output template")
	}

	if strings.HasPrefix(c.Template, "@") {
		v, err := ioutil.ReadFile(c.Template[1:])
		if err != nil {
			return err
		}
		c.Template = string(v)
	}

	template, err := template.New("").Parse(c.Template)
{'fraction_non_alphanumeric': 0.16402657128257536, 'fraction_numerical': 0.001021972406745018, 'mean_word_length': 3.596244131455399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1632360', 'n_tokens_mistral': 787, 'n_tokens_neox': 746, 'n_words': 177}