Skip to content

Commit 899bac9

Browse files
authored
Merge pull request #195 from shamirShahzad/refactor
refactoring project
2 parents c2edb95 + 4474942 commit 899bac9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+456
-406
lines changed

ipinfo/cmd_bulk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"net"
77

88
"github.com/fatih/color"
9-
"github.com/ipinfo/cli/lib"
109
"github.com/ipinfo/cli/lib/complete"
1110
"github.com/ipinfo/cli/lib/complete/predict"
11+
"github.com/ipinfo/cli/lib/iputil"
1212
"github.com/ipinfo/go/v2/ipinfo"
1313
"github.com/spf13/pflag"
1414
)
@@ -111,7 +111,7 @@ func cmdBulk() (err error) {
111111
return nil
112112
}
113113

114-
ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
114+
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
115115
if err != nil {
116116
return err
117117
}

ipinfo/cmd_default.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/fatih/color"
1010
"github.com/ipinfo/cli/lib"
11+
"github.com/ipinfo/cli/lib/iputil"
1112
"github.com/ipinfo/go/v2/ipinfo"
1213
"github.com/spf13/pflag"
1314
)
@@ -196,7 +197,7 @@ func cmdDefault() (err error) {
196197
return nil
197198
}
198199

199-
ips = lib.IPListFromStdin()
200+
ips = iputil.IPListFromStdin()
200201
if len(ips) == 0 {
201202
fmt.Println("no input ips")
202203
return nil

ipinfo/cmd_map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"fmt"
55
"net"
66

7-
"github.com/ipinfo/cli/lib"
87
"github.com/ipinfo/cli/lib/complete"
98
"github.com/ipinfo/cli/lib/complete/predict"
9+
"github.com/ipinfo/cli/lib/iputil"
1010
"github.com/pkg/browser"
1111
"github.com/spf13/pflag"
1212
)
@@ -69,7 +69,7 @@ func cmdMap() (err error) {
6969
return nil
7070
}
7171

72-
ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
72+
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
7373
if err != nil {
7474
return err
7575
}

ipinfo/cmd_sum.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"strings"
99

1010
"github.com/fatih/color"
11-
"github.com/ipinfo/cli/lib"
1211
"github.com/ipinfo/cli/lib/complete"
1312
"github.com/ipinfo/cli/lib/complete/predict"
13+
"github.com/ipinfo/cli/lib/iputil"
1414
"github.com/ipinfo/go/v2/ipinfo"
1515
"github.com/spf13/pflag"
1616
)
@@ -101,7 +101,7 @@ func cmdSum() (err error) {
101101
return nil
102102
}
103103

104-
ips, err = lib.IPListFromAllSrcs(pflag.Args()[1:])
104+
ips, err = iputil.IPListFromAllSrcs(pflag.Args()[1:])
105105
if err != nil {
106106
return err
107107
}

ipinfo/completions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33
import (
44
"os"
55

6-
"github.com/ipinfo/cli/lib"
76
"github.com/ipinfo/cli/lib/complete"
87
"github.com/ipinfo/cli/lib/complete/predict"
8+
"github.com/ipinfo/cli/lib/iputil"
99
)
1010

