github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/config_paths.go (about)

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