From 36c9de10bb8f24f968a271653bf75b7b213dfa6e Mon Sep 17 00:00:00 2001 From: lionsoul2014 Date: Thu, 9 Apr 2026 21:06:08 +0800 Subject: [PATCH] use the sCmd args for sub-command --- maker/golang/cmd/bench.go | 11 ++++++----- maker/golang/cmd/edit.go | 4 ++-- maker/golang/cmd/generate.go | 4 ++-- maker/golang/cmd/process.go | 4 ++-- maker/golang/cmd/search.go | 20 ++++++++++---------- maker/golang/cmd/util.go | 11 ----------- maker/golang/main.go | 28 ++++++++++++++++++++-------- 7 files changed, 42 insertions(+), 40 deletions(-) diff --git a/maker/golang/cmd/bench.go b/maker/golang/cmd/bench.go index cbfa9c9..fb5c7bf 100644 --- a/maker/golang/cmd/bench.go +++ b/maker/golang/cmd/bench.go @@ -9,7 +9,7 @@ import ( "github.com/lionsoul2014/ip2region/maker/golang/xdb" ) -func Bench() { +func Bench(sCmd string) { var err error var dbFile, srcFile, ipVersion, logLevel = "", "", "", "" var ignoreError = false @@ -24,11 +24,12 @@ func Bench() { case "log-level": logLevel = val case "ignore-error": - if val == "true" || val == "1" { + switch val { + case "true", "1": ignoreError = true - } else if val == "false" || val == "0" { + case "false", "0": ignoreError = false - } else { + default: return fmt.Errorf("invalid value for ignore-error option, could be false/0 or true/1") } default: @@ -42,7 +43,7 @@ func Bench() { } 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(" --db string ip2region binary xdb file path\n") fmt.Printf(" --src string source ip text file path\n") diff --git a/maker/golang/cmd/edit.go b/maker/golang/cmd/edit.go index 520c672..f0ae218 100644 --- a/maker/golang/cmd/edit.go +++ b/maker/golang/cmd/edit.go @@ -18,7 +18,7 @@ import ( // source ip data editor -func Edit() { +func Edit(sCmd string) { var err error var srcFile, ipVersion = "", "" var fErr = IterateFlags(func(key string, val string) error { @@ -38,7 +38,7 @@ func Edit() { } 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(" --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") diff --git a/maker/golang/cmd/generate.go b/maker/golang/cmd/generate.go index 7fff59f..373330b 100644 --- a/maker/golang/cmd/generate.go +++ b/maker/golang/cmd/generate.go @@ -15,7 +15,7 @@ import ( // script to do the xdb generate -func Generate() { +func Generate(sCmd string) { var err error var srcFile, dstFile = "", "" var ipVersion, fieldList, logLevel = "", "", "info" @@ -49,7 +49,7 @@ func Generate() { } 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(" --src string source ip text file path\n") fmt.Printf(" --dst string destination binary xdb file path\n") diff --git a/maker/golang/cmd/process.go b/maker/golang/cmd/process.go index b0559cd..5e21b56 100644 --- a/maker/golang/cmd/process.go +++ b/maker/golang/cmd/process.go @@ -16,7 +16,7 @@ import ( // source data process, sort, de-duplicate, merge -func Process() { +func Process(sCmd string) { var err error var srcFile, dstFile = "", "" var fieldList, logLevel = "", "" @@ -54,7 +54,7 @@ func Process() { } 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(" --src string source ip text file path\n") fmt.Printf(" --dst string target ip text file path\n") diff --git a/maker/golang/cmd/search.go b/maker/golang/cmd/search.go index e8e39ef..edb38b9 100644 --- a/maker/golang/cmd/search.go +++ b/maker/golang/cmd/search.go @@ -18,7 +18,7 @@ import ( // xdb searcher test -func Search() { +func Search(sCmd string) { var err error var dbFile = "" var fErr = IterateFlags(func(key string, val string) error { @@ -35,7 +35,7 @@ func Search() { } 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(" --db string ip2region binary xdb file path\n") return @@ -49,21 +49,21 @@ func Search() { } var version *xdb.Version = nil - versionNo := binary.LittleEndian.Uint16(header[0:]) - if versionNo == 2 { + switch versionNo := binary.LittleEndian.Uint16(header[0:]); versionNo { + case 2: // old xdb file version = xdb.IPv4 - } else if versionNo == 3 { - ipNo := int(binary.LittleEndian.Uint16(header[16:])) - if ipNo == xdb.IPv4.Id { + case 3: + switch ipNo := int(binary.LittleEndian.Uint16(header[16:])); ipNo { + case xdb.IPv4.Id: version = xdb.IPv4 - } else if ipNo == xdb.IPv6.Id { + case xdb.IPv6.Id: version = xdb.IPv6 - } else { + default: slog.Error("invalid ip version", "id", ipNo) return } - } else { + default: slog.Error("invalid xdb version", "versionNo", versionNo, "xdbFile", dbFile) return } diff --git a/maker/golang/cmd/util.go b/maker/golang/cmd/util.go index 9cabff5..847c81c 100644 --- a/maker/golang/cmd/util.go +++ b/maker/golang/cmd/util.go @@ -10,17 +10,6 @@ import ( "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 func IterateFlags(cb func(key string, val string) error) error { for i := 2; i < len(os.Args); i++ { diff --git a/maker/golang/main.go b/maker/golang/main.go index 09044e2..23a4fd0 100644 --- a/maker/golang/main.go +++ b/maker/golang/main.go @@ -5,6 +5,7 @@ package main import ( + "fmt" "log" "os" "strings" @@ -12,25 +13,36 @@ import ( "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() { if len(os.Args) < 2 { - cmd.PrintHelp() + printHelp() return } log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) - switch strings.ToLower(os.Args[1]) { + switch sCmd := strings.ToLower(os.Args[1]); sCmd { case "gen": - cmd.Generate() + cmd.Generate(sCmd) case "search": - cmd.Search() + cmd.Search(sCmd) case "bench": - cmd.Bench() + cmd.Bench(sCmd) case "edit": - cmd.Edit() + cmd.Edit(sCmd) case "process": - cmd.Process() + cmd.Process(sCmd) default: - cmd.PrintHelp() + printHelp() } }