Skip to content

Commit

Permalink
refactor(CLI): refactor parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Khalid-Nowaf committed Jul 2, 2024
1 parent f24c092 commit 00780ac
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions pkg/cli/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"os"
"strconv"
"strings"

"github.com/khalid-nowaf/supernet/pkg/supernet"
)
Expand All @@ -19,7 +18,13 @@ type CIDR struct {

type Record map[string]string

func parseJson(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) error) error {
type CidrParser interface {
Parse(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) error) error
}

type JsonParser struct{}

func (_ JsonParser) Parse(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) error) error {
file, err := os.Open(filepath)
if err != nil {
return err
Expand Down Expand Up @@ -58,8 +63,15 @@ func parseJson(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) err
return nil
}

func parseCsv(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) error) error {
file, err := os.Open(filepath)
type CsvCidrParser struct{ isTSV bool }

func (p CsvCidrParser) Parse(cmd *ResolveCmd, filePath string, onEachCidr func(cidr *CIDR) error) error {
// extension := filepath.Ext(filePath)
// if extension != "csv" && extension != "tsv" {
// return fmt.Errorf("File type %s is not supported, please use one of the following [json,csv,tsv]", extension)
// }

file, err := os.Open(filePath)
if err != nil {
return err
}
Expand All @@ -68,9 +80,9 @@ func parseCsv(cmd *ResolveCmd, filepath string, onEachCidr func(cidr *CIDR) erro
// Create a CSV Reader
reader := csv.NewReader(file)

// Optionally, configure reader fields if necessary (e.g., reader.Comma = ';')
// reader.Comma = ' ' // default delimiter
// reader.Comment = '#' // example to ignore lines starting with '#'
if p.isTSV {
reader.Comma = '\t'
}

// Read the header to build the key mapping (assuming first line is the header)
headers, err := reader.Read()
Expand Down Expand Up @@ -110,24 +122,26 @@ func parseCIDR(record Record, cmd *ResolveCmd) (*CIDR, error) {

_, cidr, err := net.ParseCIDR(record[cmd.CidrKey])
if err != nil {
fmt.Printf("Key: %s CIDR: %s \nRecord: %v", cmd.CidrKey, record[cmd.CidrKey], record)
return nil, err
return nil, fmt.Errorf("Can not parse CIDR on Key: %s CIDR: %s \nRecord: %v", cmd.CidrKey, record[cmd.CidrKey], record)
}
priorityIndex, founded := record[cmd.PriorityKey]
if founded {
prioritiesStr := strings.Split(priorityIndex, cmd.PriorityDel)
for _, priority := range prioritiesStr {
i, err := strconv.Atoi(priority)

if err != nil {
return nil, fmt.Errorf("can not convert priority to Int for record: %v", record)

for _, priorityKey := range cmd.PriorityKeys {
var value int
// parse priority value
value, err = strconv.Atoi(record[priorityKey])
if err != nil {
if cmd.FillEmptyPriority {
value = 0
} else {
panic(fmt.Sprintf("Can not parse priority %s in record:%v", priorityKey, record))
}
priorities = append(priorities, uint8(i))
}
} else {
// TODO: check if the priority at same length, if not (mm maybe we fill the result with Zeros)
panic("No priorities values founded, use 0 as default " + cmd.PriorityKey)
priorities = []uint8{0}
// flip priority
if cmd.FlipRankPriority {
value = value * -1
}

priorities = append(priorities, uint8(value))
}

if cidr.IP.To4() == nil {
Expand Down

0 comments on commit 00780ac

Please sign in to comment.