github.com/daixiang0/gci@v0.13.4/pkg/section/prefix.go (about)

     1  package section
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/daixiang0/gci/pkg/parse"
     8  	"github.com/daixiang0/gci/pkg/specificity"
     9  )
    10  
    11  type Custom struct {
    12  	Prefix string
    13  }
    14  
    15  // CustomSeparator allows you to group multiple custom prefix together in the same section
    16  // gci diff -s standard -s default -s prefix(github.com/company,gitlab.com/company,companysuffix)
    17  const CustomSeparator = ","
    18  
    19  const CustomType = "custom"
    20  
    21  func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
    22  	for _, prefix := range strings.Split(c.Prefix, CustomSeparator) {
    23  		prefix = strings.TrimSpace(prefix)
    24  		if strings.HasPrefix(spec.Path, prefix) {
    25  			return specificity.Match{Length: len(prefix)}
    26  		}
    27  	}
    28  
    29  	return specificity.MisMatch{}
    30  }
    31  
    32  func (c Custom) String() string {
    33  	return fmt.Sprintf("prefix(%s)", c.Prefix)
    34  }
    35  
    36  func (c Custom) Type() string {
    37  	return CustomType
    38  }