Skip to content

Commit

Permalink
Merge pull request #464 from Unpackerr/dn2_never_ends
Browse files Browse the repository at this point in the history
More generator changes
  • Loading branch information
davidnewhall committed Jul 17, 2024
2 parents 05da8b0 + ee1f5e4 commit 9401987
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 80 deletions.
10 changes: 7 additions & 3 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ services:
# Get the user:group correct so unpackerr can read and write to your files.
user: ${PUID}:${PGID}
#user: 1000:100
# What you see below are defaults for this compose. You only need to modify things specific to your environment.
# Remove apps and feature configs you do not use or need.
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK, UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
# What you see below are defaults mixed with examples where examples make more sense than the default.
# You only need to modify things specific to your environment.
# Remove apps and feature configs you do not use or need.
# ie. Remove all lines that begin with UN_CMDHOOK, UN_WEBHOOK,
# UN_FOLDER, UN_WEBSERVER, and other apps you do not use.
environment:
- TZ=${TZ}
## Global Settings
Expand Down Expand Up @@ -132,3 +134,5 @@ services:
- UN_CMDHOOK_0_EXCLUDE_0=readarr
- UN_CMDHOOK_0_EXCLUDE_1=lidarr
- UN_CMDHOOK_0_TIMEOUT=10s

## => Content Auto Generated, 17 JUL 2024 08:35 UTC
1 change: 1 addition & 0 deletions examples/unpackerr.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,4 @@ dir_mode = "0755"
## You can adjust how long to wait for the command to run.
# timeout = "10s"

## => Content Auto Generated, 17 JUL 2024 08:35 UTC
21 changes: 9 additions & 12 deletions init/config/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"bytes"
"fmt"
"log"
"os"
"strings"
)

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

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

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

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

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

log.Println("Writing", output, "size:", buf.Len())

if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
log.Fatalln(err)
}
buf.WriteString("\n")
writeFile(dir, output, &buf)
}

func (h *Header) makeCompose(prefix string, bare bool) string {
Expand Down
13 changes: 5 additions & 8 deletions init/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"bytes"
"fmt"
"log"
"os"
"strings"

"github.com/BurntSushi/toml"
)

func printConfFile(config *Config, output string) {
/* This file creates an example config file: unpackerr.conf.example */

func createConfFile(config *Config, output, dir string) {
buf := bytes.Buffer{}

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

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

log.Println("Writing", output, "size:", buf.Len())

if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
log.Fatalln(err)
}
writeFile(dir, output, &buf)
}

// Not all sections have defs, and it may be nil. Defs only work on 'list' sections.
Expand Down
2 changes: 2 additions & 0 deletions init/config/definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ recommendations:
- name: 24 hours
value: 24h
folders: &FOLDER_INTERVALS
- name: Default (1s in Docker)
value: 0s
- name: Disabled
value: 1ms
- name: 1/2 second
Expand Down
73 changes: 38 additions & 35 deletions init/config/docusaurus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

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

