feat(06-02): wire --config flag and config.Load into CLI pipeline

- Add configPath string var and --config flag to Cobra command
- Import config and synth packages in main.go
- Call config.Load(configPath) in run() before capture (D-11 fail fast)
- Change runLiveMode and runPcapMode signatures to accept freqCfgs map
- Pass freqCfgs to encode.RunSynthesis in both live and pcap modes
This commit is contained in:
2026-03-26 21:04:11 +01:00
parent 3dfcbbeaf5
commit 413cceb1eb
+16 -6
View File
@@ -16,7 +16,9 @@ import (
"github.com/netsynth/netsynth/aggregate" "github.com/netsynth/netsynth/aggregate"
"github.com/netsynth/netsynth/capture" "github.com/netsynth/netsynth/capture"
"github.com/netsynth/netsynth/classify" "github.com/netsynth/netsynth/classify"
"github.com/netsynth/netsynth/config"
"github.com/netsynth/netsynth/encode" "github.com/netsynth/netsynth/encode"
"github.com/netsynth/netsynth/synth"
) )
var ( var (
@@ -26,6 +28,7 @@ var (
outputPath string outputPath string
bpfFilter string // NEW: --filter flag (CAPT-05) bpfFilter string // NEW: --filter flag (CAPT-05)
readPath string // NEW: --read flag (CAPT-06) readPath string // NEW: --read flag (CAPT-06)
configPath string // NEW: --config flag (CFG-03)
) )
func main() { func main() {
@@ -42,6 +45,7 @@ func main() {
rootCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Output MP3 file path (default: netsynth-<timestamp>.mp3)") rootCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Output MP3 file path (default: netsynth-<timestamp>.mp3)")
rootCmd.Flags().StringVar(&bpfFilter, "filter", "", "BPF filter expression (tcpdump syntax, e.g. \"port 53\")") rootCmd.Flags().StringVar(&bpfFilter, "filter", "", "BPF filter expression (tcpdump syntax, e.g. \"port 53\")")
rootCmd.Flags().StringVar(&readPath, "read", "", "Read packets from pcap file instead of live capture") rootCmd.Flags().StringVar(&readPath, "read", "", "Read packets from pcap file instead of live capture")
rootCmd.Flags().StringVar(&configPath, "config", "", "Path to TOML config file (default: auto-discover)")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)
@@ -79,6 +83,12 @@ func run(cmd *cobra.Command, args []string) error {
} }
} }
// Load config (CFG-01 through CFG-05, D-11: fail fast before capture)
freqCfgs, err := config.Load(configPath)
if err != nil {
return err
}
// Resolve output path // Resolve output path
if outputPath == "" { if outputPath == "" {
if readPath != "" { if readPath != "" {
@@ -89,13 +99,13 @@ func run(cmd *cobra.Command, args []string) error {
} }
if readPath != "" { if readPath != "" {
return runPcapMode(cmd) return runPcapMode(cmd, freqCfgs)
} }
return runLiveMode(cmd) return runLiveMode(cmd, freqCfgs)
} }
// runLiveMode runs the live packet capture pipeline. // runLiveMode runs the live packet capture pipeline.
func runLiveMode(cmd *cobra.Command) error { func runLiveMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.FreqConfig) error {
// Set up signal handling (Ctrl+C) // Set up signal handling (Ctrl+C)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop() defer stop()
@@ -144,7 +154,7 @@ func runLiveMode(cmd *cobra.Command) error {
// D-07: Encoding status line // D-07: Encoding status line
fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath) fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath)
encodeStart := time.Now() encodeStart := time.Now()
if err := encode.RunSynthesis(collectedSnapshots, outputPath); err != nil { if err := encode.RunSynthesis(collectedSnapshots, outputPath, freqCfgs); err != nil {
return fmt.Errorf("synthesis failed: %w", err) return fmt.Errorf("synthesis failed: %w", err)
} }
encodeElapsed := time.Since(encodeStart) encodeElapsed := time.Since(encodeStart)
@@ -161,7 +171,7 @@ func runLiveMode(cmd *cobra.Command) error {
} }
// runPcapMode runs the pcap file processing pipeline (CAPT-06). // runPcapMode runs the pcap file processing pipeline (CAPT-06).
func runPcapMode(cmd *cobra.Command) error { func runPcapMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.FreqConfig) error {
// D-06: bookend start message // D-06: bookend start message
fmt.Fprintf(os.Stderr, "Reading %s...\n", readPath) fmt.Fprintf(os.Stderr, "Reading %s...\n", readPath)
@@ -212,7 +222,7 @@ func runPcapMode(cmd *cobra.Command) error {
// Encode // Encode
fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath) fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath)
encodeStart := time.Now() encodeStart := time.Now()
if err := encode.RunSynthesis(collectedSnapshots, outputPath); err != nil { if err := encode.RunSynthesis(collectedSnapshots, outputPath, freqCfgs); err != nil {
return fmt.Errorf("synthesis failed: %w", err) return fmt.Errorf("synthesis failed: %w", err)
} }
encodeElapsed := time.Since(encodeStart) encodeElapsed := time.Since(encodeStart)