From 53b292c18d96351177b21e1aa405dee7d577ab6a Mon Sep 17 00:00:00 2001 From: solnicki Date: Tue, 26 Nov 2024 10:44:40 +0100 Subject: [PATCH] fix flag parsing --- cmd/main.go | 4 +++- internal/config/config.go | 8 ++++---- internal/config/validate.go | 29 +++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index e4b4e8e..c87bca8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,8 @@ package main -import "github.com/logmanager-oss/logveil/cmd/logveil" +import ( + "github.com/logmanager-oss/logveil/cmd/logveil" +) func main() { logveil.Start() diff --git a/internal/config/config.go b/internal/config/config.go index 59a8b30..e291ff1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,13 +20,13 @@ type Config struct { // LoadAndValidate loads values from user supplied input into Config struct and validates them func (c *Config) LoadAndValidate() { - flag.Func("d", "Path to directory with anonymizing data", validateDir(c.AnonymizationDataPath)) + flag.Func("d", "Path to directory with anonymizing data", c.validateDirPath()) - flag.Func("i", "Path to input file containing logs to be anonymized", validateInput(c.InputPath)) + flag.Func("i", "Path to input file containing logs to be anonymized", c.validateInputPath()) - flag.Func("c", "Path to input file containing custom anonymization mappings", validateInput(c.CustomReplacementMapPath)) + flag.Func("c", "Path to input file containing custom anonymization mappings", c.validateCustomMappingPath()) - flag.Func("o", "Path to output file (default: Stdout)", validateOutput(c.OutputPath)) + flag.Func("o", "Path to output file (default: Stdout)", c.validateOutput()) flag.BoolVar(&c.IsVerbose, "v", false, "Enable verbose logging (default: Disabled)") flag.BoolVar(&c.IsLmExport, "e", false, "Change input file type to LM export (default: LM Backup)") diff --git a/internal/config/validate.go b/internal/config/validate.go index 5b6ff87..3bb6eab 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -6,7 +6,7 @@ import ( "os" ) -func validateInput(inputPath string) func(string) error { +func (c *Config) validateInputPath() func(string) error { return func(flagValue string) error { fileInfo, err := os.Stat(flagValue) if err != nil { @@ -17,13 +17,30 @@ func validateInput(inputPath string) func(string) error { return fmt.Errorf("Input file %s cannot be a directory.\n", flagValue) } - inputPath = flagValue + c.InputPath = flagValue return nil } } -func validateOutput(outputPath string) func(string) error { +func (c *Config) validateCustomMappingPath() func(string) error { + return func(flagValue string) error { + fileInfo, err := os.Stat(flagValue) + if err != nil { + return err + } + + if fileInfo.IsDir() { + return fmt.Errorf("Input file %s cannot be a directory.\n", flagValue) + } + + c.CustomReplacementMapPath = flagValue + + return nil + } +} + +func (c *Config) validateOutput() func(string) error { return func(flagValue string) error { fileInfo, err := os.Stat(flagValue) if err != nil { @@ -37,13 +54,13 @@ func validateOutput(outputPath string) func(string) error { return fmt.Errorf("Output file %s cannot be a directory.\n", flagValue) } - outputPath = flagValue + c.OutputPath = flagValue return nil } } -func validateDir(dir string) func(string) error { +func (c *Config) validateDirPath() func(string) error { return func(flagValue string) error { fileInfo, err := os.Stat(flagValue) if err != nil { @@ -54,7 +71,7 @@ func validateDir(dir string) func(string) error { return fmt.Errorf("Path to anonymization data %s needs to be a directory.\n", flagValue) } - dir = flagValue + c.AnonymizationDataPath = flagValue return nil }