github.com/nozzle/golangci-lint@v1.49.0-nz3/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  func ParseErrorPosition(pos string) (*token.Position, error) {
    13  	// file:line(<optional>:colon)
    14  	parts := strings.Split(pos, ":")
    15  	if len(parts) == 1 {
    16  		return nil, errors.New("no colons")
    17  	}
    18  
    19  	file := parts[0]
    20  	line, err := strconv.Atoi(parts[1])
    21  	if err != nil {
    22  		return nil, fmt.Errorf("can't parse line number %q: %s", parts[1], err)
    23  	}
    24  
    25  	var column int
    26  	if len(parts) == 3 { // no column
    27  		column, err = strconv.Atoi(parts[2])
    28  		if err != nil {
    29  			return nil, errors.Wrapf(err, "failed to parse column from %q", parts[2])
    30  		}
    31  	}
    32  
    33  	return &token.Position{
    34  		Filename: file,
    35  		Line:     line,
    36  		Column:   column,
    37  	}, nil
    38  }