Files
gfn/main.go

116 lines
2.5 KiB
Go
Raw Normal View History

/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-03-19 14:05:26 +01:00
package main
import (
"fmt"
"io"
2025-09-17 20:48:53 +02:00
"log"
2024-03-19 14:05:26 +01:00
"os"
"runtime/debug"
"log/slog"
"github.com/tlinden/yadu"
2024-03-19 14:05:26 +01:00
)
func main() {
os.Exit(Main(os.Stdout))
}
func TMain() int {
return Main(os.Stdout)
}
2024-03-19 14:05:26 +01:00
func Main(output io.Writer) int {
// parse config file and command line parameters, if any
2024-03-19 14:05:26 +01:00
conf, err := InitConfig(output)
if err != nil {
return Die(err)
}
if conf.Showversion {
2025-09-17 20:48:53 +02:00
_, err := fmt.Fprintf(output, "This is gfn version %s\n", VERSION)
if err != nil {
2025-09-17 20:52:58 +02:00
log.Fatalf("failed to print to output: %s", err)
2025-09-17 20:48:53 +02:00
}
2024-03-19 14:05:26 +01:00
return 0
}
// enable debugging, if needed. We only use log/slog for
// debugging, so there's no need to configure it outside debugging
if conf.Debug {
logLevel := &slog.LevelVar{}
// we're using a more verbose logger in debug mode
buildInfo, _ := debug.ReadBuildInfo()
opts := &yadu.Options{
Level: logLevel,
AddSource: true,
}
logLevel.Set(slog.LevelDebug)
handler := yadu.NewHandler(output, opts)
debuglogger := slog.New(handler).With(
slog.Group("program_info",
slog.Int("pid", os.Getpid()),
slog.String("go_version", buildInfo.GoVersion),
),
)
slog.SetDefault(debuglogger)
}
// just show what we have
2024-03-19 14:05:26 +01:00
if conf.Listshortcuts {
ListTemplates(conf, output)
2024-03-19 14:05:26 +01:00
return 0
}
// code argument is mandatory
if len(conf.Code) == 0 {
2025-09-17 20:48:53 +02:00
_, err := fmt.Fprintln(output, Usage)
if err != nil {
2025-09-17 20:52:58 +02:00
log.Fatalf("failed to print to output: %s", err)
2025-09-17 20:48:53 +02:00
}
return 1
}
// check if we can use a template, otherwise consider the argument
// to be FN code
if Exists(conf.Templates, conf.Code) {
slog.Debug("Argument resolves to template code",
"name", conf.Code, "code", conf.Templates[conf.Code])
conf.Code = conf.Templates[conf.Code]
2024-03-19 14:05:26 +01:00
}
// all prepared, run baby run
names, err := Generate(conf)
if err != nil {
return Die(err)
2024-03-19 14:05:26 +01:00
}
if err = PrintColumns(conf, names, output); err != nil {
return Die(err)
2024-03-19 14:05:26 +01:00
}
return 0
2024-03-19 14:05:26 +01:00
}