github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/okgo/checkoutput/line.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package checkoutput
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/palantir/pkg/pkgpath"
    21  	"github.com/pkg/errors"
    22  )
    23  
    24  type Issue struct {
    25  	path    string
    26  	line    int
    27  	column  int
    28  	message string
    29  	baseDir string
    30  }
    31  
    32  func (i Issue) Path(pathType pkgpath.Type) (string, error) {
    33  	if pathType == pkgpath.Relative {
    34  		return i.path, nil
    35  	}
    36  
    37  	relPath := pkgpath.NewRelPkgPath(i.path, i.baseDir)
    38  	switch pathType {
    39  	case pkgpath.Absolute:
    40  		return relPath.Abs(), nil
    41  	case pkgpath.GoPathSrcRelative:
    42  		return relPath.GoPathSrcRel()
    43  	default:
    44  		return "", errors.Errorf("unhandled type: %v", pathType)
    45  	}
    46  }
    47  
    48  func (i Issue) Message() string {
    49  	return i.message
    50  }
    51  
    52  func (i Issue) BaseDir() string {
    53  	return i.baseDir
    54  }
    55  
    56  func (i Issue) String() string {
    57  	filePart := ""
    58  	if i.path != "" || i.line != 0 || i.column != 0 {
    59  		// only include filePart if path, line or column is a non-default value
    60  		filePart = fmt.Sprintf("%v:%v", i.path, i.line)
    61  		if i.column != 0 {
    62  			filePart = fmt.Sprintf("%v:%v", filePart, i.column)
    63  		}
    64  		filePart += ": "
    65  	}
    66  	return filePart + i.message
    67  }