-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
main.go
92 lines (81 loc) · 2.23 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/notifications-engine/pkg/docs"
)
func main() {
generateNotificationsDocs()
}
func generateNotificationsDocs() {
_ = os.RemoveAll("./docs/operator-manual/notifications/services")
_ = os.MkdirAll("./docs/operator-manual/notifications/services", 0755)
files, err := docs.CopyServicesDocs("./docs/operator-manual/notifications/services")
if err != nil {
log.Fatal(err)
}
if files != nil {
if e := updateMkDocsNav("Operator Manual", "Notification", "Notification Services", files); e != nil {
log.Fatal(e)
}
}
}
func updateMkDocsNav(parent string, child string, subchild string, files []string) error {
trimPrefixes(files, "docs/")
sort.Strings(files)
data, err := ioutil.ReadFile("mkdocs.yml")
if err != nil {
return err
}
var un unstructured.Unstructured
if e := yaml.Unmarshal(data, &un.Object); e != nil {
return e
}
nav := un.Object["nav"].([]interface{})
rootitem, _ := findNavItem(nav, parent)
if rootitem == nil {
return fmt.Errorf("Can't find '%s' root item in mkdoc.yml", parent)
}
rootnavitemmap := rootitem.(map[interface{}]interface{})
childnav, _ := findNavItem(rootnavitemmap[parent].([]interface{}), child)
childnavmap := childnav.(map[interface{}]interface{})
childnavitems := childnavmap[child].([]interface{})
childnavitems = removeNavItem(childnavitems, subchild)
commands := make(map[string]interface{})
commands[subchild] = files
childnavmap[child] = append(childnavitems, commands)
newmkdocs, err := yaml.Marshal(un.Object)
if err != nil {
return err
}
return ioutil.WriteFile("mkdocs.yml", newmkdocs, 0644)
}
func trimPrefixes(files []string, prefix string) {
for i, f := range files {
files[i] = strings.TrimPrefix(f, prefix)
}
}
func findNavItem(nav []interface{}, key string) (interface{}, int) {
for i, item := range nav {
o, ismap := item.(map[interface{}]interface{})
if ismap {
if _, ok := o[key]; ok {
return o, i
}
}
}
return nil, -1
}
func removeNavItem(nav []interface{}, key string) []interface{} {
_, i := findNavItem(nav, key)
if i != -1 {
nav = append(nav[:i], nav[i+1:]...)
}
return nav
}