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:
+38
-18
@@ -18,17 +18,17 @@ import (
|
||||
"github.com/netsynth/netsynth/classify"
|
||||
"github.com/netsynth/netsynth/config"
|
||||
"github.com/netsynth/netsynth/encode"
|
||||
"github.com/netsynth/netsynth/synth"
|
||||
)
|
||||
|
||||
var (
|
||||
ifaceName string
|
||||
listIfaces bool
|
||||
verbose bool
|
||||
outputPath string
|
||||
bpfFilter string // NEW: --filter flag (CAPT-05)
|
||||
readPath string // NEW: --read flag (CAPT-06)
|
||||
configPath string // NEW: --config flag (CFG-03)
|
||||
ifaceName string
|
||||
listIfaces bool
|
||||
verbose bool
|
||||
outputPath string
|
||||
bpfFilter string // NEW: --filter flag (CAPT-05)
|
||||
readPath string // NEW: --read flag (CAPT-06)
|
||||
configPath string // NEW: --config flag (CFG-03)
|
||||
printConfig bool // NEW: --print-config flag (CFG-06)
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -46,6 +46,7 @@ func main() {
|
||||
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(&configPath, "config", "", "Path to TOML config file (default: auto-discover)")
|
||||
rootCmd.Flags().BoolVar(&printConfig, "print-config", false, "Print effective config as commented TOML and exit")
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
@@ -68,6 +69,11 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
return runListInterfaces()
|
||||
}
|
||||
|
||||
// --print-config mode (CFG-06, D-09/D-10): must come before interface-required check
|
||||
if printConfig {
|
||||
return runPrintConfig()
|
||||
}
|
||||
|
||||
// D-03: --read and -i are mutually exclusive
|
||||
if readPath != "" && ifaceName != "" {
|
||||
return fmt.Errorf("--read and -i are mutually exclusive; use one or the other")
|
||||
@@ -84,11 +90,10 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// Load config (CFG-01 through CFG-05, D-11: fail fast before capture)
|
||||
loadResult, err := config.Load(configPath)
|
||||
result, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
freqCfgs := loadResult.FreqCfgs
|
||||
|
||||
// Resolve output path
|
||||
if outputPath == "" {
|
||||
@@ -100,13 +105,24 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if readPath != "" {
|
||||
return runPcapMode(cmd, freqCfgs)
|
||||
return runPcapMode(cmd, result)
|
||||
}
|
||||
return runLiveMode(cmd, freqCfgs)
|
||||
return runLiveMode(cmd, result)
|
||||
}
|
||||
|
||||
// runPrintConfig loads the config and prints the effective configuration as commented TOML.
|
||||
func runPrintConfig() error {
|
||||
result, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
output := config.PrintConfig(result)
|
||||
fmt.Print(output)
|
||||
return nil
|
||||
}
|
||||
|
||||
// runLiveMode runs the live packet capture pipeline.
|
||||
func runLiveMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.FreqConfig) error {
|
||||
func runLiveMode(cmd *cobra.Command, result config.LoadResult) error {
|
||||
// Set up signal handling (Ctrl+C)
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
@@ -119,7 +135,9 @@ func runLiveMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.Fr
|
||||
}
|
||||
|
||||
// Stage 2: Classify (CLAS-01)
|
||||
classifier := classify.NewClassifier(classify.DefaultRules)
|
||||
// D-04: user rules prepend before built-ins; first-match-wins (RULE-02)
|
||||
allRules := append(result.UserRules, classify.DefaultRules...)
|
||||
classifier := classify.NewClassifier(allRules)
|
||||
classified := make(chan classify.ClassifiedPacket, 1024)
|
||||
go func() {
|
||||
defer close(classified)
|
||||
@@ -155,7 +173,7 @@ func runLiveMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.Fr
|
||||
// D-07: Encoding status line
|
||||
fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath)
|
||||
encodeStart := time.Now()
|
||||
if err := encode.RunSynthesis(collectedSnapshots, outputPath, freqCfgs); err != nil {
|
||||
if err := encode.RunSynthesis(collectedSnapshots, outputPath, result.FreqCfgs); err != nil {
|
||||
return fmt.Errorf("synthesis failed: %w", err)
|
||||
}
|
||||
encodeElapsed := time.Since(encodeStart)
|
||||
@@ -172,7 +190,7 @@ func runLiveMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.Fr
|
||||
}
|
||||
|
||||
// runPcapMode runs the pcap file processing pipeline (CAPT-06).
|
||||
func runPcapMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.FreqConfig) error {
|
||||
func runPcapMode(cmd *cobra.Command, result config.LoadResult) error {
|
||||
// D-06: bookend start message
|
||||
fmt.Fprintf(os.Stderr, "Reading %s...\n", readPath)
|
||||
|
||||
@@ -183,7 +201,9 @@ func runPcapMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.Fr
|
||||
}
|
||||
|
||||
// Classify packets (reuse same classifier)
|
||||
classifier := classify.NewClassifier(classify.DefaultRules)
|
||||
// D-04: user rules prepend before built-ins; first-match-wins (RULE-02)
|
||||
allRules := append(result.UserRules, classify.DefaultRules...)
|
||||
classifier := classify.NewClassifier(allRules)
|
||||
classified := make(chan classify.ClassifiedPacket, 1024)
|
||||
go func() {
|
||||
defer close(classified)
|
||||
@@ -223,7 +243,7 @@ func runPcapMode(cmd *cobra.Command, freqCfgs map[classify.TrafficClass]synth.Fr
|
||||
// Encode
|
||||
fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath)
|
||||
encodeStart := time.Now()
|
||||
if err := encode.RunSynthesis(collectedSnapshots, outputPath, freqCfgs); err != nil {
|
||||
if err := encode.RunSynthesis(collectedSnapshots, outputPath, result.FreqCfgs); err != nil {
|
||||
return fmt.Errorf("synthesis failed: %w", err)
|
||||
}
|
||||
encodeElapsed := time.Since(encodeStart)
|
||||
|
||||
Reference in New Issue
Block a user