Skip to content

Commit

Permalink
Refactor: CLI commands
Browse files Browse the repository at this point in the history
old usage: geoip -l
new usage: geoip list

old usage: geoip -c config.json
new usage: geoip convert -c config.json
  • Loading branch information
Loyalsoldier committed Jul 8, 2024
1 parent 8c55b32 commit ad3e4c3
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ jobs:
- name: Build geoip files
run: |
go run ./
go build ./
./geoip convert -c ./config.json
- name: Verify mmdb files
run: |
Expand Down
108 changes: 78 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,85 @@ These two concepts are notable: `input` and `output`. The `input` is the data so
可通过 `go install -v github.com/Loyalsoldier/geoip@latest` 直接安装 CLI。

```bash
$ ./geoip -h
Usage of ./geoip:
-c string
URI of the JSON format config file, support both local file path and remote HTTP(S) URL (default "config.json")
-l List all available input and output formats
$ ./geoip
geoip is a convenient tool to merge, convert and lookup IP & CIDR from various formats of geoip data.
$ ./geoip -c config.json
Usage:
geoip [command]
Available Commands:
convert Convert geoip data from one format to another by using config file
help Help about any command
list List all available input and output formats
Flags:
-h, --help help for geoip
Use "geoip [command] --help" for more information about a command.
$ ./geoip help list
List all available input and output formats
Usage:
geoip list [flags]
Aliases:
list, l, ls
Flags:
-h, --help help for list
$ ./geoip list
All available input formats:
- clashRuleSet (Convert ipcidr type of Clash RuleSet to other formats)
- clashRuleSetClassical (Convert classical type of Clash RuleSet to other formats (just processing IP & CIDR lines))
- cutter (Remove data from previous steps)
- maxmindGeoLite2CountryCSV (Convert MaxMind GeoLite2 country CSV data to other formats)
- maxmindMMDB (Convert MaxMind mmdb database to other formats)
- private (Convert LAN and private network CIDR to other formats)
- singboxSRS (Convert sing-box SRS data to other formats)
- stdin (Accept plaintext IP & CIDR from standard input, separated by newline)
- surgeRuleSet (Convert Surge RuleSet to other formats (just processing IP & CIDR lines))
- test (Convert specific CIDR to other formats (for test only))
- text (Convert plaintext IP & CIDR to other formats)
- v2rayGeoIPDat (Convert V2Ray GeoIP dat to other formats)
All available output formats:
- clashRuleSet (Convert data to ipcidr type of Clash RuleSet)
- clashRuleSetClassical (Convert data to classical type of Clash RuleSet)
- maxmindMMDB (Convert data to MaxMind mmdb database format)
- singboxSRS (Convert data to sing-box SRS format)
- stdout (Convert data to plaintext CIDR format and output to standard output)
- surgeRuleSet (Convert data to Surge RuleSet)
- text (Convert data to plaintext CIDR format)
- v2rayGeoIPDat (Convert data to V2Ray GeoIP dat format)
$ ./geoip help convert
Convert geoip data from one format to another by using config file
Usage:
geoip convert [flags]
Aliases:
convert, conv
Flags:
-c, --config string URI of the JSON format config file, support both local file path and remote HTTP(S) URL (default "config.json")
-h, --help help for convert
$ ./geoip convert -c config.json
2021/08/29 12:11:35 ✅ [v2rayGeoIPDat] geoip.dat --> output/dat
2021/08/29 12:11:35 ✅ [v2rayGeoIPDat] geoip-only-cn-private.dat --> output/dat
2021/08/29 12:11:35 ✅ [v2rayGeoIPDat] geoip-asn.dat --> output/dat
Expand All @@ -255,30 +327,6 @@ $ ./geoip -c config.json
2021/08/29 12:11:45 ✅ [singboxSRS] cloudfront.txt --> output/srs
2021/08/29 12:11:45 ✅ [singboxSRS] facebook.txt --> output/srs
2021/08/29 12:11:45 ✅ [singboxSRS] fastly.txt --> output/srs
$ ./geoip -l
All available input formats:
- v2rayGeoIPDat (Convert V2Ray GeoIP dat to other formats)
- maxmindMMDB (Convert MaxMind mmdb database to other formats)
- maxmindGeoLite2CountryCSV (Convert MaxMind GeoLite2 country CSV data to other formats)
- singboxSRS (Convert sing-box SRS data to other formats)
- private (Convert LAN and private network CIDR to other formats)
- stdin (Accept plaintext IP & CIDR from standard input, separated by newline)
- text (Convert plaintext IP & CIDR to other formats)
- clashRuleSetClassical (Convert classical type of Clash RuleSet to other formats (just processing IP & CIDR lines))
- clashRuleSet (Convert ipcidr type of Clash RuleSet to other formats)
- surgeRuleSet (Convert Surge RuleSet to other formats (just processing IP & CIDR lines))
- cutter (Remove data from previous steps)
- test (Convert specific CIDR to other formats (for test only))
All available output formats:
- v2rayGeoIPDat (Convert data to V2Ray GeoIP dat format)
- maxmindMMDB (Convert data to MaxMind mmdb database format)
- singboxSRS (Convert data to sing-box SRS format)
- clashRuleSetClassical (Convert data to classical type of Clash RuleSet)
- clashRuleSet (Convert data to ipcidr type of Clash RuleSet)
- surgeRuleSet (Convert data to Surge RuleSet)
- text (Convert data to plaintext CIDR format)
- stdout (Convert data to plaintext CIDR format and output to standard output)
```

