-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_client.go
186 lines (169 loc) · 4.69 KB
/
doc_client.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package internal
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"log"
"os"
"strings"
)
var clientSingleton *DocumentationClient
type DocumentationClient struct {
*Markdown
subscriptions []*SubscriptionWrapper
vnets []*VNETWrapper
docsDir string
}
func GetSingletonDocumentationClient() (*DocumentationClient, error) {
if clientSingleton == nil {
docsDir := viper.GetString("docsDir")
if docsDir == "" {
docsDir = "docs"
}
var err error
clientSingleton, err = newDocumentationClient(docsDir)
if err != nil {
return nil, err
}
}
return clientSingleton, nil
}
func newDocumentationClient(docsDir string) (*DocumentationClient, error) {
subscriptions, err := getAllSubscriptions()
if err != nil {
return nil, err
}
vnets, err := GetWrappedVNETsInSubscriptions(subscriptions)
if err != nil {
return nil, err
}
clientSingleton = newDocumentationClientWithData(docsDir, subscriptions, vnets)
return clientSingleton, nil
}
func newDocumentationClientWithData(docsDir string, subscriptions []*SubscriptionWrapper, vnets []*VNETWrapper) *DocumentationClient {
client := &DocumentationClient{
Markdown: NewMarkdown(),
docsDir: docsDir,
subscriptions: subscriptions,
vnets: vnets,
}
return client
}
func (client *DocumentationClient) GetSubscriptions() []*SubscriptionWrapper {
return client.subscriptions
}
func (client *DocumentationClient) GetSubscriptionById(id string) *SubscriptionWrapper {
for _, subscription := range client.subscriptions {
if *subscription.SubscriptionID == id {
return subscription
}
}
return nil
}
func (client *DocumentationClient) GetVNETs() []*VNETWrapper {
return client.vnets
}
//func (client *DocumentationClient) AddVNET(vnet *VNETWrapper) {
// client.vnets = append(client.vnets, vnet)
//}
func (client *DocumentationClient) GenerateMarkdown(verbose bool) error {
if verbose {
log.Println("Generating markdown files")
}
client.CleanDocsDir()
err := client.WriteDocumentation()
if err != nil {
return err
}
for _, sub := range client.GetSubscriptions() {
err = sub.WriteMarkdown()
if err != nil {
return err
}
}
for _, vnet := range client.GetVNETs() {
vnet.WriteMarkdown()
if err != nil {
return err
}
}
if verbose {
log.Println("Markdown files generated")
}
return nil
}
func (client *DocumentationClient) generateDocumentation() string {
var markdown string
markdown += client.GenerateTitle("Subscriptions", 1)
for _, sub := range client.GetSubscriptions() {
if len(sub.vnets) > 0 {
markdown += client.GenerateListItem(
client.GenerateLink(*sub.DisplayName, fmt.Sprintf("%s/Readme.md", strings.ReplaceAll(*sub.DisplayName, " ", "%20"))),
)
}
}
return markdown
}
func (client *DocumentationClient) WriteDocumentation() error {
markdown := client.generateDocumentation()
err := client.writeToFile(markdown, fmt.Sprintf("%s/Readme.md", client.GetDocsDir()))
return err
}
func (client *DocumentationClient) GetDocsDir() string {
return client.docsDir
}
func (client *DocumentationClient) CleanDocsDir() {
os.RemoveAll(client.docsDir)
}
func (client *DocumentationClient) GetSubscriptionNameByID(subscriptionId string) (name string, found bool) {
for _, subscription := range client.GetSubscriptions() {
if *subscription.SubscriptionID == subscriptionId {
name = *subscription.DisplayName
found = true
break
}
}
return name, found
}
func getAllSubscriptions() (subscriptions []*SubscriptionWrapper, err error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return subscriptions, err
}
client, err := armsubscription.NewSubscriptionsClient(cred, nil)
if err != nil {
return subscriptions, err
}
pager := client.NewListPager(nil)
for pager.More() {
subList, err := pager.NextPage(context.Background())
if err != nil {
return subscriptions, err
}
for _, subscription := range subList.ListResult.Value {
if subscriptionNeeded(subscription) {
wrappedSub := NewSubscriptionWrapper(*subscription)
subscriptions = append(subscriptions, &wrappedSub)
}
}
}
//subscriptionList = subscriptions
return subscriptions, nil
}
func subscriptionNeeded(subscription *armsubscription.Subscription) bool {
value := *subscription.SubscriptionID
if viper.GetString("subscriptions.key") == "name" {
value = *subscription.DisplayName
}
include := viper.GetStringSlice("subscriptions.include")
if len(include) > 0 {
return slices.Contains(include, value)
}
if slices.Contains(viper.GetStringSlice("subscriptions.exclude"), value) {
return false
}
return true
}