Skip to content

Commit 500baff

Browse files
committed
Refactor code to take into account optional fields
Refactor code so we are not passing in values and only using the defaults from the module config Show parent field if there are no child fields to show We want to show the labels field (among others) in the markdown table if so that it is part of the documentation rather than showing all the preset labels as individual fields in the docs. Signed-off-by: Luke Mallon (Nalum) <[email protected]> Update schema with +nodoc tags and copy to blueprint starter module Hide certain fields This change brings the output more inline with the definition Update output to remove default column Commit updated test data Signed-off-by: Luke Mallon (Nalum) <[email protected]> Update nodoc behaviour Signed-off-by: Luke Mallon (Nalum) <[email protected]> Update structure to accomodate nodoc Signed-off-by: Luke Mallon (Nalum) <[email protected]> Merge branch 'main' into module-md-optional-fields
1 parent 9fba645 commit 500baff

File tree

23 files changed

+475
-167
lines changed

23 files changed

+475
-167
lines changed

blueprints/starter/README.md

Lines changed: 35 additions & 16 deletions
Large diffs are not rendered by default.

blueprints/starter/cue.mod/pkg/timoni.sh/core/v1alpha1/image.cue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
// Reference is the image address computed from repository, tag and digest
3232
// in the format [REPOSITORY]:[TAG]@[DIGEST].
33+
// +nodoc
3334
reference: string
3435

