<commit_before>package env

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"

	"github.com/zouyx/agollo/v3/component/log"
	jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
)

const suffix = ".json"

var (
	configFileMap  = make(map[string]string, 1)
	jsonFileConfig = &jsonConfig.ConfigFile{}
)

//WriteWithRaw
func WriteWithRaw(f func(config *ApolloConfig, configPath string) error) func(config *ApolloConfig, configPath string) error {
	return func(config *ApolloConfig, configPath string) error {
		filePath := fmt.Sprintf("%s/%s", configPath, config.NamespaceName)
		file, e := os.Create(filePath)
		if e != nil {
			return e
		}
		defer file.Close()
		_, e = file.WriteString(config.Configurations["content"])
		if e != nil {
			return e
		}

		return f(config, configPath)
	}
}

//WriteConfigFile write config to file
func WriteConfigFile(config *ApolloConfig, configPath string) error {
	return jsonFileConfig.Write(config, GetConfigFile(configPath, config.NamespaceName))
}

//GetConfigFile get real config file
func GetConfigFile(configDir string, namespace string) string {
	fullPath := configFileMap[namespace]<commit_msg>修正代码注释<commit_after>package env

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"

	"github.com/zouyx/agollo/v3/component/log"
	jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
)

const suffix = ".json"

var (
	configFileMap  = make(map[string]string, 1)
	jsonFileConfig = &jsonConfig.ConfigFile{}
)

//WriteWithRaw decorator for WriteConfigFile
func WriteWithRaw(f func(config *ApolloConfig, configPath string) error) func(config *ApolloConfig, configPath string) error {
	return func(config *ApolloConfig, configPath string) error {
		filePath := fmt.Sprintf("%s/%s", configPath, config.NamespaceName)
		file, e := os.Create(filePath)
		if e != nil {
			return e
		}
		defer file.Close()
		_, e = file.WriteString(config.Configurations["content"])
		if e != nil {
			return e
		}

		return f(config, configPath)
	}
}

//WriteConfigFile write config to file
func WriteConfigFile(config *ApolloConfig, configPath string) error {
	return jsonFileConfig.Write(config, GetConfigFile(configPath, config.NamespaceName))
}

//GetConfigFile get real config file
func GetConfigFile(configDir string, namespace string) string {
	fullPath := configFileMap[namespace]
{'fraction_non_alphanumeric': 0.1190786614515428, 'fraction_numerical': 0.002607561929595828, 'mean_word_length': 4.994791666666667, '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': '2911617', 'n_tokens_mistral': 811, 'n_tokens_neox': 764, 'n_words': 182}