Skip to content

Commit

Permalink
fix flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
solnicki committed Nov 26, 2024
1 parent 2045124 commit 53b292c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
29 changes: 23 additions & 6 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 53b292c

Please sign in to comment.