github.com/crowdsecurity/crowdsec@v1.6.1/pkg/cwhub/iteminstall.go (about)

     1  package cwhub
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // enable enables the item by creating a symlink to the downloaded content, and also enables sub-items.
     8  func (i *Item) enable() error {
     9  	if i.State.Installed {
    10  		if i.State.Tainted {
    11  			return fmt.Errorf("%s is tainted, won't enable unless --force", i.Name)
    12  		}
    13  
    14  		if i.State.IsLocal() {
    15  			return fmt.Errorf("%s is local, won't enable", i.Name)
    16  		}
    17  
    18  		// if it's a collection, check sub-items even if the collection file itself is up-to-date
    19  		if i.State.UpToDate && !i.HasSubItems() {
    20  			i.hub.logger.Tracef("%s is installed and up-to-date, skip.", i.Name)
    21  			return nil
    22  		}
    23  	}
    24  
    25  	for _, sub := range i.SubItems() {
    26  		if err := sub.enable(); err != nil {
    27  			return fmt.Errorf("while installing %s: %w", sub.Name, err)
    28  		}
    29  	}
    30  
    31  	if err := i.createInstallLink(); err != nil {
    32  		return err
    33  	}
    34  
    35  	i.hub.logger.Infof("Enabled %s: %s", i.Type, i.Name)
    36  	i.State.Installed = true
    37  
    38  	return nil
    39  }
    40  
    41  // Install installs the item from the hub, downloading it if needed.
    42  func (i *Item) Install(force bool, downloadOnly bool) error {
    43  	if downloadOnly && i.State.Downloaded && i.State.UpToDate {
    44  		i.hub.logger.Infof("%s is already downloaded and up-to-date", i.Name)
    45  
    46  		if !force {
    47  			return nil
    48  		}
    49  	}
    50  
    51  	filePath, err := i.downloadLatest(force, true)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	if downloadOnly {
    57  		i.hub.logger.Infof("Downloaded %s to %s", i.Name, filePath)
    58  		return nil
    59  	}
    60  
    61  	if err := i.enable(); err != nil {
    62  		return fmt.Errorf("while enabling %s: %w", i.Name, err)
    63  	}
    64  
    65  	i.hub.logger.Infof("Enabled %s", i.Name)
    66  
    67  	return nil
    68  }