<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
)

var (
	action   = flag.String("action", "evaluate", "What should I do?")
	language = flag.String("language", "", "Language to evaluate.")
	debug    = flag.Bool("debug", false, "Print debugging output?")
	server   = flag.String("server", "http://eval.so/jsontest", "API server to evaluate against.")
)

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: evalso [flags]\n")
	fmt.Fprintf(os.Stderr, "Defaults are:\n")
	flag.PrintDefaults()
	os.Exit(2)
}

func printDebug(message string)  {
	if *debug {
		fmt.Printf("[\x1b[33;1mDEBUG\x1b[0m] %s\n", message)
	}
}

func main() {
	flag.Usage = usage
	flag.Parse()

	if flag.NFlag() == 0 {
		flag.Usage()
	}

	printDebug("Requesting evaluation...")
	code, _ := ioutil.ReadAll(os.Stdin)
	e := Evaluation{
		Language: *language,
		Code:     string(code),
	}

	result, statusCode := evaluate(e)

	if statusCode != 200 {
		fmt.Printf("[\x1b[31;1mError\x1b[0m] HTTP Code %d\n", statusCode)
	} else {
		fmt.Println(string(result))
	}
}

type Evaluation struct {
	Language string `json:"language"`
	Code     string `json:"code"`
}

func evaluate(e Evaluation) (string, int) {
	jsonQuery, _ := json.Marshal(e)
	query := bytes.NewBuffer(jsonQuery)
	parsedServer, _ := url.Parse(*server)
	printDebug(parsedServer.String())
	resp, _ := http.Post(parsedServer.String(), "application/json", query)
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	return string(body), resp.StatusCode
}<commit_msg>Add much more error handling and debugging.<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
)

var (
	action   = flag.String("action", "evaluate", "What should I do?")
	language = flag.String("language", "", "Language to evaluate.")
	debug    = flag.Bool("debug", false, "Print debugging output?")
	server   = flag.String("server", "http://eval.so/jsontest", "API server to evaluate against.")
)

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: evalso [flags]\n")
	fmt.Fprintf(os.Stderr, "Defaults are:\n")
	flag.PrintDefaults()
	os.Exit(2)
}

func printDebug(message string) {
	if *debug {
		log.Printf("[\x1b[33;1mDEBUG\x1b[0m] %s\n", message)
	}
}

func printFatal(message string) {
	log.Fatal(fmt.Sprintf("[\x1b[31;1mERROR\x1b[0m] %s\n", message))
}

func printOkay(message string) {
	log.Printf("[\x1b[32;1mOKAY\x1b[0m] %s\n", message)
}

func main() {
	flag.Usage = usage
	flag.Parse()

	if flag.NFlag() == 0 {
		flag.Usage()
	}

	printDebug("Requesting evaluation...")
	code, _ := ioutil.ReadAll(os.Stdin)
	e := Evaluation{
		Language: *language,
		Code:     string(code),
	}

	result, statusCode := evaluate(e)

	if statusCode != 200 {
		fmt.Printf("[\x1b[31;1mError\x1b[0m] HTTP Code %d\n", statusCode)
	} else {
		fmt.Println(string(result))
	}
}

type Evaluation struct {
	Language string `json:"language"`
	Code     string `json:"code"`
}

func evaluate(e Evaluation) (string, int) {
	jsonQuery, err := json.Marshal(e)
	if err != nil {
		printFatal("Error converting request into valid JSON.")
	}
	printDebug(fmt.Sprintf("JSON Query: %s", jsonQuery))

	query := bytes.NewBuffer(jsonQuery)

	parsedServer, err := url.Parse(*server)
	if err != nil {
		printFatal("Could not parse given server URL.")
	}

	resp, err := http.Post(parsedServer.String(), "application/json", query)
	if err != nil {
		printFatal("Could not successfully POST the query to the server.")
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	if err != nil {
		printFatal("Could not read the response from the server.")
	}

	return string(body), resp.StatusCode
}
{'fraction_non_alphanumeric': 0.17423014586709887, 'fraction_numerical': 0.01242571582928147, 'mean_word_length': 4.222849083215797, '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': '6215077', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1389, 'n_words': 308}