<commit_before>package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"github.com/42wim/matterbridge/matterhook"
	"gopkg.in/yaml.v2"
	"io"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"runtime"
	"time"
)

type config struct {
	Username, Channel, Title, Language string
	IconURL                            string `yaml:"icon_url"`
	MatterURL                          string `yaml:"matter_url"`
	PlainText                          bool   `yaml:"plain_text"`
	NoBuffer                           bool   `yaml:"no_buffer"`
	Extra                              bool
}

var cfg = config{
	Channel:   "",
	IconURL:   "",
	Language:  "",
	MatterURL: "",
	Title:     "",
	Username:  "mattertee",
	Extra:     false,
	NoBuffer:  false,
	PlainText: false,
}

func init() {
	// Read configuration from files
	read_configurations()

	// Now override configuration with command line parameters
	flag.StringVar(&cfg.Channel, "c", cfg.Channel, "Post input values to specified channel or user.")
	flag.StringVar(&cfg.IconURL, "i", cfg.IconURL, "This url is used as icon for posting.")
	flag.StringVar(&cfg.Language, "l", cfg.Language, "Specify the language used for syntax highlighting (ruby/python/...)")
	flag.StringVar(&cfg.MatterURL, "m", cfg.MatterURL, "Mattermost incoming webhooks URL.")
	flag.StringVar(&cfg.Title, "t", cfg.Title, "This title is added to posts. (not with -n)")
	flag.StringVar(&cfg.Username, "u", cfg.Username, "This username is used for posting.")
	flag.BoolVar(&cfg.Extra, "x", cfg.Extra, "Add extra info (user/hostname/timestamp).")
	flag.BoolVar(&cfg.NoBuffer, "n", cfg.NoBuffer, "Post input values without buffering.")
	flag.BoolVar(&cfg.PlainText, "p", cfg.PlainText, "Don't surround the post with triple backticks.")
	flag.Parse()
	fmt.Fprintf(os.Stderr, "Configuration: %v\n", cfg)
}

func read_configurations() {
	// config_files will list configuration files which will be read in order and can override
	// previous files
	config_files := []string{}

	if runtime.GOOS == "linux" {
		config_files = append(config_files, "/etc/mattertee.conf")
	}

	usr, err := user.Current()
	if err == nil {
		config_files = append(config_files, filepath.Join(usr.HomeDir, ".mattertee.conf"))
	}

	config_files = append(config_files, ".mattertee.conf")

	for _, file := range config_files {
		err := read_configuration(file)
		if err != nil {
			// something went wrong - report (but don't fail)
			fmt.Fprintf(os.Stderr, "An error has occurred while reading configuration file '%s': %s\n", file, err)
		}<commit_msg>Extend the help message<commit_after>package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"github.com/42wim/matterbridge/matterhook"
	"gopkg.in/yaml.v2"
	"io"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"runtime"
	"time"
)

type config struct {
	Username, Channel, Title, Language string
	IconURL                            string `yaml:"icon_url"`
	MatterURL                          string `yaml:"matter_url"`
	PlainText                          bool   `yaml:"plain_text"`
	NoBuffer                           bool   `yaml:"no_buffer"`
	Extra                              bool
}

var cfg = config{
	Channel:   "",
	IconURL:   "",
	Language:  "",
	MatterURL: "",
	Title:     "",
	Username:  "mattertee",
	Extra:     false,
	NoBuffer:  false,
	PlainText: false,
}

func init() {
	// Read configuration from files
	read_configurations()

	// Now override configuration with command line parameters
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "The following configuration files will also be read:")
		fmt.Fprintf(os.Stderr, " (+ if file is present)\n")
		for _, file := range config_files() {
			if _, err := os.Stat(file); os.IsNotExist(err) {
				fmt.Fprintf(os.Stderr, "  - %s\n", file)
			} else {
				fmt.Fprintf(os.Stderr, "  + %s\n", file)
			}
		}
	}
	flag.StringVar(&cfg.Channel, "c", cfg.Channel, "Post input values to specified channel or user.")
	flag.StringVar(&cfg.IconURL, "i", cfg.IconURL, "This url is used as icon for posting.")
	flag.StringVar(&cfg.Language, "l", cfg.Language, "Specify the language used for syntax highlighting (ruby/python/...)")
	flag.StringVar(&cfg.MatterURL, "m", cfg.MatterURL, "Mattermost incoming webhooks URL.")
	flag.StringVar(&cfg.Title, "t", cfg.Title, "This title is added to posts. (not with -n)")
	flag.StringVar(&cfg.Username, "u", cfg.Username, "This username is used for posting.")
	flag.BoolVar(&cfg.Extra, "x", cfg.Extra, "Add extra info (user/hostname/timestamp).")
	flag.BoolVar(&cfg.NoBuffer, "n", cfg.NoBuffer, "Post input values without buffering.")
	flag.BoolVar(&cfg.PlainText, "p", cfg.PlainText, "Don't surround the post with triple backticks.")
	flag.Parse()
	fmt.Fprintf(os.Stderr, "Configuration: %v\n", cfg)
}

func config_files() []string {
	// config_files will list configuration files which will be read in order and can override
	// previous files
	config_files := []string{}

	if runtime.GOOS == "linux" {
		config_files = append(config_files, "/etc/mattertee.conf")
	}

	usr, err := user.Current()
	if err == nil {
		config_files = append(config_files, filepath.Join(usr.HomeDir, ".mattertee.conf"))
	}

	config_files = append(config_files, ".mattertee.conf")

	return config_files
}

func read_configurations() {
	config_files := config_files()

	for _, file := range config_files {
		err := read_configuration(file)
		if err != nil {
			// something went wrong - report (but don't fail)
			fmt.Fprintf(os.Stderr, "An error has occurred while reading configuration file '%s': %s\n", file, err)
		}
{'fraction_non_alphanumeric': 0.1451986460003563, 'fraction_numerical': 0.0012471049349723855, 'mean_word_length': 3.372274143302181, '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': '6672537', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1883, 'n_words': 503}