/
generate_readme.go
116 lines (94 loc) · 2.67 KB
/
generate_readme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cmd
import (
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config/resources/blueprint"
"github.com/jumppad-labs/jumppad/pkg/jumppad"
"github.com/spf13/cobra"
)
func newGenerateReadmeCommand(e jumppad.Engine) *cobra.Command {
connectorRunCmd := &cobra.Command{
Use: "readme",
Short: "Generate a markdown readme for the blueprints",
Long: `Generate a markdown readme for the blueprints`,
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
dst := ""
if len(args) == 1 {
dst = args[0]
} else {
dst = "./"
}
if dst == "." {
dst = "./"
}
_, err := e.ParseConfig(dst)
if err != nil {
return err
}
// find a blueprint
var br *blueprint.Blueprint
bps, _ := e.Config().FindResourcesByType(blueprint.TypeBlueprint)
for _, bp := range bps {
// pick the first blueprint in the root
if bp.Metadata().ResourceModule == "" {
br = bp.(*blueprint.Blueprint)
break
}
}
if br == nil {
return nil
}
// print the title
cmd.Printf("# %s\n", br.Title)
cmd.Println("")
// print the authors
cmd.Println("| <!-- --> | <!-- --> |")
cmd.Println("| ---- | ----------- |")
cmd.Printf("| Author | %s |\n", br.Author)
cmd.Printf("| Slug | %s |\n", br.Slug)
cmd.Println("")
cmd.Println("## Description")
cmd.Println(br.Description)
variables := []*types.Variable{}
os, _ := e.Config().FindResourcesByType(types.TypeVariable)
for _, o := range os {
// only grab the root outputs
if o.Metadata().ResourceModule == "" {
variables = append(variables, o.(*types.Variable))
}
}
if len(variables) > 0 {
cmd.Println("## Variables")
cmd.Println("These variables can be set to configure this blueprint")
cmd.Println("")
cmd.Println("| Name | Description |")
cmd.Println("| ---- | ----------- |")
for _, v := range variables {
cmd.Printf("| %s | %s |\n", v.ResourceName, v.Description)
}
cmd.Println("")
}
outputs := []*types.Output{}
os, _ = e.Config().FindResourcesByType(types.TypeOutput)
for _, o := range os {
// only grab the root outputs
if o.Metadata().ResourceModule == "" {
outputs = append(outputs, o.(*types.Output))
}
}
if len(outputs) > 0 {
cmd.Println("## Outputs")
cmd.Println("These blueprint sets the following outputs")
cmd.Println("")
cmd.Println("| Name | Description |")
cmd.Println("| ---- | ----------- |")
for _, v := range outputs {
cmd.Printf("| %s | %s |\n", v.ResourceName, v.Description)
}
cmd.Println("")
}
return nil
},
}
return connectorRunCmd
}