<commit_before>// Generate Packer templates and associated files for consumption by Packer.
//
// Copyright 2014 <NAME>. All Rights Reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

// Package ranchr is a package for organizing Rancher code. It also contains the package
// level variables and sets up logging.
package app

import (
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"strconv"
	"strings"

	"github.com/BurntSushi/toml"
	jww "github.com/spf13/jwalterweatherman"
)

// supported distros
const (
	UnsupportedDistro Distro = iota
	CentOS
	Debian
	Ubuntu
)

// Distro is the distribution type
type Distro int

var distros = [...]string{
	"unsupported distro",
	"centos",
	"debian",
	"ubuntu",
}

func (d Distro) String() string { return distros[d] }

// DistroFromString returns the Distro constant for the passed string or
// unsupported.
//
// All incoming strings are normalized to lowercase.
func DistroFromString(s string) Distro {
	s = strings.ToLower(s)
	switch s {
	case "centos":
		return CentOS
	case "debian":
		return Debian
	case "ubuntu":
		return Ubuntu
	}
	return UnsupportedDistro
}

// Environment variable name constants
const (
	// EnvRancherFile is the name of the environment variable name for Rancher's config file.
	EnvRancherFile = "RANCHER_CONFIG"
	// EnvBuildsFile is the name of the environment variable name for the builds file.
	EnvBuildsFile = "RANCHER_BUILDS_FILE"
	// EnvBuildListsFile is the name of the environment variable name for the build lists file.
	EnvBuildListsFile = "RANCHER_BUILD_LISTS_FILE"
	// EnvDefaultsFile is the name of the environment variable name for the defaults file.
	EnvDefaultsFile = "RANCHER_DEFAULTS_FILE"
	// EnvSupportedFile is the name of the environment variable name for the supported file.
	EnvSupportedFile = "RANCHER_SUPPORTED_FILE"
	// EnvParamDelimStart is the name of the environment variable name for the delimter that starts Rancher variables.
	EnvParamDelimStart = "RANCHER_PARAM_DELIM_START"
	// EnvLogToFile is the name of the environment variable name for whether or not Rancher logs to a file.
	EnvLogToFile = "RANCHER_LOG_TO_FILE"
	// EnvLogFilename is the name of the environment variable name for the log filename, if logging to file is enabled..
	EnvLogFilename = "RANCHER_LOG_FILENAME"
	// EnvLogLevelFile is the name of the environment variable name for the file output's log level.
	EnvLogLevelFile = "RANCHER_LOG_LEVEL_FILE"
	// EnvLogLevelStdout is the name of the environment variable name for stdout's log level.
	EnvLogLevelStdout = "RANCHER_LOG_LEVEL_STDOUT"
)

// indent: default indent to use for marshal stuff
var indent = "    "

// Defined builds
var Builds *builds

// Defaults for each supported distribution
var DistroDefaults distroDefaults

// AppConfig contains the current Rancher configuration...loaded at start-up.
var AppConfig appConfig

type appConfig struct {
	BuildsFile      string `toml:"builds_file"`
	BuildListsFile  string `toml:"build_lists_file"`
	DefaultsFile    string `toml:"defaults_file"`
	LogToFile       bool   `toml:"log_to_file"`
	LogFilename     string `toml:"log_filename"`
	LogLevelFile    string `toml:"log_level_file"`
	LogLevelStdout  string `toml:"log_level_stdout"`
	ParamDelimStart string `toml:"param_delim_start"`
	SupportedFile   string `toml:"Supported_file"`
}

// ArgsFilter has all the valid commandline flags for the build-subcommand.
type ArgsFilter struct {
	// Arch is a distribution specific string for the OS's target
	// architecture.
	Arch string
	// Distro is the name of the distribution, this value is consistent
	// with Packer.
	Distro string
	// Image is the type of ISO image that is to be used. This is a
	// distribution specific value.
	Image string
	// Release is the release number or string of the ISO that is to be
	// used. The valid values are distribution specific.
	Release string
}

// distroDefaults contains the defaults for all supported distros and a flag
// whether its been set or not.
type distroDefaults struct {
	Templates map[Distro]*rawTemplate
	IsSet     bool
}

