<commit_before>// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package util

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"

	yaml "gopkg.in/yaml.v2"

	"github.com/TheThingsNetwork/ttn/core/types"
	"github.com/apex/log"
	"github.com/spf13/viper"
)

const (
	appFilename = "app"
	euiKey      = "eui"
	idKey       = "id"
)

// GetConfigFile returns the location of the configuration file.
// It checks the following (in this order):
// the --config flag
// $XDG_CONFIG_HOME/ttnctl/config.yml (if $XDG_CONFIG_HOME is set)
// $HOME/.ttnctl.yml
func GetConfigFile() string {
	flag := viper.GetString("config")

	xdg := os.Getenv("XDG_CONFIG_HOME")
	if xdg != "" {
		xdg = path.Join(xdg, "ttnctl", "config.yml")
	}

	home := os.Getenv("HOME")
	if home != "" {
		home = path.Join(home, ".ttnctl.yml")
	}

	for _, file := range []string{
		flag,
		xdg,
		home,
	} {
		if len(file) > 0 {
			if _, err := os.Open(file); !os.IsNotExist(err) {
				return file
			}
		}
	}

	return ""
}

// GetDataDir returns the location of the data directory used for
// sotring data.
// It checks the following (in this order):<commit_msg>ttnctl/util.GetConfigFile: refactoring<commit_after>// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package util

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"

	yaml "gopkg.in/yaml.v2"

	"github.com/TheThingsNetwork/ttn/core/types"
	"github.com/apex/log"
	"github.com/spf13/viper"
)

const (
	appFilename = "app"
	euiKey      = "eui"
	idKey       = "id"
)

// GetConfigFile returns the location of the configuration file.
// It checks the following (in this order):
// the --config flag
// $XDG_CONFIG_HOME/ttnctl/config.yml (if $XDG_CONFIG_HOME is set)
// $HOME/.ttnctl.yml
func GetConfigFile() string {
	flag := viper.GetString("config")

	xdg := os.Getenv("XDG_CONFIG_HOME")
	if xdg != "" {
		xdg = path.Join(xdg, "ttnctl", "config.yml")
	}

	home := os.Getenv("HOME")
	if home != "" {
		home = path.Join(home, ".ttnctl.yml")
	}

	for _, file := range []string{
		flag,
		xdg,
		home,
	} {
		if file != "" {
			if _, err := os.Stat(file); err == nil {
				return file
			}
		}
	}

	return ""
}

// GetDataDir returns the location of the data directory used for
// sotring data.
// It checks the following (in this order):
{'fraction_non_alphanumeric': 0.149439601494396, 'fraction_numerical': 0.0062266500622665, 'mean_word_length': 3.0916808149405774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5248458', 'n_tokens_mistral': 978, 'n_tokens_neox': 910, 'n_words': 250}