Skip to content

Commit

Permalink
Merge pull request #195 from shamirShahzad/refactor
Browse files Browse the repository at this point in the history
refactoring project
  • Loading branch information
UmanShahzad authored Dec 17, 2023
2 parents c2edb95 + 4474942 commit 899bac9
Show file tree
Hide file tree
Showing 73 changed files with 456 additions and 406 deletions.
4 changes: 2 additions & 2 deletions ipinfo/cmd_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net"

"github.com/fatih/color"
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/ipinfo/cli/lib/iputil"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -111,7 +111,7 @@ func cmdBulk() (err error) {
return nil
}

ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ipinfo/cmd_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/fatih/color"
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/iputil"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -196,7 +197,7 @@ func cmdDefault() (err error) {
return nil
}

ips = lib.IPListFromStdin()
ips = iputil.IPListFromStdin()
if len(ips) == 0 {
fmt.Println("no input ips")
return nil
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/cmd_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"net"

"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/ipinfo/cli/lib/iputil"
"github.com/pkg/browser"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -69,7 +69,7 @@ func cmdMap() (err error) {
return nil
}

ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/cmd_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"

"github.com/fatih/color"
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/ipinfo/cli/lib/iputil"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -101,7 +101,7 @@ func cmdSum() (err error) {
return nil
}

ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions ipinfo/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"os"

"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/ipinfo/cli/lib/iputil"
)

