source ip editor

This commit is contained in:
Lion 2022-12-01 17:36:51 +08:00
parent b4a525a737
commit d6e8813fef
2 changed files with 169 additions and 62 deletions

View File

@ -21,12 +21,11 @@ func printHelp() {
fmt.Printf(" gen generate the binary db file\n") fmt.Printf(" gen generate the binary db file\n")
fmt.Printf(" search binary xdb search test\n") fmt.Printf(" search binary xdb search test\n")
fmt.Printf(" bench binary xdb bench test\n") fmt.Printf(" bench binary xdb bench test\n")
fmt.Printf(" edit edit the source ip data\n")
} }
func genDb() { // Iterate the cli flags
var err error func iterateFlags(cb func(key string, val string) error) error {
var srcFile, dstFile = "", ""
var indexPolicy = xdb.VectorIndexPolicy
for i := 2; i < len(os.Args); i++ { for i := 2; i < len(os.Args); i++ {
r := os.Args[i] r := os.Args[i]
if len(r) < 5 { if len(r) < 5 {
@ -39,25 +38,41 @@ func genDb() {
var sIdx = strings.Index(r, "=") var sIdx = strings.Index(r, "=")
if sIdx < 0 { if sIdx < 0 {
fmt.Printf("missing = for args pair '%s'\n", r) return fmt.Errorf("missing = for args pair '%s'", r)
return
} }
switch r[2:sIdx] { if err := cb(r[2:sIdx], r[sIdx+1:]); err != nil {
return err
}
}
return nil
}
func genDb() {
var err error
var srcFile, dstFile = "", ""
var indexPolicy = xdb.VectorIndexPolicy
var fErr = iterateFlags(func(key string, val string) error {
switch key {
case "src": case "src":
srcFile = r[sIdx+1:] srcFile = val
case "dst": case "dst":
dstFile = r[sIdx+1:] dstFile = val
case "index": case "index":
indexPolicy, err = xdb.IndexPolicyFromString(r[sIdx+1:]) indexPolicy, err = xdb.IndexPolicyFromString(val)
if err != nil { if err != nil {
fmt.Printf("parse policy: %s", err.Error()) return fmt.Errorf("parse policy: %w", err)
return
} }
default: default:
fmt.Printf("undefine option `%s`\n", r) return fmt.Errorf("undefine option `%s=%s`\n", key, val)
return
} }
return nil
})
if fErr != nil {
fmt.Printf("failed to parse flags: %s", fErr)
return
} }
if srcFile == "" || dstFile == "" { if srcFile == "" || dstFile == "" {
@ -99,29 +114,17 @@ func genDb() {
func testSearch() { func testSearch() {
var err error var err error
var dbFile = "" var dbFile = ""
for i := 2; i < len(os.Args); i++ { var fErr = iterateFlags(func(key string, val string) error {
r := os.Args[i] if key == "db" {
if len(r) < 5 { dbFile = val
continue } else {
} return fmt.Errorf("undefined option '%s=%s'\n", key, val)
if strings.Index(r, "--") != 0 {
continue
}
var eIdx = strings.Index(r, "=")
if eIdx < 0 {
fmt.Printf("missing = for args pair '%s'\n", r)
return
}
switch r[2:eIdx] {
case "db":
dbFile = r[eIdx+1:]
default:
fmt.Printf("undefined option '%s'\n", r)
return
} }
return nil
})
if fErr != nil {
fmt.Printf("failed to parse flags: %s", fErr)
return
} }
if dbFile == "" { if dbFile == "" {
@ -194,41 +197,28 @@ func testBench() {
var err error var err error
var dbFile, srcFile = "", "" var dbFile, srcFile = "", ""
var ignoreError = false var ignoreError = false
for i := 2; i < len(os.Args); i++ { var fErr = iterateFlags(func(key string, val string) error {
r := os.Args[i] switch key {
if len(r) < 5 {
continue
}
if strings.Index(r, "--") != 0 {
continue
}
var sIdx = strings.Index(r, "=")
if sIdx < 0 {
fmt.Printf("missing = for args pair '%s'\n", r)
return
}
switch r[2:sIdx] {
case "db": case "db":
dbFile = r[sIdx+1:] dbFile = val
case "src": case "src":
srcFile = r[sIdx+1:] srcFile = val
case "ignore-error": case "ignore-error":
v := r[sIdx+1:] if val == "true" || val == "1" {
if v == "true" || v == "1" {
ignoreError = true ignoreError = true
} else if v == "false" || v == "0" { } else if val == "false" || val == "0" {
ignoreError = false ignoreError = false
} else { } else {
fmt.Printf("invalid value for ignore-error option, could be false/0 or true/1\n") return fmt.Errorf("invalid value for ignore-error option, could be false/0 or true/1\n")
return
} }
default: default:
fmt.Printf("undefined option '%s'\n", r) return fmt.Errorf("undefined option '%s=%s'\n", key, val)
return
} }
return nil
})
if fErr != nil {
fmt.Printf("failed to parse flags: %s", fErr)
return
} }
if dbFile == "" || srcFile == "" { if dbFile == "" || srcFile == "" {
@ -310,6 +300,91 @@ func testBench() {
fmt.Printf("Bench finished, {count: %d, failed: %d, took: %s}\n", count, errCount, time.Since(tStart)) fmt.Printf("Bench finished, {count: %d, failed: %d, took: %s}\n", count, errCount, time.Since(tStart))
} }
func edit() {
var err error
var srcFile, dstFile = "", ""
var fErr = iterateFlags(func(key string, val string) error {
switch key {
case "src":
srcFile = val
case "dst":
dstFile = val
default:
return fmt.Errorf("undefined option '%s=%s'\n", key, val)
}
return nil
})
if fErr != nil {
fmt.Printf("failed to parse flags: %s", fErr)
return
}
if dstFile == "" || srcFile == "" {
fmt.Printf("%s edit [command options]\n", os.Args[0])
fmt.Printf("options:\n")
fmt.Printf(" --src string source ip text file path\n")
fmt.Printf(" --dst string destination source file path\n")
return
}
fmt.Printf("init the editor from source @ `%s` ... \n", srcFile)
editor, err := xdb.NewEditor(srcFile)
if err != nil {
fmt.Printf("failed to init editor: %s", err)
return
}
var help = func() {
fmt.Printf("command list: \n")
fmt.Printf(" put [segment] : put the specifield segment\n")
fmt.Printf(" put_file [file] : put all the segments from the specified file\n")
fmt.Printf(" save : save all the changes to the destination source file\n")
fmt.Printf(" exit : exit the program\n")
fmt.Printf(" help : print this help menu\n")
}
help()
var reader = bufio.NewReader(os.Stdin)
for {
fmt.Printf(">> ")
line, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("failed to read line from cli: %s\n", err)
break
}
cmd := strings.TrimSpace(line)
if cmd == "help" {
help()
} else if cmd == "exit" {
break
} else if cmd == "save" {
err = editor.Save()
if err != nil {
fmt.Printf("failed to save the changes: %s", err)
continue
}
fmt.Printf("Changes saved\n")
} else if strings.HasPrefix(cmd, "put ") {
seg := cmd[len("put "):]
err = editor.Put(seg)
if err != nil {
fmt.Printf("failed to Put(%s): %s\n", seg, err)
continue
}
fmt.Printf("Put(%s): Ok\n", seg)
} else if strings.HasPrefix(cmd, "put_file ") {
file := cmd[len("put_file "):]
err = editor.PutFile(file)
if err != nil {
fmt.Printf("failed to PutFile(%s): %s\n", file, err)
continue
}
fmt.Printf("PutFile(%s): Ok\n", file)
}
}
}
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
printHelp() printHelp()
@ -325,6 +400,8 @@ func main() {
testSearch() testSearch()
case "bench": case "bench":
testBench() testBench()
case "edit":
edit()
default: default:
printHelp() printHelp()
} }

View File

@ -0,0 +1,30 @@
// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
// original source ip editor
package xdb
type Editor struct {
}
func NewEditor(srcFile string) (*Editor, error) {
return nil, nil
}
func (e *Editor) Put(ip string) error {
return nil
}
func (e *Editor) PutFile(src string) error {
return nil
}
func (e *Editor) Save() error {
return nil
}
func (e *Editor) Close() error {
return nil
}