Skip to content

Commit f1a4328

Browse files
authored
Merge pull request #3993 from beatrausch/marker-with-prefix
✨ (Only valid for those who consume Kubebuilder as a lib) - Allow usage of custom marker names
2 parents e82127c + 8d8d091 commit f1a4328

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

pkg/machinery/marker.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
)
2424

25-
const prefix = "+kubebuilder:scaffold:"
25+
const kbPrefix = "+kubebuilder:scaffold:"
2626

2727
var commentsByExt = map[string]string{
2828
".go": "//",
@@ -33,16 +33,28 @@ var commentsByExt = map[string]string{
3333

3434
// Marker represents a machine-readable comment that will be used for scaffolding purposes
3535
type Marker struct {
36+
prefix string
3637
comment string
3738
value string
3839
}
3940

40-
// NewMarkerFor creates a new marker customized for the specific file
41-
// Supported file extensions: .go, .yaml, .yml
41+
// NewMarkerFor creates a new marker customized for the specific file. The created marker
42+
// is prefixed with `+kubebuilder:scaffold:` the default prefix for kubebuilder.
43+
// Supported file extensions: .go, .yaml, .yml.
4244
func NewMarkerFor(path string, value string) Marker {
45+
return NewMarkerWithPrefixFor(kbPrefix, path, value)
46+
}
47+
48+
// NewMarkerWithPrefixFor creates a new custom prefixed marker customized for the specific file
49+
// Supported file extensions: .go, .yaml, .yml
50+
func NewMarkerWithPrefixFor(prefix string, path string, value string) Marker {
4351
ext := filepath.Ext(path)
4452
if comment, found := commentsByExt[ext]; found {
45-
return Marker{comment, value}
53+
return Marker{
54+
prefix: markerPrefix(prefix),
55+
comment: comment,
56+
value: value,
57+
}
4658
}
4759

4860
extensions := make([]string, 0, len(commentsByExt))
@@ -54,13 +66,13 @@ func NewMarkerFor(path string, value string) Marker {
5466

5567
// String implements Stringer
5668
func (m Marker) String() string {
57-
return m.comment + " " + prefix + m.value
69+
return m.comment + " " + m.prefix + m.value
5870
}
5971

6072
// EqualsLine compares a marker with a string representation to check if they are the same marker
6173
func (m Marker) EqualsLine(line string) bool {
6274
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), m.comment))
63-
return line == prefix+m.value
75+
return line == m.prefix+m.value
6476
}
6577

6678
// CodeFragments represents a set of code fragments
@@ -69,3 +81,17 @@ type CodeFragments []string
6981

7082
// CodeFragmentsMap binds Markers and CodeFragments together
7183
type CodeFragmentsMap map[Marker]CodeFragments
84+
85+
func markerPrefix(prefix string) string {
86+
trimmed := strings.TrimSpace(prefix)
87+
var builder strings.Builder
88+
if !strings.HasPrefix(trimmed, "+") {
89+
builder.WriteString("+")
90+
}
91+
builder.WriteString(trimmed)
92+
if !strings.HasSuffix(trimmed, ":") {
93+
builder.WriteString(":")
94+
}
95+
96+
return builder.String()
97+
}

pkg/machinery/marker_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,47 @@ var _ = Describe("Marker", func() {
3939
Context("String", func() {
4040
DescribeTable("should return the right string representation",
4141
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
42-
Entry("for go files", Marker{comment: "//", value: "test"}, "// +kubebuilder:scaffold:test"),
43-
Entry("for yaml files", Marker{comment: "#", value: "test"}, "# +kubebuilder:scaffold:test"),
42+
Entry("for go files", Marker{prefix: kbPrefix, comment: "//", value: "test"}, "// +kubebuilder:scaffold:test"),
43+
Entry("for yaml files", Marker{prefix: kbPrefix, comment: "#", value: "test"}, "# +kubebuilder:scaffold:test"),
44+
)
45+
})
46+
})
47+
48+
var _ = Describe("NewMarkerFor", func() {
49+
Context("String", func() {
50+
DescribeTable("should return the right string representation",
51+
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
52+
Entry("for yaml files", NewMarkerFor("test.yaml", "test"), "# +kubebuilder:scaffold:test"),
53+
)
54+
})
55+
})
56+
57+
var _ = Describe("NewMarkerWithPrefixFor", func() {
58+
Context("String", func() {
59+
DescribeTable("should return the right string representation",
60+
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
61+
62+
Entry("for yaml files",
63+
NewMarkerWithPrefixFor("custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
64+
Entry("for yaml files",
65+
NewMarkerWithPrefixFor("+custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
66+
Entry("for yaml files",
67+
NewMarkerWithPrefixFor("custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
68+
Entry("for yaml files",
69+
NewMarkerWithPrefixFor("+custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
70+
Entry("for yaml files",
71+
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.yaml", "test"), "# +custom:scaffold:test"),
72+
73+
Entry("for go files",
74+
NewMarkerWithPrefixFor("custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
75+
Entry("for go files",
76+
NewMarkerWithPrefixFor("+custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
77+
Entry("for go files",
78+
NewMarkerWithPrefixFor("custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
79+
Entry("for go files",
80+
NewMarkerWithPrefixFor("+custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
81+
Entry("for go files",
82+
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.go", "test"), "// +custom:scaffold:test"),
4483
)
4584
})
4685
})

0 commit comments

Comments
 (0)