github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/tfdiags/source_range.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package tfdiags
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  type SourceRange struct {
    13  	Filename   string
    14  	Start, End SourcePos
    15  }
    16  
    17  type SourcePos struct {
    18  	Line, Column, Byte int
    19  }
    20  
    21  // StartString returns a string representation of the start of the range,
    22  // including the filename and the line and column numbers.
    23  func (r SourceRange) StartString() string {
    24  	filename := r.Filename
    25  
    26  	// We'll try to relative-ize our filename here so it's less verbose
    27  	// in the common case of being in the current working directory. If not,
    28  	// we'll just show the full path.
    29  	wd, err := os.Getwd()
    30  	if err == nil {
    31  		relFn, err := filepath.Rel(wd, filename)
    32  		if err == nil {
    33  			filename = relFn
    34  		}
    35  	}
    36  
    37  	return fmt.Sprintf("%s:%d,%d", filename, r.Start.Line, r.Start.Column)
    38  }