<commit_before>// Copyright 2015 Reborndb Org. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strconv"

	docopt "github.com/docopt/docopt-go"
	log "github.com/ngaut/logging"
)

var (
	cpus            = 2
	addr            = ":39000"
	dataDir         = "./var/data"
	logDir          = "./var/log"
	configFile      = "config.ini"
	qdbConfigFile   = "qdb.toml"
	redisConfigFile = "redis.conf"
)

var usage = `usage: reborn-agent [options]

options:
    --addr=<listen_addr>           agent http listen address, example: 0.0.0.0:29000
    --data-dir=<data_dir>          directory to store important data
    --log-dir=<app_log_dir>        directory to store log 
    -L <log_file>                  set output log file, default is stdout
    --log-level=<loglevel>         set log level: info, warn, error, debug [default: info]
    --cpu=<cpu_num>                num of cpu cores that reborn can use
    --exec-path=<exec_path>        execution path which we can find reborn-* cmds
    -c <config_file>               base environment config for reborn config and proxy
    --qdb-config=<qdb_config>      base qdb config 
    --redis-config=<redis_config>  base redis config for reborn-server
`

func getStringArg(args map[string]interface{}, key string) string {
	if v := args[key]; v != nil {
		return v.(string)
	} else {
		return ""
	}
}

func setStringFromOpt(dest *string, args map[string]interface{}, key string) {
	v := getStringArg(args, key)
	if len(v) > 0 {
		*dest = v
	}
}

func main() {
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn agent v0.1", true)
	if err != nil {
		log.Fatal(err)
	}

	if v := getStringArg(args, "-c"); len(v) > 0 {
		configFile = v
	}

	if v := getStringArg(args, "--qdb-config"); len(v) > 0 {
		qdbConfigFile = v
	}

	if v := getStringArg(args, "--redis-config"); len(v) > 0 {
		redisConfigFile = v
	}

	if v := getStringArg(args, "--exec-path"); len(v) > 0 {
		path := os.ExpandEnv(fmt.Sprintf("${PATH}:%s", v))
		os.Setenv("PATH", path)
	}

	// set output log file
	if v := getStringArg(args, "-L"); len(v) > 0 {
		log.SetOutputByName(v)
	}

	// set log level
	if v := getStringArg(args, "--log-level"); len(v) > 0 {
		log.SetLevelByString(v)
	}

	// set cpu
	if v := getStringArg(args, "--cpu"); len(v) > 0 {
		cpus, err = strconv.Atoi(v)
		if err != nil {
			log.Fatal(err)
		}<commit_msg>use absolute path for config files<commit_after>// Copyright 2015 Reborndb Org. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strconv"

	docopt "github.com/docopt/docopt-go"
	log "github.com/ngaut/logging"
)

var (
	cpus            = 2
	addr            = ":39000"
	dataDir         = "./var/data"
	logDir          = "./var/log"
	configFile      = "config.ini"
	qdbConfigFile   = "qdb.toml"
	redisConfigFile = "redis.conf"
)

var usage = `usage: reborn-agent [options]

options:
    --addr=<listen_addr>           agent http listen address, example: 0.0.0.0:39000
    --data-dir=<data_dir>          directory to store important data
    --log-dir=<app_log_dir>        directory to store log 
    -L <log_file>                  set output log file, default is stdout
    --log-level=<loglevel>         set log level: info, warn, error, debug [default: info]
    --cpu=<cpu_num>                num of cpu cores that reborn can use
    --exec-path=<exec_path>        execution path which we can find reborn-* cmds
    -c <config_file>               base environment config for reborn config and proxy
    --qdb-config=<qdb_config>      base qdb config 
    --redis-config=<redis_config>  base redis config for reborn-server
`

func getStringArg(args map[string]interface{}, key string) string {
	if v := args[key]; v != nil {
		return v.(string)
	} else {
		return ""
	}
}

func setStringFromOpt(dest *string, args map[string]interface{}, key string) {
	v := getStringArg(args, key)
	if len(v) > 0 {
		*dest = v
	}
}

func main() {
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn agent v0.1", true)
	if err != nil {
		log.Fatal(err)
	}

	setStringFromOpt(&configFile, args, "-c")
	if configFile, err = filepath.Abs(configFile); err != nil {
		log.Fatal(err)
	}

	setStringFromOpt(&configFile, args, "--qdb-config")
	if qdbConfigFile, err = filepath.Abs(configFile); err != nil {
		log.Fatal(err)
	}

	setStringFromOpt(&redisConfigFile, args, "--redis-config")
	if redisConfigFile, err = filepath.Abs(redisConfigFile); err != nil {
		log.Fatal(err)
	}

	if v := getStringArg(args, "--exec-path"); len(v) > 0 {
		path := os.ExpandEnv(fmt.Sprintf("${PATH}:%s", v))
		os.Setenv("PATH", path)
	}

	// set output log file
	if v := getStringArg(args, "-L"); len(v) > 0 {
		log.SetOutputByName(v)
	}

	// set log level
	if v := getStringArg(args, "--log-level"); len(v) > 0 {
		log.SetLevelByString(v)
	}

	// set cpu
	if v := getStringArg(args, "--cpu"); len(v) > 0 {
		cpus, err = strconv.Atoi(v)
		if err != nil {
			log.Fatal(err)
		}
{'fraction_non_alphanumeric': 0.15098767846665362, 'fraction_numerical': 0.010756894191277136, 'mean_word_length': 2.7658321060382915, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7083717', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1860, 'n_words': 489}