Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
doc generation functionality extended (#127)
Browse files Browse the repository at this point in the history
Co-authored-by: Ron <[email protected]>
  • Loading branch information
amanenk and roneli authored Dec 10, 2021
1 parent c7d150c commit c386ea9
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions provider/docs/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package docs

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"text/template"
Expand All @@ -13,17 +16,49 @@ import (
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

const (
tablesDir = "tables"
)

// GenerateDocs creates table documentation for the provider based on it's ResourceMap
func GenerateDocs(p *provider.Provider, outputPath string) error {
func GenerateDocs(p *provider.Provider, outputPath string, deleteOld bool) error {
if deleteOld {
if err := deleteOldFiles(outputPath); err != nil {
fmt.Printf("failed to remove old docs: %s", err)
return err
}
}
for _, table := range p.ResourceMap {
if err := renderAllTables(table, outputPath); err != nil {
fmt.Printf("render table error: %s", err)
fmt.Printf("render table %s error: %s", table.Name, err)
return err
}
}
return nil
}

// deleteOldFiles removes old files from tables directory, creates tables directory if it does not exist
func deleteOldFiles(outputPath string) error {
tablesPath := path.Join(outputPath, tablesDir)
dir, err := ioutil.ReadDir(tablesPath)
if err != nil {
// create directory if it does not exist
if errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(tablesPath, 0644); err != nil {
return err
}
return nil
}
return fmt.Errorf("Failed to generate docs: %s\n", err)
}
for _, d := range dir {
if err := os.RemoveAll(path.Join([]string{tablesPath, d.Name()}...)); err != nil {
return fmt.Errorf("Failed to remove old docs: %s\n", err)
}
}
return nil
}

func renderAllTables(t *schema.Table, outputPath string) error {
if err := renderTable(t, outputPath); err != nil {
return err
Expand Down Expand Up @@ -53,7 +88,7 @@ func renderTable(table *schema.Table, path string) error {
if err := t.Execute(&buf, table); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(path, "tables", fmt.Sprintf("%s.md", table.Name)), buf.Bytes(), 0644)
return ioutil.WriteFile(filepath.Join(path, tablesDir, fmt.Sprintf("%s.md", table.Name)), buf.Bytes(), 0644)
}

const tableTmpl = `
Expand Down

0 comments on commit c386ea9

Please sign in to comment.