-
Notifications
You must be signed in to change notification settings - Fork 6
/
readme.go
131 lines (110 loc) · 3.19 KB
/
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/nakamasato/kubernetes-training/doc"
yaml "gopkg.in/yaml.v3"
)
func main() {
f, err := os.Open("readme.yml")
if err != nil {
log.Fatal(err)
}
defer f.Close()
d := yaml.NewDecoder(f)
var m map[string][]Version
if err := d.Decode(&m); err != nil {
log.Fatal(err)
}
generateReadme(m["versions"], "README.md")
updateVersionInReadme(m["versions"], "contents/README.md")
}
type Version struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
RepoUrl string `yaml:"repoUrl"`
Category string `yaml:"category"`
ToDo bool `yaml:"todo"`
Dir string `yaml:"dir"`
}
func (v Version) getReleaseUrl() string {
if v.Version == "" {
return v.RepoUrl
} else if v.Version == "latest" {
return v.RepoUrl + "/releases"
} else {
return v.RepoUrl + "/releases/tag/" + v.Version
}
}
func (v Version) getToDoString() string {
if v.ToDo {
return "(ToDo)"
} else {
return ""
}
}
func generateReadme(versions []Version, filename string) {
book := doc.NewMarkDown()
book.WriteTitle("Kubernetes Training", doc.LevelTitle).
WriteLines(2)
book.WriteList(fmt.Sprintf("Read on Website: %s", book.GetLink("https://www.nakamasato.com/kubernetes-training", "https://www.nakamasato.com/kubernetes-training")))
book.WriteList(
fmt.Sprintf(
"Read on GitHub: %s (All files were moved to `contents` in %s",
book.GetLink("contents", "contents"),
book.GetLink("#105", "https://github.com/nakamasato/kubernetes-training/pull/105"),
),
)
book.WriteList("README.md is generated by `readme.go`")
book.WriteTitle("Versions", doc.LevelSection)
for _, version := range versions {
fmt.Printf("name: %s, version: %s\n", version.Name, version.Version)
book.WriteList(
fmt.Sprintf(
"%s: %s%s",
version.Name,
book.GetLink(version.Version, version.getReleaseUrl()),
version.getToDoString(),
),
)
}
book.WriteTitle("Cloud Native Trail Map", doc.LevelSection)
book.WriteList(book.GetLink("https://github.com/cncf/trailmap", "https://github.com/cncf/trailmap"))
book.WriteList(book.GetLink("https://www.cncf.io/blog/2018/03/08/introducing-the-cloud-native-landscape-2-0-interactive-edition/", "https://www.cncf.io/blog/2018/03/08/introducing-the-cloud-native-landscape-2-0-interactive-edition/"))
book.WriteWordLine(fmt.Sprintf("!%s", book.GetLink("", "https://github.com/cncf/trailmap/blob/master/CNCF_TrailMap_latest.png?raw=true")))
err := book.Export(filename)
if err != nil {
log.Fatal(err)
}
}
func updateVersionInReadme(versions []Version, filename string) {
input, err := os.ReadFile(filename)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
for _, version := range versions {
for i, line := range lines {
if strings.Contains(line, "Contents") { // not update Contents
break
}
if strings.Contains(line, fmt.Sprintf("1. %s:", version.Name)) {
lines[i] = fmt.Sprintf(
"1. %s: [%s](%s)%s",
version.Name,
version.Version,
version.getReleaseUrl(),
version.getToDoString(),
)
break
}
}
}
output := strings.Join(lines, "\n")
err = os.WriteFile(filename, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}