github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/packages/errors.go (about)

     1  package packages
     2  
     3  import (
     4  	"fmt"
     5  	"go/token"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  //nolint:gomnd
    13  func ParseErrorPosition(pos string) (*token.Position, error) {
    14  	// file:line(<optional>:colon)
    15  	parts := strings.Split(pos, ":")
    16  	if len(parts) == 1 {
    17  		return nil, errors.New("no colons")
    18  	}
    19  
    20  	file := parts[0]
    21  	line, err := strconv.Atoi(parts[1])
    22  	if err != nil {
    23  		return nil, fmt.Errorf("can't parse line number %q: %s", parts[1], err)
    24  	}
    25  
    26  	var column int
    27  	if len(parts) == 3 { // no column
    28  		column, err = strconv.Atoi(parts[2])
    29  		if err != nil {
    30  			return nil, errors.Wrapf(err, "failed to parse column from %q", parts[2])
    31  		}
    32  	}
    33  
    34  	return &token.Position{
    35  		Filename: file,
    36  		Line:     line,
    37  		Column:   column,
    38  	}, nil
    39  }