use the sCmd args for sub-command

This commit is contained in:
lionsoul2014 2026-04-09 21:06:08 +08:00
parent 61ef15cff0
commit 36c9de10bb
7 changed files with 42 additions and 40 deletions

View File

@ -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")

View File

@ -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")

View File

@ -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")

View File

@ -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")

View File

@ -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
}

View File

@ -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++ {

View File

@ -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()
}
}