program works now, added a little documentation

This commit is contained in:
2024-03-19 18:12:02 +01:00
parent 50a5221f93
commit 864261dd48
9 changed files with 283 additions and 58 deletions

43
main.go
View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"log"
"os"
)
@@ -24,42 +23,26 @@ func Main(output io.Writer) int {
}
if conf.Listshortcuts {
for _, name := range Templates {
fmt.Println(name)
}
ListTemplates(output)
return 0
}
// FIXME: this is a slice of Template{}, turn it into a map
if Contains(Templates, conf.Code) {
if len(conf.Code) == 0 {
fmt.Fprintln(output, Usage)
}
if Exists(Templates, conf.Code) {
conf.Code = Templates[conf.Code]
}
names, err := Generate(conf.Count, conf.Code)
if err != nil {
return Die(err)
}
if err = PrintColumns(names, output); err != nil {
return Die(err)
}
return 0
}
func exists[K comparable, V any](m map[K]V, v K) bool {
if _, ok := m[v]; ok {
return true
}
return false
}
func Die(err error) int {
log.Fatal("Error", err.Error())
return 1
}
// find an item in a list, generic variant
func Contains[E comparable](s []E, v E) bool {
for _, vs := range s {
if v == vs {
return true
}
}
return false
}