func printDocusaurus(config *Config, output string) {
func createDocusaurus(config *Config, output string) {
// Generate index file first.
if err := makeGenerated(config, output); err != nil {
log.Fatalln(err)
Expand All @@ -24,14 +24,15 @@ func printDocusaurus(config *Config, output string) {
for _, section := range config.Order {
// If Order contains a missing section, bail.
if config.Sections[section] == nil {
log.Fatalln(section + ": in order, but missing from sections. This is a bug in conf-builder.yml.")
log.Fatalln(section + ": in order, but missing from sections. This is a bug in definitions.yml.")
}

// We only care about sections with parameters defined.
if len(config.Sections[section].Params) < 1 {
continue
}

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

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

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

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

return writeDocusaurus(output, "index", first.String()+"\n"+second.String())
err := writeDocusaurus(output, "index", first.String()+"\n"+second.String())
if err != nil {
return err
}

return writeDocusaurus(output, "footer", `<font color="gray" style={{'float': 'right', 'font-style': 'italic'}}>`+
"This page was [generated automatically](https://github.com/Unpackerr/unpackerr/tree/main/init/config), "+
strings.ToUpper(time.Now().UTC().Round(time.Second).Format("02 Jan 2006 15:04 UTC"))+"</font>\n")
}

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

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

if h.Kind == list {
header = "[[" + string(section) + "]]"
}
if !h.NoHeader {
brace1, brace2 := "[", "]"
if h.Kind == list {
brace1, brace2 = "[[", "]]"
}

if h.NoHeader {
buf.WriteString(" <summary>Examples. Prefix: <b>" + prefix + "</b></summary>\n\n")
} else {
buf.WriteString(" <summary>Examples. Prefix: <b>" + prefix + h.Prefix +
"</b>, Header: <b>" + header + "</b></summary>\n\n")
buf.WriteString(h.Prefix + "</b>, Header: <b> ") // Add to the line above.
buf.WriteString(brace1 + string(section) + brace2) // Add to the line above.
}

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

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

return buf.String()
}

func (h *Header) makeDocsTable(prefix string) string {
const (
tableHeader = "|Config Name|Variable Name|Default / Note|\n|---|---|---|\n"
tableFormat = "|%s|`%s`|%v / %s|\n"
)
const (
tableHeader = "|Config Name|Variable Name|Default / Note|\n|---|---|---|\n"
tableFormat = "|%s|`%s`|%v / %s|\n"
)

func (h *Header) makeDocsTable(prefix string) string {
buf := bytes.Buffer{}
buf.WriteString(tableHeader)

Expand Down
48 changes: 30 additions & 18 deletions init/config/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run . --config ../../examples/unpackerr.conf.example --compose ../../examples/docker-compose.yml --type config,compose --file definitions.yml
//go:generate go run . --type config,compose --file definitions.yml --output ../../examples

package main

Expand All @@ -10,6 +10,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -97,13 +98,13 @@ func main() {
switch builder {
case "doc", "docs", "documentation", "docusaurus":
log.Println("Building Docusaurus")
printDocusaurus(config, flags.Docs)
createDocusaurus(config, flags.Output)
case "conf", "config", "example":
log.Println("Building Config File")
printConfFile(config, flags.Config)
createConfFile(config, flags.Config, flags.Output)
case "docker", "compose", "yml":
log.Println("Building Docker Compose")
createCompose(config, flags.Compose)
createCompose(config, flags.Compose, flags.Output)
default:
log.Println("Unknown type: " + builder)
}
Expand All @@ -114,7 +115,7 @@ type flags struct {
Type []string
Config string
Compose string
Docs string
Output string
File string
}

Expand All @@ -126,8 +127,8 @@ func parseFlags() *flags {
"Choose filename for generated config file.")
flag.StringVar(&flags.Compose, "compose", exampleCompose,
"Choose a filename for the generated docker compose service.")
flag.StringVar(&flags.Docs, "docs", outputDir,
"Choose folder for generated documentation.")
flag.StringVar(&flags.Output, "output", outputDir,
"Choose folder for generated files.")
flag.StringVarP(&flags.File, "file", "f", "internal",
"URL or filepath for definitions.yml, 'internal' uses the compiled-in file.")
flag.Parse()
Expand All @@ -138,29 +139,27 @@ func parseFlags() *flags {
// openFile opens a file or url for the parser, or returns the internal file.
func openFile(fileName string) (io.ReadCloser, error) {
if fileName == "internal" {
buf := bytes.Buffer{}
buf.Write(confBuilder)

return io.NopCloser(&buf), nil
buf := bytes.NewBuffer(confBuilder)
return io.NopCloser(buf), nil
}

if strings.HasPrefix(fileName, "http") {
http.DefaultClient.Timeout = opTimeout

resp, err := http.Get(fileName) //nolint:noctx,gosec // because we set a timeout.
if !strings.HasPrefix(fileName, "http") {
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("%s: %w", fileName, err)
}

return resp.Body, nil
return file, nil
}

file, err := os.Open(fileName)
http.DefaultClient.Timeout = opTimeout
//nolint:noctx,gosec // because we set a timeout.
resp, err := http.Get(fileName)
if err != nil {
return nil, fmt.Errorf("%s: %w", fileName, err)
}

return file, nil
return resp.Body, nil
}

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

return newSection
}

// This is used only by compose and config. docs has it's own.
func writeFile(dir, output string, buf *bytes.Buffer) {
_ = os.Mkdir(dir, dirMode)
filePath := filepath.Join(dir, output)
log.Printf("Writing: %s, size: %d", filePath, buf.Len())
buf.WriteString("## => Content Auto Generated, " +
strings.ToUpper(time.Now().UTC().Round(time.Second).Format("02 Jan 2006 15:04 UTC")+"\n"))

if err := os.WriteFile(filePath, buf.Bytes(), fileMode); err != nil {
log.Fatalln(err)
}
}
8 changes: 4 additions & 4 deletions pkg/bindata/bindata.go

Large diffs are not rendered by default.

0 comments on commit 9401987

Please sign in to comment.