-
Notifications
You must be signed in to change notification settings - Fork 18
/
tweak.go
133 lines (122 loc) · 3.09 KB
/
tweak.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package cobrautils
import (
"regexp"
"strings"
_ "github.com/open-component-model/ocm/pkg/contexts/clictx/config"
"github.com/spf13/cobra"
"github.com/open-component-model/ocm/pkg/out"
)
func TweakCommand(cmd *cobra.Command, ctx out.Context) {
if ctx != nil {
cmd.SetOut(ctx.StdOut())
cmd.SetErr(ctx.StdErr())
cmd.SetIn(ctx.StdIn())
}
cobra.AddTemplateFuncs(templatefuncs)
CleanMarkdownUsageFunc(cmd)
SupportNestedHelpFunc(cmd)
cmd.SetHelpTemplate(HelpTemplate)
cmd.SetUsageTemplate(UsageTemplate)
}
// SupportNestedHelpFunc adds support help evaluation of given nested command path.
func SupportNestedHelpFunc(cmd *cobra.Command) {
defaultHelpFunc := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, s []string) {
_, s, err := cmd.Root().Find(s)
if err == nil {
for i := 1; i < len(s); i++ {
var next *cobra.Command
for _, c := range cmd.Commands() {
if c.Name() == s[i] {
next = c
break
}
}
if next == nil {
break
} else {
cmd = next
}
}
}
defaultHelpFunc(cmd, s)
})
}
// CleanMarkdownUsageFunc removes markdown tags from the long usage of the command.
// With this func it is possible to generate the markdown docs but still have readable commandline help func.
func CleanMarkdownUsageFunc(cmd *cobra.Command) {
defaultHelpFunc := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, s []string) {
cmd.Long = CleanMarkdown(cmd.Long)
cmd.Example = CleanMarkdown(cmd.Example)
defaultHelpFunc(cmd, s)
})
}
var center = regexp.MustCompile(" *</?(pre|center)> *")
func CleanMarkdown(s string) string {
if strings.HasPrefix(s, "##") {
for strings.HasPrefix(s, "#") {
s = s[1:]
}
}
s = strings.ReplaceAll(s, "<code>", "\u00ab")
s = strings.ReplaceAll(s, "</code>", "\u00bb")
s = strings.ReplaceAll(s, "<", "<")
s = strings.ReplaceAll(s, ">", ">")
s = strings.ReplaceAll(s, "**", "")
s = strings.ReplaceAll(s, "<em>", "")
s = strings.ReplaceAll(s, "</em>", "")
s = strings.ReplaceAll(s, "<EXAMPLE>", "")
s = strings.ReplaceAll(s, "</EXAMPLE>", "")
s = string(center.ReplaceAll([]byte(s), nil))
var r []string
found := 0
omitted := 0
lastIndex := -1
mask := false
for _, l := range strings.Split(s, "\n") {
if strings.Contains(l, "<pre>") {
mask = true
}
if strings.Contains(l, "</pre>") {
mask = false
}
if mask {
found = 0
} else {
if strings.HasSuffix(l, "\\") {
l = l[:len(l)-1]
found = 0
} else {
t := strings.TrimSpace(l)
if strings.HasPrefix(t, "- ") {
index := strings.Index(l, "-")
if omitted > 0 && lastIndex >= 0 && lastIndex > index {
r = append(r, "")
}
lastIndex = index
found = 1
} else {
if t == "" {
found++
if found > 1 {
omitted++
continue
}
} else {
found = 0
}
}
}
}
omitted = 0
if !strings.HasPrefix(strings.TrimSpace(l), "-") {
lastIndex = -1
}
r = append(r, l)
}
return strings.Join(r, "\n")
}