Skip to content

Commit 9401987

Browse files
authored
Merge pull request #464 from Unpackerr/dn2_never_ends
More generator changes
2 parents 05da8b0 + ee1f5e4 commit 9401987

8 files changed

Lines changed: 96 additions & 80 deletions

File tree

examples/docker-compose.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ services:
1616
# Get the user:group correct so unpackerr can read and write to your files.
1717
user: ${PUID}:${PGID}
1818
#user: 1000:100
19-
# What you see below are defaults for this compose. You only need to modify things specific to your environment.
20-
# Remove apps and feature configs you do not use or need.
21-
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK, UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
19+
# What you see below are defaults mixed with examples where examples make more sense than the default.
20+
# You only need to modify things specific to your environment.
21+
# Remove apps and feature configs you do not use or need.
22+
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK,
23+
# UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
2224
environment:
2325
- TZ=${TZ}
2426
## Global Settings
@@ -132,3 +134,5 @@ services:
132134
- UN_CMDHOOK_0_EXCLUDE_0=readarr
133135
- UN_CMDHOOK_0_EXCLUDE_1=lidarr
134136
- UN_CMDHOOK_0_TIMEOUT=10s
137+
138+
## => Content Auto Generated, 17 JUL 2024 08:35 UTC

examples/unpackerr.conf.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,4 @@ dir_mode = "0755"
301301
## You can adjust how long to wait for the command to run.
302302
# timeout = "10s"
303303

304+
## => Content Auto Generated, 17 JUL 2024 08:35 UTC

init/config/compose.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"bytes"
55
"fmt"
66
"log"
7-
"os"
87
"strings"
98
)
109

1110
/* This file creates an example compose file: docker-compose.yml */
1211

13-
//nolint:lll
1412
const (
1513
space = " "
1614
composeHeader = `### Unpackerr docker-compose.yml Example
@@ -31,22 +29,24 @@ services:
3129
# Get the user:group correct so unpackerr can read and write to your files.
3230
user: ${PUID}:${PGID}
3331
#user: 1000:100
34-
# What you see below are defaults for this compose. You only need to modify things specific to your environment.
35-
# Remove apps and feature configs you do not use or need.
36-
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK, UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
32+
# What you see below are defaults mixed with examples where examples make more sense than the default.
33+
# You only need to modify things specific to your environment.
34+
# Remove apps and feature configs you do not use or need.
35+
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK,
36+
# UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
3737
environment:
3838
- TZ=${TZ}`
3939
)
4040

