-
Notifications
You must be signed in to change notification settings - Fork 4
/
usage_generator.go
50 lines (42 loc) · 1.28 KB
/
usage_generator.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
package main
import (
"bytes"
"context"
"fmt"
"text/template"
"github.com/iancoleman/strcase"
)
var usageFuncMap = template.FuncMap{
"BackTick": func() string {
return "```"
},
"ToPascalCase": strcase.ToCamel,
"ToSnakeCase": strcase.ToSnake,
}
// UsageGenerator generates a simple usage documentation for a module.
//
// This function is still in developement, it's a simple utility functions
// to simplify modules maintaing.
//
// Use it as a starting point for your module README but please, do not
// consider it as a README generator.
//
// Example usage: dagger call usage-generator --module=. -o USAGE.md
func (d *DaggerverseCockpit) UsageGenerator(
ctx context.Context,
module *Directory,
) (*File, error) {
introspection, err := d.introspectModule(ctx, module)
if err != nil {
return nil, fmt.Errorf("failed to introspect the module: %w", err)
}
tmpl, err := template.New("usage").Funcs(usageFuncMap).Parse(usageTemplate)
if err != nil {
return nil, fmt.Errorf("failed to create the usage template: %w", err)
}
var result bytes.Buffer
if err = tmpl.Execute(&result, introspection[0]); err != nil {
return nil, fmt.Errorf("failed to execute the usage template: %w", err)
}
return dag.Directory().WithNewFile("USAGE.md", result.String()).File("USAGE.md"), nil
}