Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 15, 2024
1 parent 65459ab commit e35f5d1
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 53 deletions.
13 changes: 10 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ linters:
- tagliatelle
- cyclop
- testpackage
run:
timeout: 3m

issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# Exclude some linters from testing files.
- linters:
- forbidigo
- lll
path: 'init/.*'
output:
sort-results: true
run:
timeout: 3m

linters-settings:
ireturn:
Expand All @@ -41,4 +47,5 @@ linters-settings:
- github.com/radovskyb/watcher
- github.com/prometheus/client_golang/
- github.com/spf13/pflag
- github.com/julienschmidt/httprouter
- github.com/julienschmidt/httprouter
- github.com/BurntSushi/toml
115 changes: 115 additions & 0 deletions init/config/compose-builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"fmt"
"strings"
)

const (
space = " "
composeHeader = `### Unpackerr docker-compose.yml Example
### Please read this page for help using this example:
### https://unpackerr.zip/docs/install/compose
### Generator: https://notifiarr.com/unpackerr
##################################################################
services:
unpackerr:
image: golift/unpackerr
container_name: unpackerr
volumes:
# You need at least this one volume mapped so Unpackerr can find your files to extract.
# Make sure this matches your Starr apps; the folder mount (/downloads or /data) should be identical.
- /mnt/HostDownloads:/downloads
restart: always
# 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.
environment:
- TZ=${TZ}`
)

func printCompose(config *Config) {
fmt.Println(composeHeader)

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

if config.Defs[section] == nil {
config.Sections[section].printCompose(strings.Title(section), config.Prefix) //nolint:staticcheck
} else {
config.Sections[section].printComposeDefined(config.Prefix, config.Defs[section])
}
}
}

func (h *Header) printCompose(title, prefix string) {
if len(h.Params) > 0 {
fmt.Println(space, "##", title)
}

for _, param := range h.Params {
if h.Kind == list {
fmt.Print(param.Compose(prefix + h.Prefix + "0_"))
} else {
fmt.Print(param.Compose(prefix + h.Prefix))
}
}
}

func (h *Header) printComposeDefined(prefix string, defs Defs) {
for section, def := range defs {
// Loop each defined section Defaults, and see if one of the param names match.
for overrideName, override := range def.Defaults {
for _, defined := range h.Params {
// If the name of the default (override) matches this param name, overwrite the value.
if defined.Name == overrideName {
defined.Default = override
}
}
}

// Make a brand new section and print it.
(&Header{
Text: def.Text,
Prefix: def.Prefix,
Params: h.Params,
Kind: h.Kind,
}).printCompose(strings.Title(section), prefix) //nolint:staticcheck
}
}

func (p *Param) Compose(prefix string) string {
val := p.Default
if p.Example != nil {
val = p.Example
}

switch p.Kind {
default:
return fmt.Sprint(space, " - ", prefix, p.EnvVar, "=", val, "\n")
case list:
var out string

for idx, sv := range val.([]any) { //nolint:forcetypeassert
out += fmt.Sprint(space, " - ", prefix, p.EnvVar, idx, "=", sv, "\n")
}

return out
case "conlist":
out := []string{}

for _, sv := range val.([]any) { //nolint:forcetypeassert
out = append(out, fmt.Sprint(sv))
}

return fmt.Sprint(space, " - ", prefix, p.EnvVar, "=", strings.Join(out, ","), "\n")
}
}
52 changes: 3 additions & 49 deletions init/config/conf-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,12 @@ package main
import (
"bytes"
"fmt"
"os"
"strings"

"github.com/BurntSushi/toml"
"gopkg.in/yaml.v3"
)

type Param struct {
Name string `yaml:"name"`
EnvVar string `yaml:"envvar"`
Default any `yaml:"default"`
Example any `yaml:"example"`
Short string `yaml:"short"`
Desc string `yaml:"desc"`
Kind string `yaml:"kind"` // "", list, conlist
}

type Header struct {
Text string `yaml:"text"`
Prefix string `yaml:"envvar_prefix"`
Params []*Param `yaml:"params"`
Kind string `yaml:"kind"` // "", list
NoHeader bool `yaml:"no_header"` // Do not print [section] header.
}

type Def struct {
Comment bool `yaml:"comment"` // just the header.
Text string `yaml:"text"`
Defaults map[string]any `yaml:"defaults"`
}

type Defs map[string]*Def