41-
func createCompose(config *Config, output string) {
41+
func createCompose(config *Config, output, dir string) {
4242
buf := bytes.Buffer{}
4343
buf.WriteString(composeHeader + "\n")
4444

4545
// Loop the 'Order' list.
4646
for _, section := range config.Order {
4747
// If Order contains a missing section, bail.
4848
if config.Sections[section] == nil {
49-
log.Fatalln(section + ": in order, but missing from sections. This is a bug in conf-builder.yml.")
49+
log.Fatalln(section + ": in order, but missing from sections. This is a bug in definitions.yml.")
5050
}
5151

5252
if config.Defs[section] == nil {
@@ -57,11 +57,8 @@ func createCompose(config *Config, output string) {
5757
}
5858
}
5959

60-
log.Println("Writing", output, "size:", buf.Len())
61-
62-
if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
63-
log.Fatalln(err)
64-
}
60+
buf.WriteString("\n")
61+
writeFile(dir, output, &buf)
6562
}
6663

6764
func (h *Header) makeCompose(prefix string, bare bool) string {

init/config/config.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import (
44
"bytes"
55
"fmt"
66
"log"
7-
"os"
87
"strings"
98

109
"github.com/BurntSushi/toml"
1110
)
1211

13-
func printConfFile(config *Config, output string) {
12+
/* This file creates an example config file: unpackerr.conf.example */
13+
14+
func createConfFile(config *Config, output, dir string) {
1415
buf := bytes.Buffer{}
1516

1617
// Loop the 'Order' list.
1718
for _, section := range config.Order {
1819
// If Order contains a missing section, bail.
1920
if config.Sections[section] == nil {
20-
log.Fatalln(section + ": in order, but missing from sections. This is a bug in conf-builder.yml.")
21+
log.Fatalln(section + ": in order, but missing from sections. This is a bug in definitions.yml.")
2122
}
2223

2324
if config.Defs[section] != nil {
@@ -27,11 +28,7 @@ func printConfFile(config *Config, output string) {
2728
}
2829
}
2930

30-
log.Println("Writing", output, "size:", buf.Len())
31-
32-
if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
33-
log.Fatalln(err)
34-
}
31+
writeFile(dir, output, &buf)
3532
}
3633

3734
// Not all sections have defs, and it may be nil. Defs only work on 'list' sections.

init/config/definitions.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ recommendations:
122122
- name: 24 hours
123123
value: 24h
124124
folders: &FOLDER_INTERVALS
125+
- name: Default (1s in Docker)
126+
value: 0s
125127
- name: Disabled
126128
value: 1ms
127129
- name: 1/2 second

init/config/docusaurus.go

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
/* This file creates a folder full of docusaurus markdown files for https://unpackerr.zip */
1717

18-
func printDocusaurus(config *Config, output string) {
18+
func createDocusaurus(config *Config, output string) {
1919
// Generate index file first.
2020
if err := makeGenerated(config, output); err != nil {
2121
log.Fatalln(err)
@@ -24,14 +24,15 @@ func printDocusaurus(config *Config, output string) {
2424
for _, section := range config.Order {
2525
// If Order contains a missing section, bail.
2626
if config.Sections[section] == nil {
27-
log.Fatalln(section + ": in order, but missing from sections. This is a bug in conf-builder.yml.")
27+
log.Fatalln(section + ": in order, but missing from sections. This is a bug in definitions.yml.")
2828
}
29-
29+
// We only care about sections with parameters defined.
3030
if len(config.Sections[section].Params) < 1 {
3131
continue
3232
}
3333

3434
if config.Defs[section] != nil {
35+
// Repeat this section based on defined definitions.
3536
data := config.Sections[section].makeDefinedDocs(config.Prefix, config.Defs[section], config.DefOrder[section])
3637
if err := writeDocusaurus(output, string(section), data); err != nil {
3738
log.Fatalln(err)
@@ -47,72 +48,74 @@ func printDocusaurus(config *Config, output string) {
4748

4849
func writeDocusaurus(dir, file, content string) error {
4950
_ = os.Mkdir(dir, dirMode)
50-
date := "---\n# Generated: " + time.Now().Round(time.Second).String() + "\n---\n\n"
51+
date := "---\n## => Content Auto Generated, " +
52+
strings.ToUpper(time.Now().UTC().Round(time.Second).Format("02 Jan 2006 15:04 UTC")) + "\n---\n\n"
5153
filePath := filepath.Join(dir, file+".md")
5254
log.Printf("Writing: %s, size: %d", filePath, len(content))
5355
//nolint:wrapcheck
5456
return os.WriteFile(filePath, []byte(date+content), fileMode)
5557
}
5658

57-
// makeGenerated writes a special file that the website can import.
59+
// makeGenerated writes a special index file that the website can import.
5860
// Adds all param sections except global into a docusaurus import format.
61+
// Also creates a footer file that can be imported and displayed.
5962
func makeGenerated(config *Config, output string) error {
60-
var (
61-
first bytes.Buffer
62-
second bytes.Buffer
63-
)
63+
var first, second bytes.Buffer
6464

6565
for _, section := range config.Order {
6666
if len(config.Sections[section].Params) > 0 && section != "global" {
67-
title := "G" + string(section)
68-
first.WriteString("import " + title + " from './" + string(section) + ".md';\n")
69-
second.WriteString("<" + title + "/>\n")
67+
first.WriteString("import G" + string(section) + " from './" + string(section) + ".md';\n")
68+
second.WriteString("<G" + string(section) + "/>\n")
7069
}
7170
}
7271

73-
return writeDocusaurus(output, "index", first.String()+"\n"+second.String())
72+
err := writeDocusaurus(output, "index", first.String()+"\n"+second.String())
73+
if err != nil {
74+
return err
75+
}
76+
77+
return writeDocusaurus(output, "footer", `<font color="gray" style={{'float': 'right', 'font-style': 'italic'}}>`+
78+
"This page was [generated automatically](https://github.com/Unpackerr/unpackerr/tree/main/init/config), "+
79+
strings.ToUpper(time.Now().UTC().Round(time.Second).Format("02 Jan 2006 15:04 UTC"))+"</font>\n")
7480
}
7581

7682
func (h *Header) makeDocs(prefix string, section section) string {
77-
buf := bytes.Buffer{}
78-
buf.WriteString("## " + h.Title + "\n\n<details>\n")
83+
conf := h.makeSection(section, true, true) // Generate this portion of the config file.
84+
env := h.makeCompose(prefix, true) // Generate this portion of the docker-compose example.
7985

80-
conf := h.makeSection(section, true, true)
81-
env := h.makeCompose(prefix, true)
82-
header := "[" + string(section) + "]"
86+
buf := bytes.Buffer{}
87+
buf.WriteString("## " + h.Title + "\n\n<details>\n <summary>Examples. Prefix: <b>" + prefix)
8388

84-
if h.Kind == list {
85-
header = "[[" + string(section) + "]]"
86-
}
89+
if !h.NoHeader {
90+
brace1, brace2 := "[", "]"
91+
if h.Kind == list {
92+
brace1, brace2 = "[[", "]]"
93+
}
8794

88-
if h.NoHeader {
89-
buf.WriteString(" <summary>Examples. Prefix: <b>" + prefix + "</b></summary>\n\n")
90-
} else {
91-
buf.WriteString(" <summary>Examples. Prefix: <b>" + prefix + h.Prefix +
92-
"</b>, Header: <b>" + header + "</b></summary>\n\n")
95+
buf.WriteString(h.Prefix + "</b>, Header: <b> ") // Add to the line above.
96+
buf.WriteString(brace1 + string(section) + brace2) // Add to the line above.
9397
}
9498

99+
buf.WriteString("</b></summary>\n\n") // Add to the line above.
95100
buf.WriteString("- Using the config file:\n\n```yaml\n")
96101
buf.WriteString(strings.TrimSpace(conf) + "\n```\n\n")
97102
buf.WriteString("- Using environment variables:\n\n```js\n")
98103
buf.WriteString(env + "```\n\n</details>\n\n")
99-
buf.WriteString(h.Docs + "\n") // Docs comes before the table.
100-
buf.WriteString(h.makeDocsTable(prefix) + "\n")
101-
buf.WriteString(h.Tail) // Tail goes after the docs and table.
104+
buf.WriteString(h.Docs + "\n" + h.makeDocsTable(prefix) + "\n" + h.Tail)
102105

103106
if h.Notes != "" { // Notes become a sub header.
104-
buf.WriteString("### " + h.Title + " Notes\n\n" + h.Notes)
107+
buf.WriteString("### Notes for " + h.Title + "\n\n" + h.Notes)
105108
}
106109

107110
return buf.String()
108111
}
109112

110-
func (h *Header) makeDocsTable(prefix string) string {
111-
const (
112-
tableHeader = "|Config Name|Variable Name|Default / Note|\n|---|---|---|\n"
113-
tableFormat = "|%s|`%s`|%v / %s|\n"
114-
)
113+
const (
114+
tableHeader = "|Config Name|Variable Name|Default / Note|\n|---|---|---|\n"
115+
tableFormat = "|%s|`%s`|%v / %s|\n"
116+
)
115117

118+
func (h *Header) makeDocsTable(prefix string) string {
116119
buf := bytes.Buffer{}
117120
buf.WriteString(tableHeader)
118121

init/config/main.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate go run . --config ../../examples/unpackerr.conf.example --compose ../../examples/docker-compose.yml --type config,compose --file definitions.yml
1+
//go:generate go run . --type config,compose --file definitions.yml --output ../../examples
22

33
package main
44

@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"net/http"
1212
"os"
13+
"path/filepath"
1314
"strings"
1415
"time"
1516

@@ -97,13 +98,13 @@ func main() {
9798
switch builder {
9899
case "doc", "docs", "documentation", "docusaurus":
99100
log.Println("Building Docusaurus")
100-
printDocusaurus(config, flags.Docs)
101+
createDocusaurus(config, flags.Output)
101102
case "conf", "config", "example":
102103
log.Println("Building Config File")
103-
printConfFile(config, flags.Config)
104+
createConfFile(config, flags.Config, flags.Output)
104105
case "docker", "compose", "yml":
105106
log.Println("Building Docker Compose")
106-
createCompose(config, flags.Compose)
107+
createCompose(config, flags.Compose, flags.Output)
107108
default:
108109
log.Println("Unknown type: " + builder)
109110
}
@@ -114,7 +115,7 @@ type flags struct {
114115
Type []string
115116
Config string
116117
Compose string
117-
Docs string
118+
Output string
118119
File string
119120
}
120121

@@ -126,8 +127,8 @@ func parseFlags() *flags {
126127
"Choose filename for generated config file.")
127128
flag.StringVar(&flags.Compose, "compose", exampleCompose,
128129
"Choose a filename for the generated docker compose service.")
129-
flag.StringVar(&flags.Docs, "docs", outputDir,
130-
"Choose folder for generated documentation.")
130+
flag.StringVar(&flags.Output, "output", outputDir,
131+
"Choose folder for generated files.")
131132
flag.StringVarP(&flags.File, "file", "f", "internal",
132133
"URL or filepath for definitions.yml, 'internal' uses the compiled-in file.")
133134
flag.Parse()
@@ -138,29 +139,27 @@ func parseFlags() *flags {
138139
// openFile opens a file or url for the parser, or returns the internal file.
139140
func openFile(fileName string) (io.ReadCloser, error) {
140141
if fileName == "internal" {
141-
buf := bytes.Buffer{}
142-
buf.Write(confBuilder)
143-
144-
return io.NopCloser(&buf), nil
142+
buf := bytes.NewBuffer(confBuilder)
143+
return io.NopCloser(buf), nil
145144
}
146145

147-
if strings.HasPrefix(fileName, "http") {
148-
http.DefaultClient.Timeout = opTimeout
149-
150-
resp, err := http.Get(fileName) //nolint:noctx,gosec // because we set a timeout.
146+
if !strings.HasPrefix(fileName, "http") {
147+
file, err := os.Open(fileName)
151148
if err != nil {
152149
return nil, fmt.Errorf("%s: %w", fileName, err)
153150
}
154151

155-
return resp.Body, nil
152+
return file, nil
156153
}
157154

158-
file, err := os.Open(fileName)
155+
http.DefaultClient.Timeout = opTimeout
156+
//nolint:noctx,gosec // because we set a timeout.
157+
resp, err := http.Get(fileName)
159158
if err != nil {
160159
return nil, fmt.Errorf("%s: %w", fileName, err)
161160
}
162161

163-
return file, nil
162+
return resp.Body, nil
164163
}
165164

166165
func createDefinedSection(def *Def, section *Header) *Header {
@@ -184,3 +183,16 @@ func createDefinedSection(def *Def, section *Header) *Header {
184183

185184
return newSection
186185
}
186+
187+
// This is used only by compose and config. docs has it's own.
188+
func writeFile(dir, output string, buf *bytes.Buffer) {
189+
_ = os.Mkdir(dir, dirMode)
190+
filePath := filepath.Join(dir, output)
191+
log.Printf("Writing: %s, size: %d", filePath, buf.Len())
192+
buf.WriteString("## => Content Auto Generated, " +
193+
strings.ToUpper(time.Now().UTC().Round(time.Second).Format("02 Jan 2006 15:04 UTC")+"\n"))
194+
195+
if err := os.WriteFile(filePath, buf.Bytes(), fileMode); err != nil {
196+
log.Fatalln(err)
197+
}
198+
}

pkg/bindata/bindata.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)