// GetTemplate returns a deep copy of the default template for the passed
// distro name. If the distro does not exist, an error is returned.
func (d *distroDefaults) GetTemplate(n string) (*rawTemplate, error) {
	var t *rawTemplate
	var ok bool
	t, ok = d.Templates[DistroFromString(n)]
	if !ok {
		err := fmt.Errorf("unsupported distro: %s", n)
		jww.ERROR.Println(err)
		return t, err
	}
	Copy := newRawTemplate()
	Copy.PackerInf = t.PackerInf
	Copy.IODirInf = t.IODirInf
	Copy.BuildInf = t.BuildInf
	Copy.releaseISO = t.releaseISO
	Copy.date = t.date
	Copy.delim = t.delim
	Copy.Distro = t.Distro
	Copy.Arch = t.Arch
	Copy.Image = t.Image
	Copy.Release = t.Release
	Copy.varVals = t.varVals
	Copy.build = t.build.DeepCopy()
	return Copy, nil
}

// Set sets the default templates for each distro.
func (d *distroDefaults) Set() error {
	dflts := &defaults{}
	err := dflts.LoadOnce()
	if err != nil {
		jww.ERROR.Println(err)
		return err
	}
	s := &supported{}
	err = s.LoadOnce()
	if err != nil {
		jww.ERROR.Println(err)
		return err
	}
	d.Templates = map[Distro]*rawTemplate{}
	// Generate the default settings for each distro.
	for k, v := range s.Distro {
		// See if the base url exists for non cento distros
		// It isn't required for debian because automatic resolution of iso
		// information is not supported.
		if v.BaseURL == "" && k != CentOS.String() {
			err = fmt.Errorf("%s requires a BaseURL, none provided", k)
			jww.CRITICAL.Println(err)
			return err

		}<commit_msg>fix distroDefaults.GetTemplat() to copy the map instead of just copying the map reference<commit_after>// Generate Packer templates and associated files for consumption by Packer.
//
// Copyright 2014 <NAME>. All Rights Reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

// Package ranchr is a package for organizing Rancher code. It also contains the package
// level variables and sets up logging.
package app

import (
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"strconv"
	"strings"

	"github.com/BurntSushi/toml"
	jww "github.com/spf13/jwalterweatherman"
)

// supported distros
const (
	UnsupportedDistro Distro = iota
	CentOS
	Debian
	Ubuntu
)

// Distro is the distribution type
type Distro int

var distros = [...]string{
	"unsupported distro",
	"centos",
	"debian",
	"ubuntu",
}

func (d Distro) String() string { return distros[d] }

// DistroFromString returns the Distro constant for the passed string or
// unsupported.
//
// All incoming strings are normalized to lowercase.
func DistroFromString(s string) Distro {
	s = strings.ToLower(s)
	switch s {
	case "centos":
		return CentOS
	case "debian":
		return Debian
	case "ubuntu":
		return Ubuntu
	}
	return UnsupportedDistro
}

// Environment variable name constants
const (
	// EnvRancherFile is the name of the environment variable name for Rancher's config file.
	EnvRancherFile = "RANCHER_CONFIG"
	// EnvBuildsFile is the name of the environment variable name for the builds file.
	EnvBuildsFile = "RANCHER_BUILDS_FILE"
	// EnvBuildListsFile is the name of the environment variable name for the build lists file.
	EnvBuildListsFile = "RANCHER_BUILD_LISTS_FILE"
	// EnvDefaultsFile is the name of the environment variable name for the defaults file.
	EnvDefaultsFile = "RANCHER_DEFAULTS_FILE"
	// EnvSupportedFile is the name of the environment variable name for the supported file.
	EnvSupportedFile = "RANCHER_SUPPORTED_FILE"
	// EnvParamDelimStart is the name of the environment variable name for the delimter that starts Rancher variables.
	EnvParamDelimStart = "RANCHER_PARAM_DELIM_START"
	// EnvLogToFile is the name of the environment variable name for whether or not Rancher logs to a file.
	EnvLogToFile = "RANCHER_LOG_TO_FILE"
	// EnvLogFilename is the name of the environment variable name for the log filename, if logging to file is enabled..
	EnvLogFilename = "RANCHER_LOG_FILENAME"
	// EnvLogLevelFile is the name of the environment variable name for the file output's log level.
	EnvLogLevelFile = "RANCHER_LOG_LEVEL_FILE"
	// EnvLogLevelStdout is the name of the environment variable name for stdout's log level.
	EnvLogLevelStdout = "RANCHER_LOG_LEVEL_STDOUT"
)

// indent: default indent to use for marshal stuff
var indent = "    "

// Defined builds
var Builds *builds

// Defaults for each supported distribution
var DistroDefaults distroDefaults

// AppConfig contains the current Rancher configuration...loaded at start-up.
var AppConfig appConfig

type appConfig struct {
	BuildsFile      string `toml:"builds_file"`
	BuildListsFile  string `toml:"build_lists_file"`
	DefaultsFile    string `toml:"defaults_file"`
	LogToFile       bool   `toml:"log_to_file"`
	LogFilename     string `toml:"log_filename"`
	LogLevelFile    string `toml:"log_level_file"`
	LogLevelStdout  string `toml:"log_level_stdout"`
	ParamDelimStart string `toml:"param_delim_start"`
	SupportedFile   string `toml:"Supported_file"`
}

// ArgsFilter has all the valid commandline flags for the build-subcommand.
type ArgsFilter struct {
	// Arch is a distribution specific string for the OS's target
	// architecture.
	Arch string
	// Distro is the name of the distribution, this value is consistent
	// with Packer.
	Distro string
	// Image is the type of ISO image that is to be used. This is a
	// distribution specific value.
	Image string
	// Release is the release number or string of the ISO that is to be
	// used. The valid values are distribution specific.
	Release string
}

// distroDefaults contains the defaults for all supported distros and a flag
// whether its been set or not.
type distroDefaults struct {
	Templates map[Distro]*rawTemplate
	IsSet     bool
}

// GetTemplate returns a deep copy of the default template for the passed
// distro name. If the distro does not exist, an error is returned.
func (d *distroDefaults) GetTemplate(n string) (*rawTemplate, error) {
	var t *rawTemplate
	var ok bool
	t, ok = d.Templates[DistroFromString(n)]
	if !ok {
		err := fmt.Errorf("unsupported distro: %s", n)
		jww.ERROR.Println(err)
		return t, err
	}
	Copy := newRawTemplate()
	Copy.PackerInf = t.PackerInf
	Copy.IODirInf = t.IODirInf
	Copy.BuildInf = t.BuildInf
	Copy.releaseISO = t.releaseISO
	Copy.date = t.date
	Copy.delim = t.delim
	Copy.Distro = t.Distro
	Copy.Arch = t.Arch
	Copy.Image = t.Image
	Copy.Release = t.Release
	for k, v := range t.varVals {
		Copy.varVals[k] = v
	}
	Copy.build = t.build.DeepCopy()
	return Copy, nil
}

// Set sets the default templates for each distro.
func (d *distroDefaults) Set() error {
	dflts := &defaults{}
	err := dflts.LoadOnce()
	if err != nil {
		jww.ERROR.Println(err)
		return err
	}
	s := &supported{}
	err = s.LoadOnce()
	if err != nil {
		jww.ERROR.Println(err)
		return err
	}
	d.Templates = map[Distro]*rawTemplate{}
	// Generate the default settings for each distro.
	for k, v := range s.Distro {
		// See if the base url exists for non cento distros
		// It isn't required for debian because automatic resolution of iso
		// information is not supported.
		if v.BaseURL == "" && k != CentOS.String() {
			err = fmt.Errorf("%s requires a BaseURL, none provided", k)
			jww.CRITICAL.Println(err)
			return err

		}
{'fraction_non_alphanumeric': 0.08620391206508568, 'fraction_numerical': 0.0017310022503029255, 'mean_word_length': 3.8530029399412014, 'pattern_counts': {'":': 6, '<': 5, '<?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': '7506561', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3637, 'n_words': 1351}