<commit_before>package config

import (
	"github.com/vaughan0/go-ini"
	"log"
	"os"
	"path"
	"strings"
)

const defaultConfigIni = `
meta_dsn = postgres://imsto@localhost/imsto?sslmode=disable
meta_table_suffix = demo
engine = s3
bucket_name = imsto-demo
max_quality = 88
thumb_path = /thumb
thumb_root = /opt/imsto/cache/thumb/
tmp_dir = /tmp/
support_size = 120,160,400
`

// var once sync.Once

var (
	confDir       string
	defaultConfig ini.File
	loadedConfig  ini.File
)

func GetConfDir() string {
	return confDir
}

func SetConfDir(dir string) {

	if _, err := os.Stat(dir); err != nil {
		log.Println(err)
	}

	confDir = dir
}

func init() {
	defaultConfig, _ = ini.Load(strings.NewReader(defaultConfigIni))

	confDir = os.Getenv("IMSTO_CONF_DIR")
	if confDir == "" {
		log.Println("env IMSTO_CONF_DIR not found")
		confDir, _ = os.Getwd()
		// panic(errors.New("env IMSTO_CONF_DIR not found"))
	}

	if confDir != "" {
		LoadConfig(confDir)
	}

}

func GetValue(section, name string) string {
	var (
		value string
		ok    bool
	)

	value, ok = loadedConfig.Get(section, name)
	if !ok {
		value, ok = defaultConfig.Get("", name)
		if !ok {
			log.Printf("'%v' variable missing from '%v' section", name, section)
			return ""
		}
	}

	return value
}

func GetSection(sname string) (section ini.Section) {
	if section = loadedConfig.Section(sname); len(section) > 0 {
		return section
	}

	return defaultConfig.Section("")
}

func Sections() []string {
	a := []string{}
	for name, _ := range loadedConfig {
		if name != "common" {
			a = append(a, name)
		}
	}
	if len(a) == 0 {
		a = append(a, "default")
	}
	return a
}

func LoadConfig(dir string) error {<commit_msg>set default config section is common<commit_after>package config

import (
	"github.com/vaughan0/go-ini"
	"log"
	"os"
	"path"
	"strings"
)

const defaultConfigIni = `[common]
meta_dsn = postgres://imsto@localhost/imsto?sslmode=disable
meta_table_suffix = demo
engine = s3
bucket_name = imsto-demo
max_quality = 88
thumb_path = /thumb
thumb_root = /opt/imsto/cache/thumb/
tmp_dir = /tmp/
support_size = 120,160,400
`

// var once sync.Once

var (
	confDir       string
	defaultConfig ini.File
	loadedConfig  ini.File
)

func GetConfDir() string {
	return confDir
}

func SetConfDir(dir string) {

	if _, err := os.Stat(dir); err != nil {
		log.Println(err)
	}

	confDir = dir
}

func init() {
	defaultConfig, _ = ini.Load(strings.NewReader(defaultConfigIni))

	confDir = os.Getenv("IMSTO_CONF_DIR")
	if confDir == "" {
		log.Println("env IMSTO_CONF_DIR not found")
		confDir, _ = os.Getwd()
		// panic(errors.New("env IMSTO_CONF_DIR not found"))
	}

	if confDir != "" {
		LoadConfig(confDir)
	}

}

func GetValue(section, name string) string {
	var (
		value string
		ok    bool
	)

	value, ok = loadedConfig.Get(section, name)
	if !ok {
		value, ok = defaultConfig.Get("common", name)
		if !ok {
			log.Printf("'%v' variable missing from '%v' section", name, section)
			return ""
		}
	}

	return value
}

func GetSection(sname string) (section ini.Section) {
	if section = loadedConfig.Section(sname); len(section) > 0 {
		return section
	}

	return defaultConfig.Section("")
}

func Sections() []string {
	a := []string{}
	for name, _ := range loadedConfig {
		if name != "common" {
			a = append(a, name)
		}
	}
	if len(a) == 0 {
		a = append(a, "default")
	}
	return a
}

func LoadConfig(dir string) error {
{'fraction_non_alphanumeric': 0.1400295420974889, 'fraction_numerical': 0.008862629246676515, 'mean_word_length': 3.6639118457300275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4252496', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1277, 'n_words': 304}