-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcommands_generate.go
211 lines (188 loc) · 6.16 KB
/
commands_generate.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"embed"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"text/template"
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/config/jsonschema"
"github.com/creativeprojects/resticprofile/restic"
"github.com/creativeprojects/resticprofile/util/templates"
)
const pathTemplates = "contrib/templates"
//go:embed contrib/completion/bash-completion.sh
var bashCompletionScript string
//go:embed contrib/completion/zsh-completion.sh
var zshCompletionScript string
func generateCommand(output io.Writer, ctx commandContext) (err error) {
args := ctx.request.arguments
// enforce no-log
logger := clog.GetDefaultLogger()
handler := logger.GetHandler()
logger.SetHandler(clog.NewDiscardHandler())
if slices.Contains(args, "--bash-completion") {
_, err = fmt.Fprintln(output, bashCompletionScript)
} else if slices.Contains(args, "--config-reference") {
err = generateConfigReference(output, args[slices.Index(args, "--config-reference")+1:])
} else if slices.Contains(args, "--json-schema") {
err = generateJsonSchema(output, args[slices.Index(args, "--json-schema")+1:])
} else if slices.Contains(args, "--random-key") {
ctx.flags.resticArgs = args[slices.Index(args, "--random-key"):]
err = randomKey(output, ctx)
} else if slices.Contains(args, "--zsh-completion") {
_, err = fmt.Fprintln(output, zshCompletionScript)
} else {
err = fmt.Errorf("nothing to generate for: %s", strings.Join(args, ", "))
}
if err != nil {
logger.SetHandler(handler)
}
return
}
//go:embed contrib/templates/*
var configReferenceTemplates embed.FS
func generateConfigReference(output io.Writer, args []string) error {
resticVersion := restic.AnyVersion
destination := "docs/content/reference"
if slices.Contains(args, "--version") {
args = args[slices.Index(args, "--version"):]
if len(args) > 1 {
resticVersion = args[1]
args = args[2:]
}
}
if slices.Contains(args, "--to") {
args = args[slices.Index(args, "--to"):]
if len(args) > 1 {
destination = args[1]
args = args[2:]
}
}
data := config.NewTemplateInfoData(resticVersion)
tpl := templates.New("config-reference", data.GetFuncs())
templates, err := fs.Sub(configReferenceTemplates, pathTemplates)
if err != nil {
return fmt.Errorf("cannot load templates: %w", err)
}
if len(args) > 0 {
tpl, err = tpl.ParseFiles(args...)
} else {
tpl, err = tpl.ParseFS(templates, "*.gomd")
}
if err != nil {
return fmt.Errorf("parsing failed: %w", err)
}
staticPages := []struct {
templateName string
fileName string
}{
{"reference.gomd", "_index.md"},
{"global.gomd", "global.md"},
{"profile.gomd", "profile/_index.md"},
{"nested.gomd", "nested/_index.md"},
{"groups.gomd", "groups.md"},
{"value-types.gomd", "value-types.md"},
{"json-schema.gomd", "json-schema.md"},
}
for _, staticPage := range staticPages {
fmt.Fprintf(output, "generating %s...\n", staticPage.templateName)
err = generateFileFromTemplate(tpl, data, filepath.Join(destination, staticPage.fileName), staticPage.templateName)
if err != nil {
return fmt.Errorf("unable to generate page %s: %w", staticPage.fileName, err)
}
}
weight := 1
for _, profileSection := range data.ProfileSections() {
fmt.Fprintf(output, "generating profile section %s (weight %d)...\n", profileSection.Name(), weight)
sectionData := SectionInfoData{
DefaultData: data.DefaultData,
Section: profileSection,
Weight: weight,
}
err = generateFileFromTemplate(tpl, sectionData, filepath.Join(destination, "profile", profileSection.Name()+".md"), "profile.sub-section.gomd")
if err != nil {
return fmt.Errorf("unable to generate profile section %s: %w", profileSection.Name(), err)
}
weight++
}
weight = 1
for _, nestedSection := range data.NestedSections() {
fmt.Fprintf(output, "generating nested section %s (weight %d)...\n", nestedSection.Name(), weight)
sectionData := SectionInfoData{
DefaultData: data.DefaultData,
Section: nestedSection,
Weight: weight,
}
err = generateFileFromTemplate(tpl, sectionData, filepath.Join(destination, "nested", nestedSection.Name()+".md"), "profile.nested-section.gomd")
if err != nil {
return fmt.Errorf("unable to generate nested section %s: %w", nestedSection.Name(), err)
}
weight++
}
return nil
}
func generateFileFromTemplate(tpl *template.Template, data any, fileName, templateName string) error {
err := os.MkdirAll(filepath.Dir(fileName), 0o755)
if err != nil {
return fmt.Errorf("cannot create directory: %w", err)
}
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("cannot open file: %w", err)
}
defer file.Close()
err = tpl.ExecuteTemplate(file, templateName, data)
if err != nil {
return fmt.Errorf("cannot execute template: %w", err)
}
return nil
}
func generateJsonSchema(output io.Writer, args []string) (err error) {
resticVersion := restic.AnyVersion
if slices.Contains(args, "--version") {
args = args[slices.Index(args, "--version"):]
if len(args) > 1 {
resticVersion = args[1]
args = args[2:]
}
}
if len(args) == 0 {
return fmt.Errorf("missing type of json schema to generate (global, v1, v2)")
}
switch args[0] {
case "global":
data := config.NewTemplateInfoData(resticVersion)
tpl := templates.New("", data.GetFuncs())
templates, err := fs.Sub(configReferenceTemplates, pathTemplates)
if err != nil {
return fmt.Errorf("cannot load templates: %w", err)
}
tpl, err = tpl.ParseFS(templates, "config-schema.gojson")
if err != nil {
return fmt.Errorf("parsing failed: %w", err)
}
err = tpl.ExecuteTemplate(output, "config-schema.gojson", data)
if err != nil {
return fmt.Errorf("cannot execute template: %w", err)
}
return nil
case "v1":
return jsonschema.WriteJsonSchema(config.Version01, resticVersion, output)
case "v2":
return jsonschema.WriteJsonSchema(config.Version02, resticVersion, output)
default:
return fmt.Errorf("unknown json schema type: %s", args[0])
}
}
// SectionInfoData is used as data for go templates that render profile section references
type SectionInfoData struct {
templates.DefaultData
Section config.SectionInfo
Weight int
}