use the sCmd args for sub-command
This commit is contained in:
parent
61ef15cff0
commit
36c9de10bb
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/lionsoul2014/ip2region/maker/golang/xdb"
|
"github.com/lionsoul2014/ip2region/maker/golang/xdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Bench() {
|
func Bench(sCmd string) {
|
||||||
var err error
|
var err error
|
||||||
var dbFile, srcFile, ipVersion, logLevel = "", "", "", ""
|
var dbFile, srcFile, ipVersion, logLevel = "", "", "", ""
|
||||||
var ignoreError = false
|
var ignoreError = false
|
||||||
|
|
@ -24,11 +24,12 @@ func Bench() {
|
||||||
case "log-level":
|
case "log-level":
|
||||||
logLevel = val
|
logLevel = val
|
||||||
case "ignore-error":
|
case "ignore-error":
|
||||||
if val == "true" || val == "1" {
|
switch val {
|
||||||
|
case "true", "1":
|
||||||
ignoreError = true
|
ignoreError = true
|
||||||
} else if val == "false" || val == "0" {
|
case "false", "0":
|
||||||
ignoreError = false
|
ignoreError = false
|
||||||
} else {
|
default:
|
||||||
return fmt.Errorf("invalid value for ignore-error option, could be false/0 or true/1")
|
return fmt.Errorf("invalid value for ignore-error option, could be false/0 or true/1")
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
@ -42,7 +43,7 @@ func Bench() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbFile == "" || srcFile == "" {
|
if dbFile == "" || srcFile == "" {
|
||||||
fmt.Printf("%s bench [command options]\n", os.Args[0])
|
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||||
fmt.Printf("options:\n")
|
fmt.Printf("options:\n")
|
||||||
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
||||||
fmt.Printf(" --src string source ip text file path\n")
|
fmt.Printf(" --src string source ip text file path\n")
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
// source ip data editor
|
// source ip data editor
|
||||||
|
|
||||||
func Edit() {
|
func Edit(sCmd string) {
|
||||||
var err error
|
var err error
|
||||||
var srcFile, ipVersion = "", ""
|
var srcFile, ipVersion = "", ""
|
||||||
var fErr = IterateFlags(func(key string, val string) error {
|
var fErr = IterateFlags(func(key string, val string) error {
|
||||||
|
|
@ -38,7 +38,7 @@ func Edit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if srcFile == "" {
|
if srcFile == "" {
|
||||||
fmt.Printf("%s edit [command options]\n", os.Args[0])
|
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||||
fmt.Printf("options:\n")
|
fmt.Printf("options:\n")
|
||||||
fmt.Printf(" --src string source ip text file path\n")
|
fmt.Printf(" --src string source ip text file path\n")
|
||||||
fmt.Printf(" --version string IP version, options: ipv4/ipv6, specify this flag so you don't get confused \n")
|
fmt.Printf(" --version string IP version, options: ipv4/ipv6, specify this flag so you don't get confused \n")
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import (
|
||||||
|
|
||||||
// script to do the xdb generate
|
// script to do the xdb generate
|
||||||
|
|
||||||
func Generate() {
|
func Generate(sCmd string) {
|
||||||
var err error
|
var err error
|
||||||
var srcFile, dstFile = "", ""
|
var srcFile, dstFile = "", ""
|
||||||
var ipVersion, fieldList, logLevel = "", "", "info"
|
var ipVersion, fieldList, logLevel = "", "", "info"
|
||||||
|
|
@ -49,7 +49,7 @@ func Generate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if srcFile == "" || dstFile == "" {
|
if srcFile == "" || dstFile == "" {
|
||||||
fmt.Printf("%s gen [command options]\n", os.Args[0])
|
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||||
fmt.Printf("options:\n")
|
fmt.Printf("options:\n")
|
||||||
fmt.Printf(" --src string source ip text file path\n")
|
fmt.Printf(" --src string source ip text file path\n")
|
||||||
fmt.Printf(" --dst string destination binary xdb file path\n")
|
fmt.Printf(" --dst string destination binary xdb file path\n")
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
|
|
||||||
// source data process, sort, de-duplicate, merge
|
// source data process, sort, de-duplicate, merge
|
||||||
|
|
||||||
func Process() {
|
func Process(sCmd string) {
|
||||||
var err error
|
var err error
|
||||||
var srcFile, dstFile = "", ""
|
var srcFile, dstFile = "", ""
|
||||||
var fieldList, logLevel = "", ""
|
var fieldList, logLevel = "", ""
|
||||||
|
|
@ -54,7 +54,7 @@ func Process() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if srcFile == "" || dstFile == "" {
|
if srcFile == "" || dstFile == "" {
|
||||||
fmt.Printf("%s process [command options]\n", os.Args[0])
|
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||||
fmt.Printf("options:\n")
|
fmt.Printf("options:\n")
|
||||||
fmt.Printf(" --src string source ip text file path\n")
|
fmt.Printf(" --src string source ip text file path\n")
|
||||||
fmt.Printf(" --dst string target ip text file path\n")
|
fmt.Printf(" --dst string target ip text file path\n")
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
// xdb searcher test
|
// xdb searcher test
|
||||||
|
|
||||||
func Search() {
|
func Search(sCmd string) {
|
||||||
var err error
|
var err error
|
||||||
var dbFile = ""
|
var dbFile = ""
|
||||||
var fErr = IterateFlags(func(key string, val string) error {
|
var fErr = IterateFlags(func(key string, val string) error {
|
||||||
|
|
@ -35,7 +35,7 @@ func Search() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbFile == "" {
|
if dbFile == "" {
|
||||||
fmt.Printf("%s search [command options]\n", os.Args[0])
|
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||||
fmt.Printf("options:\n")
|
fmt.Printf("options:\n")
|
||||||
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
||||||
return
|
return
|
||||||
|
|
@ -49,21 +49,21 @@ func Search() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var version *xdb.Version = nil
|
var version *xdb.Version = nil
|
||||||
versionNo := binary.LittleEndian.Uint16(header[0:])
|
switch versionNo := binary.LittleEndian.Uint16(header[0:]); versionNo {
|
||||||
if versionNo == 2 {
|
case 2:
|
||||||
// old xdb file
|
// old xdb file
|
||||||
version = xdb.IPv4
|
version = xdb.IPv4
|
||||||
} else if versionNo == 3 {
|
case 3:
|
||||||
ipNo := int(binary.LittleEndian.Uint16(header[16:]))
|
switch ipNo := int(binary.LittleEndian.Uint16(header[16:])); ipNo {
|
||||||
if ipNo == xdb.IPv4.Id {
|
case xdb.IPv4.Id:
|
||||||
version = xdb.IPv4
|
version = xdb.IPv4
|
||||||
} else if ipNo == xdb.IPv6.Id {
|
case xdb.IPv6.Id:
|
||||||
version = xdb.IPv6
|
version = xdb.IPv6
|
||||||
} else {
|
default:
|
||||||
slog.Error("invalid ip version", "id", ipNo)
|
slog.Error("invalid ip version", "id", ipNo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
default:
|
||||||
slog.Error("invalid xdb version", "versionNo", versionNo, "xdbFile", dbFile)
|
slog.Error("invalid xdb version", "versionNo", versionNo, "xdbFile", dbFile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PrintHelp() {
|
|
||||||
fmt.Printf("ip2region xdb maker\n")
|
|
||||||
fmt.Printf("%s [command] [command options]\n", os.Args[0])
|
|
||||||
fmt.Printf("Command: \n")
|
|
||||||
fmt.Printf(" gen generate the binary xdb file\n")
|
|
||||||
fmt.Printf(" search binary xdb search test\n")
|
|
||||||
fmt.Printf(" bench binary xdb bench test\n")
|
|
||||||
fmt.Printf(" edit edit the source ip data\n")
|
|
||||||
fmt.Printf(" process process the source ip data\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate the cli flags
|
// Iterate the cli flags
|
||||||
func IterateFlags(cb func(key string, val string) error) error {
|
func IterateFlags(cb func(key string, val string) error) error {
|
||||||
for i := 2; i < len(os.Args); i++ {
|
for i := 2; i < len(os.Args); i++ {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -12,25 +13,36 @@ import (
|
||||||
"github.com/lionsoul2014/ip2region/maker/golang/cmd"
|
"github.com/lionsoul2014/ip2region/maker/golang/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func printHelp() {
|
||||||
|
fmt.Printf("ip2region xdb maker\n")
|
||||||
|
fmt.Printf("%s [command] [command options]\n", os.Args[0])
|
||||||
|
fmt.Printf("Command: \n")
|
||||||
|
fmt.Printf(" gen generate the binary xdb file\n")
|
||||||
|
fmt.Printf(" search binary xdb search test\n")
|
||||||
|
fmt.Printf(" bench binary xdb bench test\n")
|
||||||
|
fmt.Printf(" edit edit the source ip data\n")
|
||||||
|
fmt.Printf(" process process the source ip data\n")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
cmd.PrintHelp()
|
printHelp()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
switch strings.ToLower(os.Args[1]) {
|
switch sCmd := strings.ToLower(os.Args[1]); sCmd {
|
||||||
case "gen":
|
case "gen":
|
||||||
cmd.Generate()
|
cmd.Generate(sCmd)
|
||||||
case "search":
|
case "search":
|
||||||
cmd.Search()
|
cmd.Search(sCmd)
|
||||||
case "bench":
|
case "bench":
|
||||||
cmd.Bench()
|
cmd.Bench(sCmd)
|
||||||
case "edit":
|
case "edit":
|
||||||
cmd.Edit()
|
cmd.Edit(sCmd)
|
||||||
case "process":
|
case "process":
|
||||||
cmd.Process()
|
cmd.Process(sCmd)
|
||||||
default:
|
default:
|
||||||
cmd.PrintHelp()
|
printHelp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue