-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
115 lines (96 loc) · 2.31 KB
/
product.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
package docweaver
import (
"fmt"
"github.com/reliqarts/go-common"
yml "gopkg.in/yaml.v3"
"os"
"strings"
)
type Product struct {
Name string
Description string
BaseUrl string
ImageUrl string
Versions []string
LatestVersion string
Index *Page
root productRoot
}
type Page struct {
UrlPath string
Title string
Content string
Version string
Product *Product
Index *Page
}
type productRoot struct {
ParentDir string
Key string
Source string
}
type productMeta struct {
Name string
Description string
ImageUrl string `yaml:"image_url"`
}
func (p *productRoot) filePath() string {
return fmt.Sprintf("%s%c%s", p.ParentDir, os.PathSeparator, p.Key)
}
func (p *productRoot) versionFilePath(version string) string {
return fmt.Sprintf("%s%c%s", p.filePath(), os.PathSeparator, version)
}
func (p *productRoot) hasSource() bool {
return p.Source != ""
}
func (p *Product) Key() string {
return p.root.Key
}
func (p *Product) Url() string {
return fmt.Sprintf("%s/%s", p.BaseUrl, p.LatestVersion)
}
func (p *Product) loadMeta() {
var err error
var meta *productMeta
var mainVersion string
r := p.root
// find product meta using main versions
for _, ver := range common.Intersection(p.Versions, mainVersions) {
meta, err = p.readMeta(ver)
if err != nil {
log(lError, "Failed to read meta file from product \"%s\", version \"%s\". %s\n", r.Key, ver, err)
}
if meta != nil {
mainVersion = ver
break
}
}
if meta != nil {
if meta.Name != "" {
p.Name = meta.Name
}
if meta.Description != "" {
p.Description = meta.Description
}
if meta.ImageUrl != "" {
p.ImageUrl = p.getAssetLink(meta.ImageUrl, mainVersion)
}
}
}
func (p *Product) readMeta(version string) (meta *productMeta, err error) {
r := p.root
metaFilePath := fmt.Sprintf("%s%c%s", r.versionFilePath(version), os.PathSeparator, metaFileName)
yaml, err := os.ReadFile(metaFilePath)
if err != nil {
return nil, err
}
if err := yml.Unmarshal(yaml, &meta); err != nil {
return nil, err
}
return meta, nil
}
func (p *Product) getAssetLink(link, version string) string {
linkReplacement := fmt.Sprintf("%s/%s/%s", GetAssetsRoutePrefix(), p.root.Key, version)
repl := strings.NewReplacer(assetUrlPlaceholder, linkReplacement)
return repl.Replace(link)
}