type ConfigFile struct {
Defs map[string]Defs `yaml:"defs"`
Prefix string `yaml:"envvar_prefix"`
Order []string `yaml:"order"`
Sections map[string]*Header `yaml:"sections"`
}

func main() {
file, err := os.Open("./conf-builder.yml")
if err != nil {
panic(err)
}

config := &ConfigFile{}
// Decode conf-builder file into Go data structure.
if err = yaml.NewDecoder(file).Decode(config); err != nil {
panic(err)
}

func printConfFile(config *Config) {
// Loop the 'Order' list.
for _, section := range config.Order {
// If Order contains a missing section, panic.
Expand Down Expand Up @@ -84,7 +38,7 @@ func printSection(name string, section *Header, noComment bool) {
}

if !section.NoHeader { // Print the [section] or [[section]] header.
if section.Kind == "list" { // list sections are commented by default.
if section.Kind == list { // list sections are commented by default.
fmt.Println(comment + "[[" + name + "]]") // list sections use double-brackets.
} else {
fmt.Println("[" + name + "]") // non-list sections use single brackets.
Expand All @@ -108,7 +62,7 @@ func printSection(name string, section *Header, noComment bool) {
comment = "#"
}

if section.Kind == "list" {
if section.Kind == list {
// If the 'kind' is a 'list', we comment all the parameters.
fmt.Printf("# %s = %s\n", param.Name, param.Value())
} else {
Expand Down
6 changes: 5 additions & 1 deletion init/config/conf-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ order:
defs:
starr:
sonarr:
prefix: SONARR_
text: |
## Leaving the [[sonarr]] header uncommented (no leading hash #) without also
## uncommenting the api_key (remove the hash #) will produce a startup warning.
defaults:
url: http://127.0.0.1:8989
radarr:
prefix: RADARR_
text: |
## Leaving the [[radarr]] header uncommented (no leading hash #) without also
## uncommenting the api_key (remove the hash #) will produce a startup warning.
defaults:
url: http://127.0.0.1:7878
lidarr:
prefix: LIDARR_
comment: true
defaults:
url: http://127.0.0.1:8686
readarr:
prefix: READARR_
comment: true
defaults:
url: http://127.0.0.1:8787
whisparr:
prefix: WHISPARR_
comment: true
defaults:
url: http://127.0.0.1:6969
Expand Down Expand Up @@ -231,7 +236,6 @@ sections:
###############################################################################
starr:
envvar_prefix: '$APP_' # $APP must be replaced by parser.
kind: list
params:
- name: url
Expand Down
7 changes: 7 additions & 0 deletions init/config/docs-builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func printDocusaurus(_ *Config) {
fmt.Println("not yet")
}
71 changes: 71 additions & 0 deletions init/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"os"

"gopkg.in/yaml.v3"
)

const (
list = "list"
)

type Config struct {
Defs map[string]Defs `yaml:"defs"`
Prefix string `yaml:"envvar_prefix"`
Order []string `yaml:"order"`
Sections map[string]*Header `yaml:"sections"`
}

type Header struct {
Text string `yaml:"text"`
Prefix string `yaml:"envvar_prefix"`
Params []*Param `yaml:"params"`
Kind string `yaml:"kind"` // "", list
NoHeader bool `yaml:"no_header"` // Do not print [section] header.
}

type Param struct {
Name string `yaml:"name"`
EnvVar string `yaml:"envvar"`
Default any `yaml:"default"`
Example any `yaml:"example"`
Short string `yaml:"short"`
Desc string `yaml:"desc"`
Kind string `yaml:"kind"` // "", list, conlist
}

type Def struct {
Comment bool `yaml:"comment"` // just the header.
Prefix string `yaml:"prefix"`
Text string `yaml:"text"`
Defaults map[string]any `yaml:"defaults"`
}

type Defs map[string]*Def

func main() {
file, err := os.Open("./conf-builder.yml")
if err != nil {
panic(err)
}

config := &Config{}
// Decode conf-builder file into Go data structure.
if err = yaml.NewDecoder(file).Decode(config); err != nil {
panic(err)
}

switch {
default:
fallthrough
case len(os.Args) <= 1:
fallthrough
case os.Args[1] == "conf":
printConfFile(config)
case os.Args[1] == "compose", os.Args[1] == "docker":
printCompose(config)
case os.Args[1] == "docs":
printDocusaurus(config)
}
}

0 comments on commit e35f5d1

Please sign in to comment.