forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
135 lines (104 loc) · 2.86 KB
/
add.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
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
// linkAddrToPath converts a link address into a path name.
func (d *Doc) linkAddrToPath(address string) (string, error) {
if address == "" {
return "", errors.New("need address")
}
dir := filepath.Dir(d.Name)
var file string
// An "absolute link path" like this has been specified:
//
// [Foo](/absolute-link.md)
if strings.HasPrefix(address, absoluteLinkPrefix) {
if !fileExists(docRoot) {
return "", fmt.Errorf("document root %q does not exist", docRoot)
}
file = filepath.Join(docRoot, address)
} else {
file = filepath.Join(dir, address)
}
return file, nil
}
// addHeading adds the specified heading to the document.
//
// Note that headings must be unique.
func (d *Doc) addHeading(heading Heading) error {
name := heading.Name
if name == "" {
return d.Errorf("heading name cannot be blank: %+v", heading)
}
if heading.LinkName == "" {
return d.Errorf("heading link name cannot be blank: %q (%+v)",
name, heading)
}
if heading.Level <= 0 {
return d.Errorf("heading level must be atleast 1: %q (%+v)",
name, heading)
}
if _, ok := d.Headings[name]; ok {
return d.Errorf("duplicate heading: %q (heading: %+v)",
name, heading)
}
// Potentially change the ID to handle strange characters
// supported in links by GitHub.
id, err := createHeadingID(heading.Name)
if err != nil {
return err
}
heading.LinkName = id
d.Logger.WithField("heading", fmt.Sprintf("%+v", heading)).Debug("adding heading")
d.Headings[name] = heading
return nil
}
// addLink potentially adds the specified link to the document.
//
// Note that links do not need to be unique: a document can contain
// multiple links with:
//
// - the same description and the same address.
// - the same description but with different addresses.
// - different descriptions but with the same address.
func (d *Doc) addLink(link Link) error {
addr := link.Address
if link.ResolvedPath != "" {
addr = link.ResolvedPath
}
if addr == "" {
return d.Errorf("link address cannot be blank: %+v", link)
}
if link.Type == unknownLink {
return d.Errorf("BUG: link type invalid: %+v", link)
}
// Not checked by default as magic "build status" / go report / godoc
// links don't have a description - they have a image only.
if strict && link.Description == "" {
return d.Errorf("link description cannot be blank: %q (%+v)",
addr, link)
}
fields := logrus.Fields{
"link": fmt.Sprintf("%+v", link),
}
links := d.Links[addr]
for _, l := range links {
if l.Type == link.Type {
d.Logger.WithFields(fields).Debug("not adding duplicate link")
return nil
}
}
d.Logger.WithFields(fields).Debug("adding link")
links = append(links, link)
d.Links[addr] = links
return nil
}