feat(07-02): wire LoadResult into main.go and add --print-config flag

- Add printConfig bool var and --print-config flag registration
- runPrintConfig() early-exit before interface-required check (CFG-06)
- runLiveMode and runPcapMode accept config.LoadResult; user rules prepend via append(result.UserRules, classify.DefaultRules...)
- Remove unused synth import from main.go
- Add TestPrintConfigFlagRegistered, TestPrintConfigNoInterface, TestPrintConfigWithConfigFile
- newTestCmd() wires --print-config and --config flags through PersistentPreRunE
This commit is contained in:
2026-03-26 21:51:19 +01:00
parent 9e71a805b8
commit d43914f869
2 changed files with 115 additions and 18 deletions
+77
View File
@@ -2,6 +2,8 @@ package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
@@ -16,6 +18,8 @@ func newTestCmd() *cobra.Command {
var testFilter string
var testRead string
var testOutput string
var testPrintConfig bool
var testConfigPath string
rootCmd := &cobra.Command{
Use: "netsynth",
@@ -29,6 +33,8 @@ func newTestCmd() *cobra.Command {
rootCmd.Flags().StringVarP(&testOutput, "output", "o", "", "Output MP3 file path")
rootCmd.Flags().StringVar(&testFilter, "filter", "", "BPF filter expression")
rootCmd.Flags().StringVar(&testRead, "read", "", "Read packets from pcap file instead of live capture")
rootCmd.Flags().BoolVar(&testPrintConfig, "print-config", false, "Print effective config")
rootCmd.Flags().StringVar(&testConfigPath, "config", "", "Path to TOML config file")
// Wire test variables to package-level vars used by run()
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
@@ -38,6 +44,8 @@ func newTestCmd() *cobra.Command {
outputPath = testOutput
bpfFilter = testFilter
readPath = testRead
printConfig = testPrintConfig
configPath = testConfigPath
return nil
}
@@ -320,3 +328,72 @@ func TestHelpOutputNewFlags(t *testing.T) {
t.Errorf("expected help/usage output to contain '--read', usage: %s", usageStr)
}
}
// TestPrintConfigFlagRegistered verifies --print-config flag is registered.
func TestPrintConfigFlagRegistered(t *testing.T) {
rootCmd := newTestCmd()
f := rootCmd.Flags().Lookup("print-config")
if f == nil {
t.Fatal("expected --print-config flag to be registered")
}
}
// TestPrintConfigNoInterface verifies --print-config works without -i flag.
func TestPrintConfigNoInterface(t *testing.T) {
// Reset globals
ifaceName = ""
listIfaces = false
verbose = false
bpfFilter = ""
readPath = ""
outputPath = ""
printConfig = false
configPath = ""
// Use a temp dir with no netsynth.toml so no config is auto-discovered
t.Chdir(t.TempDir())
rootCmd := newTestCmd()
rootCmd.SetArgs([]string{"--print-config"})
var outBuf, errBuf bytes.Buffer
rootCmd.SetOut(&outBuf)
rootCmd.SetErr(&errBuf)
err := rootCmd.Execute()
if err != nil {
t.Fatalf("--print-config should not require -i, got error: %v", err)
}
}
// TestPrintConfigWithConfigFile verifies --print-config loads and displays a user config.
func TestPrintConfigWithConfigFile(t *testing.T) {
// Create temp TOML with an override
dir := t.TempDir()
tomlPath := filepath.Join(dir, "test.toml")
if err := os.WriteFile(tomlPath, []byte("[sounds.ICMP]\nfrequency = 100.0\n"), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
// Reset globals
ifaceName = ""
listIfaces = false
verbose = false
bpfFilter = ""
readPath = ""
outputPath = ""
printConfig = false
configPath = ""
rootCmd := newTestCmd()
rootCmd.SetArgs([]string{"--print-config", "--config", tomlPath})
var outBuf, errBuf bytes.Buffer
rootCmd.SetOut(&outBuf)
rootCmd.SetErr(&errBuf)
err := rootCmd.Execute()
if err != nil {
t.Fatalf("--print-config with --config should succeed, got: %v", err)
}
}