github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/metadata.go (about)

     1  package parse
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     8  )
     9  
    10  func GetMetadataForParsedResource(resourceName string, srcRange hcl.Range, fileData map[string][]byte, mod *modconfig.Mod) (*modconfig.ResourceMetadata, error) {
    11  	// convert the name into a short name
    12  	parsedName, err := modconfig.ParseResourceName(resourceName)
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  	m := &modconfig.ResourceMetadata{
    17  		ResourceName:     parsedName.Name,
    18  		FileName:         srcRange.Filename,
    19  		StartLineNumber:  srcRange.Start.Line,
    20  		EndLineNumber:    srcRange.End.Line,
    21  		IsAutoGenerated:  false,
    22  		SourceDefinition: getSourceDefinition(srcRange, fileData),
    23  	}
    24  	// update the 'ModName' and 'ModShortName' fields
    25  	m.SetMod(mod)
    26  	return m, nil
    27  }
    28  
    29  func getSourceDefinition(sourceRange hcl.Range, fileData map[string][]byte) string {
    30  	filename := sourceRange.Filename
    31  	fileBytes, ok := fileData[filename]
    32  	if !ok {
    33  		return ""
    34  	}
    35  
    36  	source := strings.Join(
    37  		strings.Split(string(fileBytes), "\n")[sourceRange.Start.Line-1:sourceRange.End.Line], "\n")
    38  	return source
    39  }