Move internal/cmd to cmd
Some checks failed
Test and build / Build receiver (amd64, , linux) (push) Failing after 40s
Test and build / Build receiver (arm, 6, linux) (push) Failing after 41s
Test and build / Build receiver (arm, 7, linux) (push) Failing after 40s
Test and build / Build collector (push) Failing after 54s
Test and build / test (push) Successful in 59s

This commit is contained in:
2026-02-23 15:51:35 +01:00
parent 03a2fc0e6f
commit be7e6b093a
10 changed files with 3 additions and 216 deletions

27
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
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
}

View File

@@ -11,7 +11,7 @@ import (
"git.maze.io/go/ham/protocol"
"git.maze.io/ham/hamview"
"git.maze.io/ham/hamview/internal/cmd"
"git.maze.io/ham/hamview/cmd"
)
var logger *logrus.Logger

View File

@@ -1,8 +0,0 @@
FROM alpine:3
ARG TARGETARCH
ARG TARGETVARIANT
COPY ./etc /etc/hamview
COPY ./build/hamview-receiver-${$TARGETARCH}${TARGETVARIANT#v} /opt/hamview/bin/hamview-receiver
WORKDIR /opt/hamview
ENTRYPOINT ["bin/hamview-receiver"]
CMD [ "protocol" ]

View File

@@ -1,49 +0,0 @@
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"git.maze.io/ham/hamview/internal/cmd"
)
var logger *logrus.Logger
func main() {
cmd := &cli.Command{
Name: "hamview-receiver",
Usage: "Receiver for HAM radio protocols",
Action: func(context.Context, *cli.Command) error {
fmt.Println("boom! I say!")
return nil
},
Flags: cmd.AllFlags("hamview-receiver.yaml"),
Commands: []*cli.Command{
{
Name: "aprsis",
Usage: "Start an APRS-IS proxy",
Before: cmd.ConfigureLogging(&logger),
Action: runAPRSIS,
},
{
Name: "meshcore",
Usage: "Start a MeshCore receiver",
Before: cmd.ConfigureLogging(&logger),
Action: runMeshCore,
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
func waitForInterrupt() error {
return cmd.WaitForInterrupt(logger, "receiver")
}

View File

@@ -1,77 +0,0 @@
package main
import (
"context"
"github.com/urfave/cli/v3"
"git.maze.io/go/ham/protocol/aprs/aprsis"
"git.maze.io/ham/hamview"
"git.maze.io/ham/hamview/internal/cmd"
)
type aprsisConfig struct {
Broker hamview.BrokerConfig `yaml:"broker"`
Receiver hamview.APRSISConfig `yaml:"receiver"`
Include []string `yaml:"include"`
}
func (config *aprsisConfig) Includes() []string {
includes := config.Include
config.Include = nil
return includes
}
func runAPRSIS(ctx context.Context, command *cli.Command) error {
var config = aprsisConfig{
Receiver: hamview.APRSISConfig{
Listen: hamview.DefaultAPRSISListen,
Server: hamview.DefaultAPRSISServer,
},
}
if err := cmd.Load(logger, command.String(cmd.FlagConfig), &config); err != nil {
return err
}
logger.Infof("receiver: starting APRS-IS proxy on tcp://%s to tcp://%s",
config.Receiver.Listen,
config.Receiver.Server)
proxy, err := aprsis.NewProxy(config.Receiver.Listen, config.Receiver.Server)
if err != nil {
return err
}
proxy.OnClient = func(callsign string, client *aprsis.ProxyClient) {
go receiveAPRSIS(&config.Broker, callsign, client)
}
return waitForInterrupt()
}
func receiveAPRSIS(config *hamview.BrokerConfig, callsign string, client *aprsis.ProxyClient) {
defer client.Close()
broker, err := hamview.NewBroker(config)
if err != nil {
logger.Errorf("receiver: can't setup to broker: %v", err)
return
}
defer broker.Close()
info := client.Info() // TODO: enrich info from config?
if err = broker.StartRadio("aprs", info); err != nil {
logger.Fatalf("receiver: can't start broker: %v", err)
return
}
logger.Infof("receiver: start receiving packets from station: %s", callsign)
for packet := range client.RawPackets() {
logger.Debugf("aprs packet: %#+v", packet)
if err := broker.PublishPacket("aprs/packet", packet); err != nil {
logger.Error(err)
}
}
logger.Info("receiver: stopped receiving packets from station: %s", callsign)
}

View File

@@ -1,79 +0,0 @@
package main
import (
"context"
"github.com/urfave/cli/v3"
"git.maze.io/go/ham/protocol"
"git.maze.io/go/ham/protocol/meshcore"
"git.maze.io/ham/hamview"
"git.maze.io/ham/hamview/internal/cmd"
)
type meshCoreConfig struct {
Broker hamview.BrokerConfig `yaml:"broker"`
Receiver hamview.MeshCoreConfig `yaml:"receiver"`
Include []string `yaml:"include"`
}
func (config *meshCoreConfig) Includes() []string {
includes := config.Include
config.Include = nil
return includes
}
func runMeshCore(ctx context.Context, command *cli.Command) error {
var config meshCoreConfig
if err := cmd.Load(logger, command.String(cmd.FlagConfig), &config); err != nil {
return err
}
broker, err := hamview.NewBroker(&config.Broker)
if err != nil {
return err
}
defer broker.Close()
receiver, err := hamview.NewMeshCoreReceiver(&config.Receiver)
if err != nil {
return err
}
defer receiver.Close()
info := receiver.Info() // TODO: enrich info from config?
if err = broker.StartRadio(protocol.MeshCore, info); err != nil {
logger.Fatalf("receiver: can't start broker: %v", err)
return err
}
// Trace scheduler
//go receiver.RunTraces()
// Packet decoder
go func() {
logger.Info("receiver: start receiving packets")
for packet := range receiver.RawPackets() {
if len(packet.Raw) >= 1 {
var (
header = packet.Raw[0]
version = (header >> 6) & 0x03
routeType = meshcore.RouteType(header & 0x03)
payloadType = meshcore.PayloadType((header >> 2) & 0x0F)
)
logger.Debugf("meshcore packet: %d %s %s: %d bytes",
version,
routeType,
payloadType,
len(packet.Raw))
}
if err = broker.PublishPacket("meshcore/packet", packet); err != nil {
logger.Errorf("receiver: failed to publish packet: %v", err)
}
}
logger.Warn("receiver: closing")
}()
return waitForInterrupt()
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/urfave/cli/v3"
"git.maze.io/ham/hamview"
"git.maze.io/ham/hamview/internal/cmd"
"git.maze.io/ham/hamview/cmd"
)
var logger = logrus.New()

View File

@@ -14,7 +14,7 @@ import (
"git.maze.io/go/ham/protocol/meshcore"
"git.maze.io/ham/hamview"
"git.maze.io/ham/hamview/internal/cmd"
"git.maze.io/ham/hamview/cmd"
_ "github.com/cridenour/go-postgis" // PostGIS support
_ "github.com/lib/pq" // PostgreSQL support

69
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/ham/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
}