Skip to content

Commit

Permalink
one folder for all files
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 17, 2024
1 parent 315b7c4 commit a0a89e0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
13 changes: 9 additions & 4 deletions init/config/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)

/* This file creates an example compose file: docker-compose.yml */
Expand Down Expand Up @@ -39,15 +41,15 @@ services:
- 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 @@ -58,9 +60,12 @@ func createCompose(config *Config, output string) {
}
}

log.Println("Writing", output, "size:", buf.Len())
_ = os.Mkdir(dir, dirMode)
filePath := filepath.Join(dir, output)
log.Printf("Writing: %s, size: %d", filePath, buf.Len())
buf.WriteString("\n# Generated: " + time.Now().Round(time.Second).String() + "\n")

if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
if err := os.WriteFile(filePath, buf.Bytes(), fileMode); err != nil {
log.Fatalln(err)
}
}
Expand Down
11 changes: 7 additions & 4 deletions init/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
)

/* This file creates an example config file: unpackerr.conf.example */

func printConfFile(config *Config, output string) {
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 @@ -29,9 +30,11 @@ func printConfFile(config *Config, output string) {
}
}

log.Println("Writing", output, "size:", buf.Len())
_ = os.Mkdir(dir, dirMode)
filePath := filepath.Join(dir, output)
log.Printf("Writing: %s, size: %d", filePath, buf.Len())

if err := os.WriteFile(output, buf.Bytes(), fileMode); err != nil {
if err := os.WriteFile(filePath, buf.Bytes(), fileMode); err != nil {
log.Fatalln(err)
}
}
Expand Down
58 changes: 26 additions & 32 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 @@ -57,62 +58,55 @@ func writeDocusaurus(dir, file, content string) error {
// makeGenerated writes a special file that the website can import.
// Adds all param sections except global into a docusaurus import format.
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())
}

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
34 changes: 16 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 Down Expand Up @@ -97,13 +97,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 +114,7 @@ type flags struct {
Type []string
Config string
Compose string
Docs string
Output string
File string
}

Expand All @@ -126,8 +126,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 +138,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 Down

0 comments on commit a0a89e0

Please sign in to comment.