Last active
December 23, 2023 11:37
-
-
Save knabben/0e0cb96a5c4c6f9d30fad17230df304a to your computer and use it in GitHub Desktop.
Table Markdown converter for Inspec JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/fbiville/markdown-table-formatter/pkg/markdown" | |
"io" | |
"log" | |
"os" | |
"reflect" | |
) | |
const FILE = "./parser/data.json" | |
type Results struct { | |
Status string `json:"status"` | |
Description string `json:"code_desc"` | |
} | |
type Controls struct { | |
ID string `json:"id"` | |
Title string `json:"title"` | |
Results []Results `json:"results"` | |
} | |
type Profiles struct { | |
Name string `json:"name"` | |
Version string `json:"version"` | |
Controls []Controls `json:"controls"` | |
} | |
type Data struct { | |
Version string `json:"version"` | |
Profiles []Profiles `json:"profiles"` | |
} | |
func main() { | |
var ( | |
err error | |
data Data | |
) | |
if data, err = renderJSON(FILE); err != nil { | |
log.Fatal(err) | |
} | |
content := loadContent(data) | |
buildTable(content) | |
} | |
func renderJSON(file string) (data Data, err error) { | |
var content []byte | |
f, err := os.Open(file) | |
if err != nil { | |
return | |
} | |
defer f.Close() | |
if content, err = io.ReadAll(f); err != nil { | |
return | |
} | |
json.Unmarshal(content, &data) | |
return | |
} | |
func loadContent(data Data) (result [][]string) { | |
for _, p := range data.Profiles { | |
for _, c := range p.Controls { | |
for _, n := range c.Results { | |
if n.Status == "failed" { | |
title := strings.ReplaceAll(c.Title, "\n", " ") | |
tmpResult := []string{c.ID, title, ""} | |
if len(result) > 0 && reflect.DeepEqual(result[len(result)-1], tmpResult) { | |
continue | |
} | |
result = append(result, tmpResult) | |
} | |
} | |
} | |
} | |
return | |
} | |
func buildTable(table [][]string) error { | |
tt, err := markdown.NewTableFormatterBuilder(). | |
WithPrettyPrint(). | |
Build("ID", "Description", "Reason"). | |
Format(table) | |
if err != nil { | |
return err | |
} | |
fmt.Println(tt) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment