bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/simulation.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	"github.com/pkg/errors"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type SimulationConfig struct {
    13  	Simulation *bool    `yaml:"simulation"`
    14  	Exclusions []string `yaml:"exclusions,omitempty"`
    15  }
    16  
    17  func (s *SimulationConfig) IsSimulated(scenario string) bool {
    18  	var simulated bool
    19  
    20  	if s.Simulation != nil && *s.Simulation {
    21  		simulated = true
    22  	}
    23  	for _, excluded := range s.Exclusions {
    24  		if excluded == scenario {
    25  			simulated = !simulated
    26  			break
    27  		}
    28  	}
    29  	return simulated
    30  }
    31  
    32  func (c *Config) LoadSimulation() error {
    33  
    34  	if err := c.LoadConfigurationPaths(); err != nil {
    35  		return err
    36  	}
    37  
    38  	simCfg := SimulationConfig{}
    39  	if c.ConfigPaths.SimulationFilePath == "" {
    40  		c.ConfigPaths.SimulationFilePath = filepath.Clean(c.ConfigPaths.ConfigDir + "/simulation.yaml")
    41  	}
    42  	rcfg, err := ioutil.ReadFile(c.ConfigPaths.SimulationFilePath)
    43  	if err != nil {
    44  		return errors.Wrapf(err, "while reading '%s'", c.ConfigPaths.SimulationFilePath)
    45  	} else {
    46  		if err := yaml.UnmarshalStrict(rcfg, &simCfg); err != nil {
    47  			return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
    48  		}
    49  	}
    50  	if simCfg.Simulation == nil {
    51  		simCfg.Simulation = new(bool)
    52  	}
    53  	if c.Synsec != nil {
    54  		c.Synsec.SimulationConfig = &simCfg
    55  	}
    56  	if c.Cscli != nil {
    57  		c.Cscli.SimulationConfig = &simCfg
    58  	}
    59  	return nil
    60  }