@@ -2,11 +2,12 @@ package docs
2
2
3
3
import (
4
4
"bytes"
5
+ "errors"
5
6
"fmt"
7
+ "io"
6
8
"log"
7
9
"os"
8
10
"path/filepath"
9
- "strings"
10
11
"text/template"
11
12
12
13
"github.com/opendevstack/ods-pipeline/internal/command"
@@ -37,20 +38,11 @@ func renderTemplate(targetDir, targetFilename string, data Task) error {
37
38
if err != nil {
38
39
return err
39
40
}
40
- templateFilename := filepath .Join (targetDir , "template.adoc.tmpl" )
41
- templateFileParts := strings .Split (templateFilename , "/" )
42
- templateDisplayname := templateFileParts [len (templateFileParts )- 1 ]
43
- _ , err = targetFile .WriteString (
44
- "// Document generated by internal/documentation/tasks.go from " + templateDisplayname + "; DO NOT EDIT.\n \n " ,
45
- )
41
+ tmpl , err := template .ParseFiles (filepath .Join (targetDir , "template.adoc.tmpl" ))
46
42
if err != nil {
47
43
return err
48
44
}
49
- tmpl , err := template .ParseFiles (templateFilename )
50
- if err != nil {
51
- return err
52
- }
53
- return tmpl .Execute (targetFile , data )
45
+ return RenderTaskDocumentation (targetFile , tmpl , & data )
54
46
}
55
47
56
48
func parseTasks (helmTemplateOutput []byte ) ([]* tekton.Task , error ) {
@@ -131,3 +123,47 @@ func RenderTasks(tasksSourceDir, descriptionsSourceDir, targetDir string) error
131
123
}
132
124
return nil
133
125
}
126
+
127
+ func ParseTask (f []byte , desc []byte ) (* Task , error ) {
128
+ var t tekton.Task
129
+ err := yaml .Unmarshal (f , & t )
130
+ if err != nil {
131
+ return nil , err
132
+ }
133
+ if t .Name == "" {
134
+ return nil , errors .New ("encountered empty name, something is wrong with the task" )
135
+ }
136
+ task := & Task {
137
+ Name : t .Name ,
138
+ Description : string (desc ),
139
+ Params : []Param {},
140
+ Results : []Result {},
141
+ }
142
+ for _ , p := range t .Spec .Params {
143
+ defaultValue := ""
144
+ if p .Default != nil {
145
+ defaultValue = p .Default .StringVal
146
+ }
147
+ task .Params = append (task .Params , Param {
148
+ Name : p .Name ,
149
+ Default : defaultValue ,
150
+ Description : p .Description ,
151
+ })
152
+ }
153
+ for _ , r := range t .Spec .Results {
154
+ task .Results = append (task .Results , Result {
155
+ Name : r .Name ,
156
+ Description : r .Description ,
157
+ })
158
+ }
159
+ return task , nil
160
+ }
161
+
162
+ func RenderTaskDocumentation (w io.Writer , tmpl * template.Template , task * Task ) error {
163
+ if _ , err := w .Write (
164
+ []byte ("// File is generated; DO NOT EDIT.\n \n " ),
165
+ ); err != nil {
166
+ return err
167
+ }
168
+ return tmpl .Execute (w , task )
169
+ }
0 commit comments