var completions = &complete.Command{
Expand Down Expand Up @@ -50,9 +50,9 @@ func handleCompletions() {
args := complete.Parse(line)
if len(args) > 1 {
cmdSecondArg := args[1].Text
if lib.StrIsIPStr(cmdSecondArg) {
if iputil.StrIsIPStr(cmdSecondArg) {
completions.Sub[cmdSecondArg] = completionsIP
} else if lib.StrIsASNStr(cmdSecondArg) {
} else if iputil.StrIsASNStr(cmdSecondArg) {
completions.Sub[cmdSecondArg] = completionsASNSingle
}
}
Expand Down
6 changes: 3 additions & 3 deletions ipinfo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"path/filepath"

"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/iputil"
)

// global config.
Expand Down Expand Up @@ -62,7 +62,7 @@ func InitConfig() error {
}

// create default config if none yet.
if !lib.FileExists(configpath) {
if !iputil.FileExists(configpath) {
gConfig = NewConfig()

tokenpath, err := TokenPath()
Expand All @@ -71,7 +71,7 @@ func InitConfig() error {
}

// if token exists, migrate it to the config file.
if lib.FileExists(tokenpath) {
if iputil.FileExists(tokenpath) {
token, err := ReadTokenFile()
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions ipinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/iputil"
)

var progBase = filepath.Base(os.Args[0])
Expand Down Expand Up @@ -35,9 +35,9 @@ func main() {
}

switch {
case lib.StrIsIPStr(cmd):
case iputil.StrIsIPStr(cmd):
err = cmdIP(cmd)
case lib.StrIsASNStr(cmd):
case iputil.StrIsASNStr(cmd):
asn := strings.ToUpper(cmd)
err = cmdASNSingle(asn)
case len(cmd) >= 3 && strings.IndexByte(cmd, '.') != -1:
Expand Down
14 changes: 8 additions & 6 deletions lib/cmd_asn_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package lib

import (
"errors"
"strings"

"github.com/ipinfo/cli/lib/iputil"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/spf13/pflag"
"strings"
)

// CmdASNBulkFlags are flags expected by CmdASNBulk
Expand Down Expand Up @@ -63,22 +65,22 @@ func CmdASNBulk(f CmdASNBulkFlags, ii *ipinfo.Client, args []string, printHelp f

var asns []string

op := func(string string, inputType INPUT_TYPE) error {
op := func(string string, inputType iputil.INPUT_TYPE) error {
switch inputType {
case INPUT_TYPE_ASN:
case iputil.INPUT_TYPE_ASN:
asns = append(asns, strings.ToUpper(string))
default:
return ErrInvalidInput
return iputil.ErrInvalidInput
}
return nil
}
err := GetInputFrom(args, true, true, op)
err := iputil.GetInputFrom(args, true, true, op)
if err != nil {
return nil, err
}

if ii.Token == "" {
return nil, errors.New("bulk lookups require a token; login via `ipinfo init`.")
return nil, errors.New("bulk lookups require a token; login via `ipinfo init`")
}

return ii.GetASNDetailsBatch(asns, ipinfo.BatchReqOpts{
Expand Down
28 changes: 15 additions & 13 deletions lib/cmd_calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package lib

import (
"fmt"
"github.com/fatih/color"
"github.com/spf13/pflag"
"math"
"math/big"
"net"
"regexp"
"strconv"
"strings"

"github.com/fatih/color"
"github.com/ipinfo/cli/lib/iputil"
"github.com/spf13/pflag"
)

// CmdCalcFlags are flags expected by CmdCalc
Expand Down Expand Up @@ -105,21 +107,21 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
if el == "" {
continue
}
if isFloat(el) || StrIsIPv4Str(el) || StrIsIPv6Str(el) {
if isFloat(el) || iputil.StrIsIPv4Str(el) || iputil.StrIsIPv6Str(el) {
postfixStack.Push(el)
continue
}

// if operator pop two elements off of the stack.
strNum1, isEmpty := postfixStack.Pop()
if isEmpty {
return big.NewFloat(0), ErrInvalidInput
return big.NewFloat(0), iputil.ErrInvalidInput
}
num1, _, _ := big.ParseFloat(strNum1, 10, precision, big.ToZero)

strNum2, isEmpty := postfixStack.Pop()
if isEmpty {
return big.NewFloat(0), ErrInvalidInput
return big.NewFloat(0), iputil.ErrInvalidInput
}
num2, _, _ := big.ParseFloat(strNum2, 10, precision, big.ToZero)

Expand All @@ -136,7 +138,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
case operator == "/":
// Check for division by zero
if num1.Cmp(big.NewFloat(0)) == 0 {
return big.NewFloat(0), ErrInvalidInput
return big.NewFloat(0), iputil.ErrInvalidInput
}
result = new(big.Float).Quo(num2, num1)
case operator == "^":
Expand All @@ -148,7 +150,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
res := math.Pow(num2F64, num1F64)
result = new(big.Float).SetPrec(precision).SetFloat64(res)
default:
return big.NewFloat(0), ErrInvalidInput
return big.NewFloat(0), iputil.ErrInvalidInput
}
strResult := result.Text('f', 50)
postfixStack.Push(strResult)
Expand All @@ -175,19 +177,19 @@ func translateToken(tempToken string, tokens []string) ([]string, error) {

if isFloat(tempToken) {
tokens = append(tokens, tempToken)
} else if StrIsIPv4Str(tempToken) {
} else if iputil.StrIsIPv4Str(tempToken) {
// Convert ipv4 to decimal then append to tokens
ip := net.ParseIP(tempToken)
decimalIP := IP4toInt(ip)
decimalIP := iputil.IP4toInt(ip)
res := strconv.FormatInt(decimalIP, 10)
tokens = append(tokens, res)

} else if StrIsIPv6Str(tempToken) {
} else if iputil.StrIsIPv6Str(tempToken) {
ip := net.ParseIP(tempToken)
decimalIP := IP6toInt(ip)
decimalIP := iputil.IP6toInt(ip)
tokens = append(tokens, decimalIP.String())
} else {
return []string{}, ErrInvalidInput
return []string{}, iputil.ErrInvalidInput
}
return tokens, nil
}
Expand Down Expand Up @@ -324,7 +326,7 @@ func CmdCalc(f CmdCalcFlags, args []string, printHelp func()) error {

infix := args[0]
if IsInvalidInfix(infix) {
return ErrInvalidInput
return iputil.ErrInvalidInput
}

tokens, err := TokenizeInfix(infix)
Expand Down
3 changes: 2 additions & 1 deletion lib/cmd_cidr2ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"os"

"github.com/ipinfo/cli/lib/iputil"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -37,5 +38,5 @@ func CmdCIDR2IP(f CmdCIDR2IPFlags, args []string, printHelp func()) error {
return nil
}

return IPListWriteFrom(args, true, false, false, true, true)
return iputil.IPListWriteFrom(args, true, false, false, true, true)
}
9 changes: 5 additions & 4 deletions lib/cmd_cidr2range.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/ipinfo/cli/lib/iputil"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -82,13 +83,13 @@ func CmdCIDR2Range(

cidrStr := d[:sepIdx]
if strings.IndexByte(cidrStr, ':') == -1 {
if r, err := IPRangeStrFromCIDR(cidrStr); err == nil {
if r, err := iputil.IPRangeStrFromCIDR(cidrStr); err == nil {
fmt.Printf("%s%s", r.String(), rem)
} else {
goto noip
}
} else {
if r, err := IP6RangeStrFromCIDR(cidrStr); err == nil {
if r, err := iputil.IP6RangeStrFromCIDR(cidrStr); err == nil {
fmt.Printf("%s%s", r.String(), rem)
} else {
goto noip
Expand Down Expand Up @@ -123,12 +124,12 @@ func CmdCIDR2Range(
if err != nil {
// is it a CIDR?
if strings.IndexByte(arg, ':') == -1 {
if r, err := IPRangeStrFromCIDR(arg); err == nil {
if r, err := iputil.IPRangeStrFromCIDR(arg); err == nil {
fmt.Println(r.String())
continue
}
} else {
if r, err := IP6RangeStrFromCIDR(arg); err == nil {
if r, err := iputil.IP6RangeStrFromCIDR(arg); err == nil {
fmt.Println(r.String())
continue
}
Expand Down
Loading

0 comments on commit 899bac9

Please sign in to comment.