<commit_before>package config

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"runtime"
	"sync"
)

type Config struct {
	path          string
	dataMutex     sync.Mutex
	data          map[string]interface{}
	saveChanMutex sync.Mutex
	saveChan      chan struct{}
}

var config *Config
var configLock sync.Mutex

const CONFIG_FILENAME = ".config/plaincast.json"

var disableConfig = flag.Bool("no-config", false, "disable reading from and writing to config file")
var configPath = flag.String("config", "", "config file location (default "+CONFIG_FILENAME+")")

// Get returns a global Config instance.
// It may be called multiple times: the same object will be returned each time.
func Get() *Config {
	configLock.Lock()
	defer configLock.Unlock()

	if config == nil {
		var path = ""

		if *disableConfig {
			// don't set config path

		} else if *configPath != "" {
			// set custom config path
			path = *configPath

		} else {
			// use default config path
			u, err := user.Current()
			handle(err, "could not get current user")

			path := filepath.Join(u.HomeDir, CONFIG_FILENAME)

			err = os.MkdirAll(filepath.Dir(path), 0777)
			handle(err, "could not create parent directories of config file")
		}

		config = newConfig(path)
	}

	return config
}

func newConfig(path string) *Config {
	c := &Config{}
	c.data = make(map[string]interface{})
	c.saveChan = make(chan struct{}, 1)

	if path == "" {
		return c
	}

	c.path = path
<commit_msg>config: Fix bug in default config path<commit_after>package config

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
	"runtime"
	"sync"
)

type Config struct {
	path          string
	dataMutex     sync.Mutex
	data          map[string]interface{}
	saveChanMutex sync.Mutex
	saveChan      chan struct{}
}

var config *Config
var configLock sync.Mutex

const CONFIG_FILENAME = ".config/plaincast.json"

var disableConfig = flag.Bool("no-config", false, "disable reading from and writing to config file")
var configPath = flag.String("config", "", "config file location (default "+CONFIG_FILENAME+")")

// Get returns a global Config instance.
// It may be called multiple times: the same object will be returned each time.
func Get() *Config {
	configLock.Lock()
	defer configLock.Unlock()

	if config == nil {
		var path = ""

		if *disableConfig {
			// don't set config path

		} else if *configPath != "" {
			// set custom config path
			path = *configPath

		} else {
			// use default config path
			u, err := user.Current()
			handle(err, "could not get current user")

			path = filepath.Join(u.HomeDir, CONFIG_FILENAME)

			err = os.MkdirAll(filepath.Dir(path), 0777)
			handle(err, "could not create parent directories of config file")
		}

		config = newConfig(path)
	}

	return config
}

func newConfig(path string) *Config {
	c := &Config{}
	c.data = make(map[string]interface{})
	c.saveChan = make(chan struct{}, 1)

	if path == "" {
		return c
	}

	c.path = path

{'fraction_non_alphanumeric': 0.12425644415069398, 'fraction_numerical': 0.003304692663582287, 'mean_word_length': 3.393323657474601, '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': '6490686', 'n_tokens_mistral': 1082, 'n_tokens_neox': 995, 'n_words': 312}