github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/utils.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/crowdsecurity/crowdsec/pkg/types"
    12  )
    13  
    14  func printHelp(cmd *cobra.Command) {
    15  	if err := cmd.Help(); err != nil {
    16  		log.Fatalf("unable to print help(): %s", err)
    17  	}
    18  }
    19  
    20  func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {
    21  	/*if a range is provided, change the scope*/
    22  	if *ipRange != "" {
    23  		_, _, err := net.ParseCIDR(*ipRange)
    24  		if err != nil {
    25  			return fmt.Errorf("%s isn't a valid range", *ipRange)
    26  		}
    27  	}
    28  
    29  	if *ip != "" {
    30  		ipRepr := net.ParseIP(*ip)
    31  		if ipRepr == nil {
    32  			return fmt.Errorf("%s isn't a valid ip", *ip)
    33  		}
    34  	}
    35  
    36  	// avoid confusion on scope (ip vs Ip and range vs Range)
    37  	switch strings.ToLower(*scope) {
    38  	case "ip":
    39  		*scope = types.Ip
    40  	case "range":
    41  		*scope = types.Range
    42  	case "country":
    43  		*scope = types.Country
    44  	case "as":
    45  		*scope = types.AS
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func removeFromSlice(val string, slice []string) []string {
    52  	var i int
    53  	var value string
    54  
    55  	valueFound := false
    56  
    57  	// get the index
    58  	for i, value = range slice {
    59  		if value == val {
    60  			valueFound = true
    61  			break
    62  		}
    63  	}
    64  
    65  	if valueFound {
    66  		slice[i] = slice[len(slice)-1]
    67  		slice[len(slice)-1] = ""
    68  		slice = slice[:len(slice)-1]
    69  	}
    70  
    71  	return slice
    72  }