Skip to content

Commit b9aed8e

Browse files
authored
[Maintenance] Change MD content injection method (#1256)
1 parent 2fcace5 commit b9aed8e

File tree

9 files changed

+148
-59
lines changed

9 files changed

+148
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- (Bugfix) Fix invalid Timeout calculation in case of ActionList
1818
- (Feature) Optional JSON logger format
1919
- (Improvement) Change Operator default ReplicasCount to 1
20+
- (Maintenance) Change MD content injection method
2021

2122
## [1.2.24](https://github.com/arangodb/kube-arangodb/tree/1.2.24) (2023-01-25)
2223
- (Bugfix) Fix deployment creation on ARM64

docs/generated/actions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## List
44

5+
<!-- START(actionsTable) -->
56
| Action | Internal | Timeout | Optional | Edition | Description |
67
|:----------------------------------:|:--------:|:-------:|:--------:|:----------------------:|:------------------------------------------------------------------------------------------------------------------:|
78
| AddMember | no | 10m0s | no | Community & Enterprise | Adds new member to the Member list |
@@ -81,9 +82,11 @@
8182
| WaitForMemberReady | no | 30m0s | no | Community & Enterprise | Wait for member Ready condition |
8283
| WaitForMemberUp | no | 30m0s | no | Community & Enterprise | Wait for member to be responsive |
8384

85+
<!-- END(actionsTable) -->
8486

8587
## ArangoDeployment spec
8688

89+
<!-- START(actionsModYaml) -->
8790
```yaml
8891
spec:
8992
timeouts:
@@ -165,4 +168,5 @@ spec:
165168
WaitForMemberReady: 30m0s
166169
WaitForMemberUp: 30m0s
167170

168-
```
171+
```
172+
<!-- END(actionsModYaml) -->

docs/generated/metrics/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# ArangoDB Operator Metrics
22

3-
## List
3+
## List of the Operator metrics
44

5+
<!-- START(metricsTable) -->
56
| Name | Namespace | Group | Type | Description |
67
|:-------------------------------------------------------------------------------------------------------------------------------------:|:-----------------:|:-----------------:|:-------:|:--------------------------------------------------------------------------------------|
78
| [arangodb_operator_agency_errors](./arangodb_operator_agency_errors.md) | arangodb_operator | agency | Counter | Current count of agency cache fetch errors |
@@ -30,3 +31,5 @@
3031
| [arangodb_operator_resources_arangodeployment_status_restores](./arangodb_operator_resources_arangodeployment_status_restores.md) | arangodb_operator | resources | Counter | Counter for deployment status restored |
3132
| [arangodb_operator_resources_arangodeployment_uptodate](./arangodb_operator_resources_arangodeployment_uptodate.md) | arangodb_operator | resources | Gauge | Defines if ArangoDeployment is uptodate |
3233
| [arangodb_operator_resources_arangodeployment_validation_errors](./arangodb_operator_resources_arangodeployment_validation_errors.md) | arangodb_operator | resources | Counter | Counter for deployment validation errors |
34+
35+
<!-- END(metricsTable) -->

internal/actions.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ import (
4040
//go:embed actions.yaml
4141
var actions []byte
4242

43-
//go:embed actions.tmpl
44-
var actionsMD []byte
45-
4643
//go:embed actions.go.tmpl
4744
var actionsGoTemplate []byte
4845

@@ -300,16 +297,6 @@ func RenderActions(root string) error {
300297
{
301298
actions := path.Join(root, "docs", "generated", "actions.md")
302299

303-
out, err := os.OpenFile(actions, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
304-
if err != nil {
305-
return err
306-
}
307-
308-
i, err := template.New("actions").Parse(string(actionsMD))
309-
if err != nil {
310-
return err
311-
}
312-
313300
action := md.NewColumn("Action", md.ColumnCenterAlign)
314301
timeout := md.NewColumn("Timeout", md.ColumnCenterAlign)
315302
description := md.NewColumn("Description", md.ColumnCenterAlign)
@@ -379,16 +366,12 @@ func RenderActions(root string) error {
379366
return err
380367
}
381368

382-
if err := i.Execute(out, map[string]interface{}{
383-
"table": t.Render(),
384-
"example": string(d),
369+
if err := md.ReplaceSectionsInFile(actions, map[string]string{
370+
"actionsTable": md.WrapWithNewLines(t.Render()),
371+
"actionsModYaml": md.WrapWithNewLines(md.WrapWithYAMLSegment(string(d))),
385372
}); err != nil {
386373
return err
387374
}
388-
389-
if err := out.Close(); err != nil {
390-
return err
391-
}
392375
}
393376

394377
return nil

internal/actions.tmpl

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/md/sections.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package md
22+
23+
import (
24+
"bytes"
25+
"fmt"
26+
"os"
27+
"strings"
28+
29+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
30+
)
31+
32+
func ReplaceSectionsInFile(path string, sections map[string]string) error {
33+
data, err := os.ReadFile(path)
34+
if err != nil {
35+
return err
36+
}
37+
38+
res, err := ReplaceSections(string(data), sections)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return os.WriteFile(path, []byte(res), 0644)
44+
}
45+
46+
func ReplaceSections(in string, sections map[string]string) (string, error) {
47+
for k, v := range sections {
48+
if n, err := ReplaceSection(in, v, k); err != nil {
49+
return "", err
50+
} else {
51+
in = n
52+
}
53+
}
54+
55+
return in, nil
56+
}
57+
58+
func ReplaceSection(in, replace, section string) (string, error) {
59+
start, end := fmt.Sprintf("<!-- START(%s) -->", section), fmt.Sprintf("<!-- END(%s) -->", section)
60+
61+
b := bytes.NewBuffer(nil)
62+
63+
for len(in) > 0 {
64+
startID := strings.Index(in, start)
65+
if startID == -1 {
66+
b.WriteString(in)
67+
in = ""
68+
continue
69+
}
70+
71+
b.WriteString(in[0:startID])
72+
73+
in = moveString(in, startID+len(start))
74+
75+
b.WriteString(start)
76+
77+
b.WriteString(replace)
78+
79+
endID := strings.Index(in, end)
80+
if endID == -1 {
81+
return "", errors.Newf("END sections is missing")
82+
}
83+
84+
b.WriteString(end)
85+
86+
in = moveString(in, endID+len(end))
87+
}
88+
89+
return b.String(), nil
90+
}
91+
92+
func moveString(in string, offset int) string {
93+
if offset >= len(in) {
94+
return ""
95+
}
96+
97+
return in[offset:]
98+
}

internal/md/wrap.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package md
22+
23+
import "fmt"
24+
25+
func WrapWithNewLines(in string) string {
26+
return fmt.Sprintf("\n%s\n", in)
27+
}
28+
29+
func WrapWithYAMLSegment(in string) string {
30+
return WrapWithSegment(in, "yaml")
31+
}
32+
33+
func WrapWithSegment(in, segment string) string {
34+
return fmt.Sprintf("```%s\n%s\n```", segment, in)
35+
}

internal/metrics.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ var metricsGoTemplate []byte
3838
//go:embed metrics.item.go.tmpl
3939
var metricsItemGoTemplate []byte
4040

41-
//go:embed metrics.tmpl
42-
var metricsTemplate []byte
43-
4441
//go:embed metrics.item.tmpl
4542
var metricItemTemplate []byte
4643

@@ -255,28 +252,12 @@ func generateMetricsREADME(root string, in MetricsDoc) error {
255252
}
256253
}
257254

258-
table := t.Render()
259-
260-
q, err := template.New("metrics").Parse(string(metricsTemplate))
261-
if err != nil {
262-
return err
263-
}
264-
265-
out, err := os.OpenFile(path.Join(root, "README.md"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
266-
if err != nil {
267-
return err
268-
}
269-
270-
if err := q.Execute(out, map[string]interface{}{
271-
"table": table,
255+
if err := md.ReplaceSectionsInFile(path.Join(root, "README.md"), map[string]string{
256+
"metricsTable": md.WrapWithNewLines(t.Render()),
272257
}); err != nil {
273258
return err
274259
}
275260

276-
if err := out.Close(); err != nil {
277-
return err
278-
}
279-
280261
return nil
281262
}
282263

internal/metrics.tmpl

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)