99 "os"
1010 "path"
1111 "path/filepath"
12- "sort"
1312 "strings"
1413 "text/template"
1514
@@ -59,74 +58,6 @@ var docsYamlCmd = &cobra.Command{
5958 },
6059}
6160
62- // required to be before more detail platform to ensure the right mapping
63- var platformMappingKeys = []string {
64- "aws" , "gcp" , "k8s" , "azure" , "azurerm" , "arista" , "equinix" , "ms365" , "msgraph" , "vsphere" , "esxi" , "terraform" , "terraform.state" , "terraform.plan" ,
65- }
66-
67- var platformMapping = map [string ][]string {
68- "aws" : {"aws" },
69- "gcp" : {"gcp" },
70- "k8s" : {"kubernetes" },
71- "azure" : {"azure" },
72- "azurerm" : {"azure" },
73- "arista" : {"arista-eos" },
74- "equinix" : {"equinix" },
75- "ms365" : {"microsoft365" },
76- "msgraph" : {"microsoft365" },
77- "vsphere" : {"vmware-esxi" , "vmware-vsphere" },
78- "esxi" : {"vmware-esxi" , "vmware-vsphere" },
79- "terraform" : {"terraform-hcl" },
80- "terraform.state" : {"terraform-state" },
81- "terraform.plan" : {"terraform-plan" },
82- }
83-
84- func ensureDefaults (id string , entry * lrcore.LrDocsEntry , version string ) * lrcore.LrDocsEntry {
85- for _ , k := range platformMappingKeys {
86- if entry == nil {
87- entry = & lrcore.LrDocsEntry {}
88- }
89- if entry .MinMondooVersion == "" {
90- entry .MinMondooVersion = version
91- } else if entry .MinMondooVersion == defaultVersionField && version != defaultVersionField {
92- // Update to specified version if previously set to default
93- entry .MinMondooVersion = version
94- }
95- if strings .HasPrefix (id , k ) {
96- entry .Platform = & lrcore.LrDocsPlatform {
97- Name : platformMapping [k ],
98- }
99- }
100- }
101- return entry
102- }
103-
104- func mergeFields (version string , entry * lrcore.LrDocsEntry , fields []* lrcore.BasicField ) {
105- if entry == nil && len (fields ) > 0 {
106- entry = & lrcore.LrDocsEntry {}
107- entry .Fields = map [string ]* lrcore.LrDocsField {}
108- } else if entry == nil {
109- return
110- } else if entry .Fields == nil {
111- entry .Fields = map [string ]* lrcore.LrDocsField {}
112- }
113- docFields := entry .Fields
114- for _ , f := range fields {
115- if docFields [f .ID ] == nil {
116- fDoc := & lrcore.LrDocsField {
117- MinMondooVersion : version ,
118- }
119- entry .Fields [f .ID ] = fDoc
120- } else if entry .Fields [f .ID ].MinMondooVersion == defaultVersionField && version != defaultVersionField {
121- entry .Fields [f .ID ].MinMondooVersion = version
122- }
123- // Scrub field version if same as resource
124- if entry .Fields [f .ID ].MinMondooVersion == entry .MinMondooVersion {
125- entry .Fields [f .ID ].MinMondooVersion = ""
126- }
127- }
128- }
129-
13061var docsJsonCmd = & cobra.Command {
13162 Use : "json" ,
13263 Short : "convert yaml docs manifest into json" ,
@@ -157,43 +88,6 @@ func runDocsYamlCmd(lrFile string, headerFile string, version string, docsFilePa
15788 return
15889 }
15990
160- // to ensure we generate the same markdown, we sort the resources first
161- sort .SliceStable (res .Resources , func (i , j int ) bool {
162- return res .Resources [i ].ID < res .Resources [j ].ID
163- })
164-
165- d := lrcore.LrDocs {
166- Resources : map [string ]* lrcore.LrDocsEntry {},
167- }
168-
169- fields := map [string ][]* lrcore.BasicField {}
170- isPrivate := map [string ]bool {}
171- for i := range res .Resources {
172- id := res .Resources [i ].ID
173- isPrivate [id ] = res .Resources [i ].IsPrivate
174- d .Resources [id ] = nil
175- if res .Resources [i ].Body != nil {
176- basicFields := []* lrcore.BasicField {}
177- for _ , f := range res .Resources [i ].Body .Fields {
178- if f .BasicField != nil {
179- basicFields = append (basicFields , f .BasicField )
180- }
181- }
182- fields [id ] = basicFields
183- }
184- }
185-
186- // default behaviour is to output the result on cli
187- if docsFilePath == "" {
188- data , err := yaml .Marshal (d )
189- if err != nil {
190- log .Fatal ().Err (err ).Msg ("could not marshal docs" )
191- }
192-
193- fmt .Println (string (data ))
194- return
195- }
196-
19791 // if an file was provided, we check if the file exist and merge existing content with the new resources
19892 // to ensure that existing documentation stays available
19993 var existingData lrcore.LrDocs
@@ -208,28 +102,28 @@ func runDocsYamlCmd(lrFile string, headerFile string, version string, docsFilePa
208102 if err != nil {
209103 log .Fatal ().Err (err ).Msg ("could not load yaml data" )
210104 }
105+ }
211106
212- log .Info ().Msg ("merge content" )
213- for k := range existingData .Resources {
214- v := existingData .Resources [k ]
215- d .Resources [k ] = v
216- }
107+ docs , err := res .GenerateDocs (version , defaultVersionField , existingData )
108+ if err != nil {
109+ log .Fatal ().Err (err ).Msg ("could not generate docs" )
217110 }
111+ // default behaviour is to output the result on cli
112+ if docsFilePath == "" {
113+ data , err := yaml .Marshal (docs )
114+ if err != nil {
115+ log .Fatal ().Err (err ).Msg ("could not marshal docs" )
116+ }
218117
219- // ensure default values and fields are set
220- for k := range d .Resources {
221- d .Resources [k ] = ensureDefaults (k , d .Resources [k ], version )
222- mergeFields (version , d .Resources [k ], fields [k ])
223- // Merge in other doc fields from core.lr
224- d .Resources [k ].IsPrivate = isPrivate [k ]
118+ fmt .Println (string (data ))
119+ return
225120 }
226121
227122 // generate content
228- data , err := yaml .Marshal (d )
123+ data , err := yaml .Marshal (docs )
229124 if err != nil {
230125 log .Fatal ().Err (err ).Msg ("could not marshal docs" )
231126 }
232-
233127 // add license header
234128 var headerTpl * template.Template
235129 if headerFile != "" {
0 commit comments