<commit_before>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

// Package env provides runtime, server level setup and configuration
package env

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/BurntSushi/toml"
)

// LoadConfig loads runtime parameters like port numbers and DB connections.
// We first check for -config switch that would point us towards a .CONF file.
// If not found, we then read parameters from command line and environment vars.
func LoadConfig() (f Flags, ok bool) {
	// Check and process config file
	f, ok = configFile()

	// If not OK then get parameters from command line and environment variables.
	if !ok {
		f, ok = commandLineEnv()
	}

	// reserved
	if len(f.Location) == 0 {
		f.Location = "selfhost"
	}

	return
}

// configFile checks for the presence of zero or one command argument.
// If no arguments are provided then we look for and load documize.conf file.
// If one argument is provided then we load the specified config file.
// If more than one argument is provided then we exit as flags as have passed.
// checks to see if it is a TOML format config file.
func configFile() (f Flags, ok bool) {
	ok = false
	var configFile string

	// First argument is always program being executed.
	// No additional arguments means check for documize.conf file.
	if len(os.Args) == 1 {
		// No arguments, so we default to default config filename.
		configFile = "documize.conf"
	} else if len(os.Args) == 2 {
		// Config filename passed in, so we use it.
		configFile = os.Args[1]
	} else {
		// Too many arguments means flags passed in so we return.
		return
	}

	// Does file exist?
	if len(configFile) == 0 || !configFileExists(configFile) {
		return
	}

	// Tell caller where the config came from.
	f.ConfigSource = configFile

	// We parse the TOML format config file.
	var ct ConfigToml
	if _, err := toml.DecodeFile(configFile, &ct); err != nil {
		fmt.Println(err)
		return
	}
	f.DBType = strings.ToLower(ct.Database.Type)
	f.DBConn = ct.Database.Connection
	f.Salt = ct.Database.Salt
	f.HTTPPort = strconv.Itoa(ct.HTTP.Port)
	f.ForceHTTPPort2SSL = strconv.Itoa(ct.HTTP.ForceSSLPort)
	f.SSLCertFile = ct.HTTP.Cert
	f.SSLKeyFile = ct.HTTP.Key
	f.TLSVersion = ct.HTTP.TLSVersion
	f.Location = strings.ToLower(ct.Install.Location)

	if len(f.TLSVersion) == 0 {
		f.TLSVersion = "1.2"
	}

	ok = true
	return
}

// commandLineEnv loads command line and OS environment variables required by the program to function.
func commandLineEnv() (f Flags, ok bool) {
	ok = true
	var dbConn, dbType, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL, TLSVersion, location string

	// register(&configFile, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
	register(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
	register(&certFile, "cert", false, "the cert.pem file used for https")
	register(&keyFile, "key", false, "the key.pem file used for https")
	register(&port, "port", false, "http/https port number")
	register(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS")
	register(&TLSVersion, "tlsversion", false, "select minimum TLS: 1.0, 1.1, 1.2, 1.3")
	register(&siteMode, "offline", false, "set to '1' for OFFLINE mode")
	register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgresql|sqlserver")
	register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
	register(&location, "location", false, `reserved`)

	if !parse("db") {
		ok = false
	}

	f.DBType = strings.ToLower(dbType)
	f.DBConn = dbConn<commit_msg>Set TLS min to 1.3<commit_after>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

// Package env provides runtime, server level setup and configuration
package env

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/BurntSushi/toml"
)

// LoadConfig loads runtime parameters like port numbers and DB connections.
// We first check for -config switch that would point us towards a .CONF file.
// If not found, we then read parameters from command line and environment vars.
func LoadConfig() (f Flags, ok bool) {
	// Check and process config file
	f, ok = configFile()

	// If not OK then get parameters from command line and environment variables.
	if !ok {
		f, ok = commandLineEnv()
	}

	// reserved
	if len(f.Location) == 0 {
		f.Location = "selfhost"
	}

	return
}

// configFile checks for the presence of zero or one command argument.
// If no arguments are provided then we look for and load documize.conf file.
// If one argument is provided then we load the specified config file.
// If more than one argument is provided then we exit as flags as have passed.
// checks to see if it is a TOML format config file.
func configFile() (f Flags, ok bool) {
	ok = false
	var configFile string

	// First argument is always program being executed.
	// No additional arguments means check for documize.conf file.
	if len(os.Args) == 1 {
		// No arguments, so we default to default config filename.
		configFile = "documize.conf"
	} else if len(os.Args) == 2 {
		// Config filename passed in, so we use it.
		configFile = os.Args[1]
	} else {
		// Too many arguments means flags passed in so we return.
		return
	}

	// Does file exist?
	if len(configFile) == 0 || !configFileExists(configFile) {
		return
	}

	// Tell caller where the config came from.
	f.ConfigSource = configFile

	// We parse the TOML format config file.
	var ct ConfigToml
	if _, err := toml.DecodeFile(configFile, &ct); err != nil {
		fmt.Println(err)
		return
	}
	f.DBType = strings.ToLower(ct.Database.Type)
	f.DBConn = ct.Database.Connection
	f.Salt = ct.Database.Salt
	f.HTTPPort = strconv.Itoa(ct.HTTP.Port)
	f.ForceHTTPPort2SSL = strconv.Itoa(ct.HTTP.ForceSSLPort)
	f.SSLCertFile = ct.HTTP.Cert
	f.SSLKeyFile = ct.HTTP.Key
	f.TLSVersion = ct.HTTP.TLSVersion
	f.Location = strings.ToLower(ct.Install.Location)

	if len(f.TLSVersion) == 0 {
		f.TLSVersion = "1.3"
	}

	ok = true
	return
}

// commandLineEnv loads command line and OS environment variables required by the program to function.
func commandLineEnv() (f Flags, ok bool) {
	ok = true
	var dbConn, dbType, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL, TLSVersion, location string

	// register(&configFile, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
	register(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
	register(&certFile, "cert", false, "the cert.pem file used for https")
	register(&keyFile, "key", false, "the key.pem file used for https")
	register(&port, "port", false, "http/https port number")
	register(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS")
	register(&TLSVersion, "tlsversion", false, "select minimum TLS: 1.0, 1.1, 1.2, 1.3")
	register(&siteMode, "offline", false, "set to '1' for OFFLINE mode")
	register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgresql|sqlserver")
	register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
	register(&location, "location", false, `reserved`)

	if !parse("db") {
		ok = false
	}

	f.DBType = strings.ToLower(dbType)
	f.DBConn = dbConn
{'fraction_non_alphanumeric': 0.11327980234712785, 'fraction_numerical': 0.007906114885731933, 'mean_word_length': 3.859543817527011, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2835842', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2556, 'n_words': 963}