1111
var completions = &complete.Command{
@@ -50,9 +50,9 @@ func handleCompletions() {
5050
args := complete.Parse(line)
5151
if len(args) > 1 {
5252
cmdSecondArg := args[1].Text
53-
if lib.StrIsIPStr(cmdSecondArg) {
53+
if iputil.StrIsIPStr(cmdSecondArg) {
5454
completions.Sub[cmdSecondArg] = completionsIP
55-
} else if lib.StrIsASNStr(cmdSecondArg) {
55+
} else if iputil.StrIsASNStr(cmdSecondArg) {
5656
completions.Sub[cmdSecondArg] = completionsASNSingle
5757
}
5858
}

ipinfo/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
"path/filepath"
88

9-
"github.com/ipinfo/cli/lib"
9+
"github.com/ipinfo/cli/lib/iputil"
1010
)
1111

1212
// global config.
@@ -62,7 +62,7 @@ func InitConfig() error {
6262
}
6363

6464
// create default config if none yet.
65-
if !lib.FileExists(configpath) {
65+
if !iputil.FileExists(configpath) {
6666
gConfig = NewConfig()
6767

6868
tokenpath, err := TokenPath()
@@ -71,7 +71,7 @@ func InitConfig() error {
7171
}
7272

7373
// if token exists, migrate it to the config file.
74-
if lib.FileExists(tokenpath) {
74+
if iputil.FileExists(tokenpath) {
7575
token, err := ReadTokenFile()
7676
if err != nil {
7777
return err

ipinfo/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88

99
"github.com/fatih/color"
10-
"github.com/ipinfo/cli/lib"
10+
"github.com/ipinfo/cli/lib/iputil"
1111
)
1212

1313
var progBase = filepath.Base(os.Args[0])
@@ -35,9 +35,9 @@ func main() {
3535
}
3636

3737
switch {
38-
case lib.StrIsIPStr(cmd):
38+
case iputil.StrIsIPStr(cmd):
3939
err = cmdIP(cmd)
40-
case lib.StrIsASNStr(cmd):
40+
case iputil.StrIsASNStr(cmd):
4141
asn := strings.ToUpper(cmd)
4242
err = cmdASNSingle(asn)
4343
case len(cmd) >= 3 && strings.IndexByte(cmd, '.') != -1:

lib/cmd_asn_bulk.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package lib
22

33
import (
44
"errors"
5+
"strings"
6+
7+
"github.com/ipinfo/cli/lib/iputil"
58
"github.com/ipinfo/go/v2/ipinfo"
69
"github.com/spf13/pflag"
7-
"strings"
810
)
911

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

6466
var asns []string
6567

66-
op := func(string string, inputType INPUT_TYPE) error {
68+
op := func(string string, inputType iputil.INPUT_TYPE) error {
6769
switch inputType {
68-
case INPUT_TYPE_ASN:
70+
case iputil.INPUT_TYPE_ASN:
6971
asns = append(asns, strings.ToUpper(string))
7072
default:
71-
return ErrInvalidInput
73+
return iputil.ErrInvalidInput
7274
}
7375
return nil
7476
}
75-
err := GetInputFrom(args, true, true, op)
77+
err := iputil.GetInputFrom(args, true, true, op)
7678
if err != nil {
7779
return nil, err
7880
}
7981

8082
if ii.Token == "" {
81-
return nil, errors.New("bulk lookups require a token; login via `ipinfo init`.")
83+
return nil, errors.New("bulk lookups require a token; login via `ipinfo init`")
8284
}
8385

8486
return ii.GetASNDetailsBatch(asns, ipinfo.BatchReqOpts{

lib/cmd_calc.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package lib
22

33
import (
44
"fmt"
5-
"github.com/fatih/color"
6-
"github.com/spf13/pflag"
75
"math"
86
"math/big"
97
"net"
108
"regexp"
119
"strconv"
1210
"strings"
11+
12+
"github.com/fatih/color"
13+
"github.com/ipinfo/cli/lib/iputil"
14+
"github.com/spf13/pflag"
1315
)
1416

1517
// CmdCalcFlags are flags expected by CmdCalc
@@ -105,21 +107,21 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
105107
if el == "" {
106108
continue
107109
}
108-
if isFloat(el) || StrIsIPv4Str(el) || StrIsIPv6Str(el) {
110+
if isFloat(el) || iputil.StrIsIPv4Str(el) || iputil.StrIsIPv6Str(el) {
109111
postfixStack.Push(el)
110112
continue
111113
}
112114

113115
// if operator pop two elements off of the stack.
114116
strNum1, isEmpty := postfixStack.Pop()
115117
if isEmpty {
116-
return big.NewFloat(0), ErrInvalidInput
118+
return big.NewFloat(0), iputil.ErrInvalidInput
117119
}
118120
num1, _, _ := big.ParseFloat(strNum1, 10, precision, big.ToZero)
119121

120122
strNum2, isEmpty := postfixStack.Pop()
121123
if isEmpty {
122-
return big.NewFloat(0), ErrInvalidInput
124+
return big.NewFloat(0), iputil.ErrInvalidInput
123125
}
124126
num2, _, _ := big.ParseFloat(strNum2, 10, precision, big.ToZero)
125127

@@ -136,7 +138,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
136138
case operator == "/":
137139
// Check for division by zero
138140
if num1.Cmp(big.NewFloat(0)) == 0 {
139-
return big.NewFloat(0), ErrInvalidInput
141+
return big.NewFloat(0), iputil.ErrInvalidInput
140142
}
141143
result = new(big.Float).Quo(num2, num1)
142144
case operator == "^":
@@ -148,7 +150,7 @@ func EvaluatePostfix(postfix []string) (*big.Float, error) {
148150
res := math.Pow(num2F64, num1F64)
149151
result = new(big.Float).SetPrec(precision).SetFloat64(res)
150152
default:
151-
return big.NewFloat(0), ErrInvalidInput
153+
return big.NewFloat(0), iputil.ErrInvalidInput
152154
}
153155
strResult := result.Text('f', 50)
154156
postfixStack.Push(strResult)
@@ -175,19 +177,19 @@ func translateToken(tempToken string, tokens []string) ([]string, error) {
175177

176178
if isFloat(tempToken) {
177179
tokens = append(tokens, tempToken)
178-
} else if StrIsIPv4Str(tempToken) {
180+
} else if iputil.StrIsIPv4Str(tempToken) {
179181
// Convert ipv4 to decimal then append to tokens
180182
ip := net.ParseIP(tempToken)
181-
decimalIP := IP4toInt(ip)
183+
decimalIP := iputil.IP4toInt(ip)
182184
res := strconv.FormatInt(decimalIP, 10)
183185
tokens = append(tokens, res)
184186

185-
} else if StrIsIPv6Str(tempToken) {
187+
} else if iputil.StrIsIPv6Str(tempToken) {
186188
ip := net.ParseIP(tempToken)
187-
decimalIP := IP6toInt(ip)
189+
decimalIP := iputil.IP6toInt(ip)
188190
tokens = append(tokens, decimalIP.String())
189191
} else {
190-
return []string{}, ErrInvalidInput
192+
return []string{}, iputil.ErrInvalidInput
191193
}
192194
return tokens, nil
193195
}
@@ -324,7 +326,7 @@ func CmdCalc(f CmdCalcFlags, args []string, printHelp func()) error {
324326

325327
infix := args[0]
326328
if IsInvalidInfix(infix) {
327-
return ErrInvalidInput
329+
return iputil.ErrInvalidInput
328330
}
329331

330332
tokens, err := TokenizeInfix(infix)

lib/cmd_cidr2ip.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lib
33
import (
44
"os"
55

6+
"github.com/ipinfo/cli/lib/iputil"
67
"github.com/spf13/pflag"
78
)
89

@@ -37,5 +38,5 @@ func CmdCIDR2IP(f CmdCIDR2IPFlags, args []string, printHelp func()) error {
3738
return nil
3839
}
3940

40-
return IPListWriteFrom(args, true, false, false, true, true)
41+
return iputil.IPListWriteFrom(args, true, false, false, true, true)
4142
}

0 commit comments

Comments
 (0)