3536
if digest != "" && tag != "" {

blueprints/starter/cue.mod/pkg/timoni.sh/core/v1alpha1/metadata.cue

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ import "strings"
4343

4444
// Standard Kubernetes labels: app name, version and managed-by.
4545
labels: {
46-
(#StdLabelName): name
47-
(#StdLabelVersion): #Version
46+
// +nodoc
47+
(#StdLabelName): name
48+
// +nodoc
49+
(#StdLabelVersion): #Version
50+
// +nodoc
4851
(#StdLabelManagedBy): "timoni"
4952
}
5053

5154
// LabelSelector selects Pods based on the app.kubernetes.io/name label.
5255
#LabelSelector: #Labels & {
56+
// +nodoc
5357
(#StdLabelName): name
5458
}
5559

@@ -74,6 +78,7 @@ import "strings"
7478
namespace: #Meta.namespace
7579

7680
labels: #Meta.labels
81+
// +nodoc
7782
labels: (#StdLabelComponent): #Component
7883

7984
annotations?: #Annotations
@@ -84,8 +89,10 @@ import "strings"
8489
// LabelSelector selects Pods based on the app.kubernetes.io/name
8590
// and app.kubernetes.io/component labels.
8691
#LabelSelector: #Labels & {
92+
// +nodoc
8793
(#StdLabelComponent): #Component
88-
(#StdLabelName): #Meta.name
94+
// +nodoc
95+
(#StdLabelName): #Meta.name
8996
}
9097
}
9198

@@ -104,6 +111,7 @@ import "strings"
104111
name: #Meta.name + "-" + #Component
105112

106113
labels: #Meta.labels
114+
// +nodoc
107115
labels: (#StdLabelComponent): #Component
108116

109117
annotations?: #Annotations
@@ -114,7 +122,9 @@ import "strings"
114122
// LabelSelector selects Pods based on the app.kubernetes.io/name
115123
// and app.kubernetes.io/component labels.
116124
#LabelSelector: #Labels & {
125+
// +nodoc
117126
(#StdLabelComponent): #Component
118-
(#StdLabelName): #Meta.name
127+
// +nodoc
128+
(#StdLabelName): #Meta.name
119129
}
120130
}

blueprints/starter/cue.mod/pkg/timoni.sh/core/v1alpha1/selector.cue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ package v1alpha1
1515
labels: #Labels
1616

1717
// Standard Kubernetes label: app name.
18-
labels: (#StdLabelName): #Name
18+
labels: {
19+
// +nodoc
20+
(#StdLabelName): #Name
21+
}
1922
}

blueprints/starter/cue.mod/pkg/timoni.sh/core/v1alpha1/semver.cue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
let minMajor = strconv.Atoi(strings.Split(#Minimum, ".")[0])
2222
let minMinor = strconv.Atoi(strings.Split(#Minimum, ".")[1])
2323

24+
// +nodoc
2425
major: int & >=minMajor
2526
major: strconv.Atoi(strings.Split(#Version, ".")[0])
2627

28+
// +nodoc
2729
minor: int & >=minMinor
2830
minor: strconv.Atoi(strings.Split(#Version, ".")[1])
2931
}

blueprints/starter/templates/config.cue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import (
99
#Config: {
1010
// The kubeVersion is a required field, set at apply-time
1111
// via timoni.cue by querying the user's Kubernetes API.
12+
// +nodoc
1213
kubeVersion!: string
1314
// Using the kubeVersion you can enforce a minimum Kubernetes minor version.
1415
// By default, the minimum Kubernetes version is set to 1.20.
16+
// +nodoc
1517
clusterVersion: timoniv1.#SemVer & {#Version: kubeVersion, #Minimum: "1.20.0"}
1618

1719
// The moduleVersion is set from the user-supplied module version.
1820
// This field is used for the `app.kubernetes.io/version` label.
21+
// +nodoc
1922
moduleVersion!: string
2023

2124
// The Kubernetes metadata common to all resources.

cmd/timoni/mod_show_config.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ import (
4040

4141
var configShowModCmd = &cobra.Command{
4242
Use: "config [MODULE PATH]",
43-
Short: "Output the #Config structure of a local module",
43+
Short: "Output the `#Config` structure of a local module",
4444
Long: `The config command parses the local module configuration structure and outputs the information to stdout.`,
4545
Example: ` # print the config of a module in the current directory
4646
timoni mod show config
4747
48-
# output the config to a file, if the file is markdown, the table will overwrite a table in a Configuration section or
49-
# be appended to the end of the file
48+
# output the config to a file, if the file is markdown, the table will overwrite a table in the ## Configuration section
49+
# under the sub heading ### General values or be appended to the end of the file
5050
timoni mod show config --output ./README.md
5151
`,
5252
RunE: runConfigShowModCmd,
@@ -115,7 +115,7 @@ func runConfigShowModCmd(cmd *cobra.Command, args []string) error {
115115
configShowModArgs.pkg.String(),
116116
)
117117

118-
if err := builder.WriteSchemaFile(); err != nil {
118+
if err = builder.WriteSchemaFile(); err != nil {
119119
return err
120120
}
121121

@@ -124,17 +124,16 @@ func runConfigShowModCmd(cmd *cobra.Command, args []string) error {
124124
return fmt.Errorf("build failed: %w", err)
125125
}
126126

127-
buildResult, err := builder.Build()
127+
_, err = builder.Build()
128128
if err != nil {
129129
return describeErr(f.GetModuleRoot(), "validation failed", err)
130130
}
131-
132-
rows, err := builder.GetConfigDoc(buildResult)
131+
rows, err := builder.GetConfigDoc()
133132
if err != nil {
134133
return describeErr(f.GetModuleRoot(), "failed to get config structure", err)
135134
}
136135

137-
header := []string{"Key", "Type", "Default", "Description"}
136+
header := []string{"Key", "Type", "Description"}
138137

139138
if configShowModArgs.output == "" {
140139
printMarkDownTable(rootCmd.OutOrStdout(), header, rows)
@@ -158,36 +157,49 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc
158157
var tableBuffer bytes.Buffer
159158
tableWriter := bufio.NewWriter(&tableBuffer)
160159
printMarkDownTable(tableWriter, header, rows)
161-
tableWriter.Flush()
160+
err := tableWriter.Flush()
161+
if err != nil {
162+
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
163+
}
162164
// get a temporary file name
163165
tmpFileName := readFile + ".tmp"
164166
// open the input file
165167
inputFile, err := os.Open(readFile)
166168
if err != nil {
167169
if errors.Is(err, fs.ErrNotExist) {
168170
inputFile, err = os.Create(readFile)
169-
170171
if err != nil {
171172
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
172173
}
173174
} else {
174175
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
175176
}
176177
}
177-
defer inputFile.Close()
178+
fileClose := func(file *os.File) {
179+
err = file.Close()
180+
if err != nil {
181+
fmt.Println(describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err))
182+
}
183+
}
184+
defer fileClose(inputFile)
178185

179186
// open the output file
180187
outputFile, err := os.Create(tmpFileName)
181188
if err != nil {
182189
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
183190
}
184-
defer outputFile.Close()
191+
defer fileClose(outputFile)
185192

186193
// Create the scanner and writer
187194
inputScanner := bufio.NewScanner(inputFile)
188195
outputWriter := bufio.NewWriter(outputFile)
189196
var configSection bool
197+
var generalValuesSection bool
190198
var foundTable bool
199+
regex, err := regexp.Compile(`^\|.*\|$`)
200+
if err != nil {
201+
return "", describeErr(f.GetModuleRoot(), "Unalbe to compile the regex check", err)
202+
}
191203

192204
// Scan the input file line by line to find the table and replace it or append it to the end
193205
for inputScanner.Scan() {
@@ -197,29 +209,40 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc
197209
if !configSection && line == "## Configuration" {
198210
configSection = true
199211
}
200-
201-
matched, err := regexp.MatchString(`^\|.*\|$`, line)
202-
if err != nil {
203-
return "", describeErr(f.GetModuleRoot(), "Regex Match for table content failed", err)
212+
if !generalValuesSection && line == "### General values" {
213+
generalValuesSection = true
204214
}
205215

206-
if configSection && !foundTable && matched {
216+
matched := regex.MatchString(line)
217+
if configSection && generalValuesSection && !foundTable && matched {
207218
foundTable = true
208-
outputWriter.WriteString(tableBuffer.String() + "\n")
209-
} else if configSection && foundTable && matched {
210-
} else if configSection && foundTable && !matched {
219+
_, err = outputWriter.WriteString(tableBuffer.String() + "\n")
220+
if err != nil {
221+
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
222+
}
223+
} else if configSection && generalValuesSection && foundTable && matched {
224+
} else if configSection && generalValuesSection && foundTable && !matched {
211225
configSection = false
212226
} else {
213-
outputWriter.WriteString(line + "\n")
227+
_, err = outputWriter.WriteString(line + "\n")
228+
if err != nil {
229+
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
230+
}
214231
}
215232
} else {
216-
outputWriter.WriteString(line + "\n")
233+
_, err = outputWriter.WriteString(line + "\n")
234+
if err != nil {
235+
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
236+
}
217237
}
218238
}
219239

220240
// If no table was found, append it to the end of the file
221241
if !foundTable {
222-
outputWriter.WriteString("\n" + tableBuffer.String())
242+
_, err = outputWriter.WriteString("\n" + tableBuffer.String())
243+
if err != nil {
244+
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
245+
}
223246
}
224247

225248
err = outputWriter.Flush()

0 commit comments

Comments
 (0)