mirror of
https://codeberg.org/scip/gfn.git
synced 2025-12-16 18:30:57 +01:00
program works now, added a little documentation
This commit is contained in:
43
main.go
43
main.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user