go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/lr/docs/docs.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package docs
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/resources"
    12  )
    13  
    14  type LrDocs struct {
    15  	Resources map[string]*LrDocsEntry `json:"resources,omitempty"`
    16  }
    17  
    18  type LrDocsEntry struct {
    19  	// Maturity of the resource: experimental, preview, public, deprecated
    20  	// default maturity is public if nothing is provided
    21  	Maturity string `json:"maturity,omitempty"`
    22  	// this is just an indicator, we may want to replace this with native MQL resource platform information
    23  	Platform         *LrDocsPlatform         `json:"platform,omitempty"`
    24  	Docs             *LrDocsDocumentation    `json:"docs,omitempty"`
    25  	Resources        []LrDocsRefs            `json:"resources,omitempty"`
    26  	Fields           map[string]*LrDocsField `json:"fields,omitEmpty"`
    27  	Refs             []LrDocsRefs            `json:"refs,omitempty"`
    28  	Snippets         []LrDocsSnippet         `json:"snippets,omitempty"`
    29  	IsPrivate        bool                    `json:"is_private,omitempty"`
    30  	MinMondooVersion string                  `json:"min_mondoo_version,omitempty"`
    31  }
    32  
    33  type LrDocsPlatform struct {
    34  	Name   []string `json:"name,omitempty"`
    35  	Family []string `json:"family,omitempty"`
    36  }
    37  
    38  func (d LrDocsPlatform) MarshalGo() string {
    39  	var sb strings.Builder
    40  	if len(d.Name) > 0 {
    41  		sb.WriteString("Name: []string{\"" + strings.Join(d.Name, "\",\"") + "\"},")
    42  	}
    43  
    44  	if len(d.Family) > 0 {
    45  		sb.WriteString("Family: []string{\"" + strings.Join(d.Family, "\",\"") + "\"},")
    46  	}
    47  
    48  	return sb.String()
    49  }
    50  
    51  type LrDocsDocumentation struct {
    52  	Description string `json:"desc,omitempty"`
    53  }
    54  
    55  func (d LrDocsDocumentation) MarshalGo() string {
    56  	return fmt.Sprintf("Description: " + strconv.Quote(d.Description) + ",\n")
    57  }
    58  
    59  type LrDocsRefs struct {
    60  	Title string `json:"title,omitempty"`
    61  	Url   string `json:"url,omitempty"`
    62  }
    63  
    64  type LrDocsField struct {
    65  	MinMondooVersion string `json:"min_mondoo_version,omitempty"`
    66  }
    67  
    68  func (d LrDocsRefs) MarshalGo() string {
    69  	var sb strings.Builder
    70  	sb.WriteString("Title: " + strconv.Quote(d.Title) + ",\n")
    71  	sb.WriteString("Url: " + strconv.Quote(d.Url) + ",\n")
    72  	return sb.String()
    73  }
    74  
    75  type LrDocsSnippet struct {
    76  	Title string `json:"title,omitempty"`
    77  	Query string `json:"query,omitempty"`
    78  }
    79  
    80  func InjectMetadata(schema *resources.Schema, docs *LrDocs) {
    81  	for resource, rdoc := range docs.Resources {
    82  		info, ok := schema.Resources[resource]
    83  		if !ok {
    84  			continue
    85  		}
    86  
    87  		info.MinMondooVersion = rdoc.MinMondooVersion
    88  
    89  		for field, fdoc := range rdoc.Fields {
    90  			finfo, ok := info.Fields[field]
    91  			if !ok {
    92  				continue
    93  			}
    94  
    95  			finfo.MinMondooVersion = fdoc.MinMondooVersion
    96  		}
    97  	}
    98  }