## License
Expand Down
36 changes: 36 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"log"

"github.com/Loyalsoldier/geoip/lib"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(convertCmd)
convertCmd.PersistentFlags().StringP("config", "c", "config.json", "URI of the JSON format config file, support both local file path and remote HTTP(S) URL")
}

var convertCmd = &cobra.Command{
Use: "convert",
Aliases: []string{"conv"},
Short: "Convert geoip data from one format to another by using config file",
Run: func(cmd *cobra.Command, args []string) {
configFile, _ := cmd.Flags().GetString("config")
log.Println("Use config:", configFile)

instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}

if err := instance.Init(configFile); err != nil {
log.Fatal(err)
}

if err := instance.Run(); err != nil {
log.Fatal(err)
}
},
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ toolchain go1.21.10

require (
github.com/maxmind/mmdbwriter v1.0.0
github.com/oschwald/maxminddb-golang v1.13.0
github.com/oschwald/maxminddb-golang v1.13.1
github.com/sagernet/sing-box v1.9.3
github.com/spf13/cobra v1.8.1
github.com/v2fly/v2ray-core/v5 v5.16.1
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
google.golang.org/protobuf v1.34.2
Expand All @@ -17,13 +18,15 @@ require (
require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/miekg/dns v1.1.59 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/quic-go/quic-go v0.43.0 // indirect
github.com/sagernet/sing v0.4.1 // indirect
github.com/sagernet/sing-dns v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.starlark.net v0.0.0-20230612165344-9532f5667272 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -75,6 +76,8 @@ github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a h1:fEBsGL/sjAuJrgah5X
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
Expand Down Expand Up @@ -103,8 +106,8 @@ github.com/onsi/ginkgo/v2 v2.10.0 h1:sfUl4qgLdvkChZrWCYndY2EAu9BRIw1YphNAzy1VNWs
github.com/onsi/ginkgo/v2 v2.10.0/go.mod h1:UDQOh5wbQUlMnkLfVaIUMtQ1Vus92oM+P2JX1aulgcE=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
Expand Down Expand Up @@ -134,6 +137,7 @@ github.com/refraction-networking/utls v1.6.5 h1:Jlfqgs/t1Uy6FHHQ8Fz9ZTrRmP/zS7d/
github.com/refraction-networking/utls v1.6.5/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr8meMVVGxhp+QBTqY91tM8HjEuMjGg=
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/quic-go v0.43.1-beta.2 h1:6YRCE9t1Q3UbNX1/dJGqpwFQbh6DXC6XBrQr2xp6hXY=
github.com/sagernet/quic-go v0.43.1-beta.2/go.mod h1:BkrQYeop7Jx3hN3TW8/76CXcdhYiNPyYEBL/BVJ1ifc=
github.com/sagernet/sing v0.4.1 h1:zVlpE+7k7AFoC2pv6ReqLf0PIHjihL/jsBl5k05PQFk=
Expand All @@ -146,6 +150,10 @@ github.com/secure-io/siv-go v0.0.0-20180922214919-5ff40651e2c4 h1:zOjq+1/uLzn/Xo
github.com/secure-io/siv-go v0.0.0-20180922214919-5ff40651e2c4/go.mod h1:aI+8yClBW+1uovkHw6HM01YXnYB8vohtB9C83wzx34E=
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb h1:XfLJSPIOUX+osiMraVgIrMR27uMXnRJWGm1+GL8/63U=
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb/go.mod h1:bR6DqgcAl1zTcOX8/pE2Qkj9XO00eCNqmKb7lXP8EAg=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
21 changes: 21 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/Loyalsoldier/geoip/lib"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(listCmd)
}

var listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l", "ls"},
Short: "List all available input and output formats",
Run: func(cmd *cobra.Command, args []string) {
lib.ListInputConverter()
println()
lib.ListOutputConverter()
},
}
33 changes: 9 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
package main

import (
"flag"
"log"

"github.com/Loyalsoldier/geoip/lib"
"github.com/spf13/cobra"
)

var (
list = flag.Bool("l", false, "List all available input and output formats")
configFile = flag.String("c", "config.json", "URI of the JSON format config file, support both local file path and remote HTTP(S) URL")
)
var rootCmd = &cobra.Command{
Use: "geoip",
Short: "geoip is a convenient tool to merge, convert and lookup IP & CIDR from various formats of geoip data.",
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
}

func main() {
flag.Parse()

if *list {
lib.ListInputConverter()
lib.ListOutputConverter()
return
}

instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}

if err := instance.Init(*configFile); err != nil {
log.Fatal(err)
}

if err := instance.Run(); err != nil {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}

0 comments on commit ad3e4c3

Please sign in to comment.