Skip to content

Commit

Permalink
remove redundent code from main
Browse files Browse the repository at this point in the history
  • Loading branch information
haim-kermany committed Jan 7, 2024
1 parent b525dd2 commit 1b5c8c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
67 changes: 28 additions & 39 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func analysisVPCConfigs(c1, c2 map[string]*vpcmodel.VPCConfig, inArgs *InArgs, o
return analysisOut, nil
}

func vpcConfigsFromFile(fileName string, inArgs *InArgs) (map[string]*vpcmodel.VPCConfig, error) {
rc, err1 := ibmvpc.ParseResourcesFromFile(fileName)
if err1 != nil {
return nil, fmt.Errorf("error parsing input vpc resources file: %w", err1)
}

vpcConfigs, err2 := ibmvpc.VPCConfigsFromResources(rc, *inArgs.VPC, *inArgs.Debug)
if err2 != nil {
return nil, fmt.Errorf(ErrorFormat, InGenerationErr, err2)
}
return vpcConfigs, nil
}

// The actual main function
// Takes command-line flags and returns an error rather than exiting, so it can be more easily used in testing
func _main(cmdlineArgs []string) error {
Expand All @@ -85,55 +98,31 @@ func _main(cmdlineArgs []string) error {
fmt.Printf("vpc-network-config-analyzer v%s\n", version.VersionCore)
return nil
}

rc, err1 := ibmvpc.ParseResourcesFromFile(*inArgs.InputConfigFile)
if err1 != nil {
return fmt.Errorf("error parsing input vpc resources file: %w", err1)
}

vpcConfigs, err2 := ibmvpc.VPCConfigsFromResources(rc, *inArgs.VPC, *inArgs.Debug)
vpcConfigs1, err2 := vpcConfigsFromFile(*inArgs.InputConfigFile, inArgs)
if err2 != nil {
return fmt.Errorf(ErrorFormat, InGenerationErr, err2)
}

outFile := ""
if inArgs.OutputFile != nil {
outFile = *inArgs.OutputFile
return err2
}

diffAnalysis := *inArgs.AnalysisType == allEndpointsDiff || *inArgs.AnalysisType == allSubnetsDiff
if !diffAnalysis {
vpcAnalysisOutput, err2 := analysisVPCConfigs(vpcConfigs, nil, inArgs, outFile)
var vpcConfigs2 map[string]*vpcmodel.VPCConfig
if inArgs.InputSecondConfigFile != nil && *inArgs.InputSecondConfigFile != "" {
vpcConfigs2, err2 = vpcConfigsFromFile(*inArgs.InputSecondConfigFile, inArgs)
if err2 != nil {
return err2
}
fmt.Println(vpcAnalysisOutput)
} else {
return diffAnalysisMain(inArgs, vpcConfigs, outFile)
}
return nil
}

func diffAnalysisMain(inArgs *InArgs, vpcConfigs map[string]*vpcmodel.VPCConfig, outFile string) error {
// ToDo SM: for diff analysis assume 2 configs only, the 2nd given through vpc-config-second
rc2ndForDiff, err1 := ibmvpc.ParseResourcesFromFile(*inArgs.InputSecondConfigFile)
if err1 != nil {
return fmt.Errorf(ErrorFormat, ParsingErr, err1)
}
vpc2ndConfigs, err2 := ibmvpc.VPCConfigsFromResources(rc2ndForDiff, *inArgs.VPC, *inArgs.Debug)
if err2 != nil {
return fmt.Errorf(ErrorFormat, InGenerationErr, err2)
// we are in diff mode, checking we have only one config per file:
if len(vpcConfigs1) != 1 || len(vpcConfigs2) != 1 {
return fmt.Errorf("for diff mode %v a single configuration should be provided "+
"for both -vpc-config and -vpc-config-second", *inArgs.AnalysisType)
}
}
// For diff analysis each vpcConfigs have a single element
if len(vpcConfigs) != 1 || len(vpc2ndConfigs) != 1 {
return fmt.Errorf("for diff mode %v a single configuration should be provided "+
"for both -vpc-config and -vpc-config-second", *inArgs.AnalysisType)
outFile := ""
if inArgs.OutputFile != nil {
outFile = *inArgs.OutputFile
}
analysisOutput, err2 := analysisVPCConfigs(vpcConfigs, vpc2ndConfigs, inArgs, outFile)
vpcAnalysisOutput, err2 := analysisVPCConfigs(vpcConfigs1, vpcConfigs2, inArgs, outFile)
if err2 != nil {
return err2
}
fmt.Println(analysisOutput)
fmt.Println(vpcAnalysisOutput)
return nil
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/common/genericSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ func (s GenericSet[T]) IsIntersect(s2 GenericSet[T]) bool {
}

// /////////////////////////////////////////////////////////////////
// AnyMapEntry() return one arbitrary entry of a map.
// the user of this function should know that "arbitrary" means that *any* entry in the map can be returned,
// so, the user will probably use this function only in cases that it does not matter which of the entries is returned.
// however, the user can use this function for other cases, but she should know that it return an arbitrary entry of a map.
// AnyMapEntry() return a random entry of a map.
// this func is not related to genericSet,
// todo: consider moving to another file
// todo: AnyMapEntry() for GenericSet, and use it
// todo: create AnyMapEntry() for GenericSet, and use it
// /////////////////////////////////////////////////////////
func AnyMapEntry[K comparable, V any](m map[K]V) (k K, v V) {
for k, v = range m {
Expand Down

0 comments on commit 1b5c8c1

Please sign in to comment.