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

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  type ConfigurationPaths struct {
    11  	ConfigDir          string `yaml:"config_dir"`
    12  	DataDir            string `yaml:"data_dir,omitempty"`
    13  	SimulationFilePath string `yaml:"simulation_path,omitempty"`
    14  	HubIndexFile       string `yaml:"index_path,omitempty"` //path of the .index.json
    15  	HubDir             string `yaml:"hub_dir,omitempty"`
    16  }
    17  
    18  func (c *Config) LoadConfigurationPaths() error {
    19  	var err error
    20  	if c.ConfigPaths == nil {
    21  		return fmt.Errorf("no configuration paths provided")
    22  	}
    23  
    24  	if c.ConfigPaths.DataDir == "" {
    25  		return fmt.Errorf("please provide a data directory with the 'data_dir' directive in the 'config_paths' section")
    26  	}
    27  
    28  	if c.ConfigPaths.HubDir == "" {
    29  		c.ConfigPaths.HubDir = filepath.Clean(c.ConfigPaths.ConfigDir + "/hub")
    30  	}
    31  
    32  	if c.ConfigPaths.HubIndexFile == "" {
    33  		c.ConfigPaths.HubIndexFile = filepath.Clean(c.ConfigPaths.HubDir + "/.index.json")
    34  	}
    35  
    36  	var configPathsCleanup = []*string{
    37  		&c.ConfigPaths.HubDir,
    38  		&c.ConfigPaths.HubIndexFile,
    39  		&c.ConfigPaths.ConfigDir,
    40  		&c.ConfigPaths.DataDir,
    41  		&c.ConfigPaths.SimulationFilePath,
    42  	}
    43  	for _, k := range configPathsCleanup {
    44  		if *k == "" {
    45  			continue
    46  		}
    47  		*k, err = filepath.Abs(*k)
    48  		if err != nil {
    49  			return errors.Wrapf(err, "failed to get absolute path of '%s'", *k)
    50  		}
    51  	}
    52  
    53  	return nil
    54  }