summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoyalsoldier <[email protected]>2024-07-08 13:54:29 +0800
committerLoyalsoldier <[email protected]>2024-07-08 14:23:41 +0800
commit56ddd1bd048e6d42645f36cd473a211dca90b5dc (patch)
tree1832981ab31a2c457992e053f0dd94453ac475f5
parent8c55b323a91acff7cc7f839a371ec9510fe60039 (diff)
Refactor: CLI commands
old usage: geoip -l new usage: geoip list old usage: geoip -c config.json new usage: geoip convert -c config.json
-rw-r--r--.github/workflows/build.yml3
-rw-r--r--README.md75
-rw-r--r--convert.go36
-rw-r--r--go.mod5
-rw-r--r--go.sum12
-rw-r--r--list.go21
-rw-r--r--main.go33
7 files changed, 127 insertions, 58 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d80c5a76..d8b0e068 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -56,7 +56,8 @@ jobs:
- name: Build geoip files
run: |
- go run ./
+ go build ./
+ ./geoip convert -c ./config.json
- name: Verify mmdb files
run: |
diff --git a/README.md b/README.md
index 5c993e5e..60669e26 100644
--- a/README.md
+++ b/README.md
@@ -227,13 +227,52 @@ 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.
+```
+
+```bash
+$ ./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)
+```
+
+```bash
+$ ./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
@@ -255,30 +294,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
diff --git a/convert.go b/convert.go
new file mode 100644
index 00000000..6635137f
--- /dev/null
+++ b/convert.go
@@ -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)
+ }
+ },
+}
diff --git a/go.mod b/go.mod
index 37f698b3..9f17a26b 100644
--- a/go.mod
+++ b/go.mod
@@ -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
@@ -17,6 +18,7 @@ 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
@@ -24,6 +26,7 @@ require (
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
diff --git a/go.sum b/go.sum
index 9a115a95..90b28631 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
@@ -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=
@@ -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=
@@ -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=
@@ -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=
diff --git a/list.go b/list.go
new file mode 100644
index 00000000..1a05bea5
--- /dev/null
+++ b/list.go
@@ -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()
+ },
+}
diff --git a/main.go b/main.go
index 8dd8f45b..efc6a46a 100644
--- a/main.go
+++ b/main.go
@@ -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)
}
}