Initial import
Some checks failed
Run tests / test (1.25) (push) Has been cancelled
Run tests / test (stable) (push) Has been cancelled

This commit is contained in:
2026-02-22 20:27:07 +01:00
commit fb898bb058
77 changed files with 2719 additions and 0 deletions

27
internal/cmd/all.go Normal file
View File

@@ -0,0 +1,27 @@
package cmd
import (
"errors"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
)
func AllFlags(configFile string) []cli.Flag {
return append(ConfigFlags(configFile), LoggerFlags()...)
}
func WaitForInterrupt(logger *logrus.Logger, what string) error {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
logger.Infof("%s: running, interrupt with ^C", what)
for sig := range sigs {
return errors.New("terminating on signal " + sig.String())
}
return nil
}

80
internal/cmd/config.go Normal file
View File

@@ -0,0 +1,80 @@
package cmd
import (
"bytes"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"go.yaml.in/yaml/v3"
)
const (
FlagConfig = "config"
)
func ConfigFlags(defaultValue string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: FlagConfig,
Aliases: []string{"c"},
Usage: "Configuration file path",
Value: defaultValue,
},
}
}
type ConfigWithIncludes interface {
Includes() []string
}
func Load(logger *logrus.Logger, name string, config ConfigWithIncludes, parsed ...string) (err error) {
if !filepath.IsAbs(name) {
var abs string
if abs, err = filepath.Abs(name); err != nil {
return
}
logger.Tracef("config: canonicallize %s => %s", name, abs)
name = abs
}
logger.Tracef("config: parsed %s", parsed)
logger.Debugf("config: parse %s", name)
var data []byte
if data, err = os.ReadFile(name); err != nil {
return
}
decoder := yaml.NewDecoder(bytes.NewBuffer(data))
decoder.KnownFields(true)
if err = decoder.Decode(config); err != nil {
return err
}
for _, include := range config.Includes() {
if contains(parsed, include) {
continue
}
if !filepath.IsAbs(include) {
abs := filepath.Clean(filepath.Join(filepath.Dir(name), include))
logger.Tracef("config: canonicallize %s => %s", include, abs)
include = abs
}
parsed = append(parsed, include)
if err = Load(logger, include, config, parsed...); err != nil {
return
}
}
return nil
}
func contains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}

69
internal/cmd/logger.go Normal file
View File

@@ -0,0 +1,69 @@
package cmd
import (
"context"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"git.maze.io/go/ham/protocol/meshcore"
"git.maze.io/go/hamview"
)
const (
FlagQuiet = "quiet"
FlagDebug = "debug"
FlagTrace = "trace"
)
func LoggerFlags() []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: FlagQuiet,
Aliases: []string{"q"},
Usage: "Disable informational logging",
},
&cli.BoolFlag{
Name: FlagDebug,
Aliases: []string{"D"},
Usage: "Enable debug level logging",
},
&cli.BoolFlag{
Name: FlagTrace,
Aliases: []string{"T"},
Usage: "Enable trace level logging",
},
}
}
func ConfigureLogging(logger **logrus.Logger) cli.BeforeFunc {
return func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
*logger = NewLogger(cmd)
hamview.Logger = *logger
return ctx, nil
}
}
func NewLogger(cmd *cli.Command) *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339,
})
if cmd != nil {
if cmd.Bool(FlagTrace) {
logger.SetLevel(logrus.TraceLevel)
} else if cmd.Bool(FlagDebug) {
logger.SetLevel(logrus.DebugLevel)
} else if cmd.Bool(FlagQuiet) {
logger.SetLevel(logrus.ErrorLevel)
}
}
// Update package loggers:
meshcore.Logger = logger
return logger
}