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