You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
276 lines
8.1 KiB
276 lines
8.1 KiB
package main |
|
|
|
import ( |
|
"bytes" |
|
"context" |
|
"encoding/json" |
|
"fmt" |
|
"github.com/go-redis/redis/v8" |
|
"github.com/gogf/gf/v2/encoding/gjson" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"log" |
|
"os" |
|
"os/exec" |
|
"strings" |
|
"time" |
|
) |
|
|
|
const redisAdress = "119.29.144.246:4114" |
|
const redisPass = "peach" |
|
|
|
const linkAccount = "mongodb://root:peach123@192.168.2.100:27017/PeachValley" |
|
const link = "mongodb://127.0.0.1:27017/PeachValley" |
|
|
|
const notAccount = false |
|
|
|
func main() { |
|
|
|
//fmt.Println("参数: ", len(os.Args)) |
|
var model string |
|
|
|
fmt.Print("下载账号数据输入 1 , 替换账号数据输入 2 : ") |
|
_, err := fmt.Scan(&model) // 读取输入并赋值给变量 |
|
if err != nil { |
|
fmt.Println("输入错误:", err) |
|
return |
|
} |
|
|
|
ctx := context.Background() |
|
rdb := redis.NewClient(&redis.Options{ |
|
Addr: redisAdress, |
|
DialTimeout: 10 * time.Second, |
|
ReadTimeout: 30 * time.Second, |
|
WriteTimeout: 30 * time.Second, |
|
PoolSize: 10, |
|
PoolTimeout: 30 * time.Second, |
|
Password: redisPass, |
|
}) |
|
|
|
if model == "1" { |
|
fmt.Printf("%s准备复制账号信息: \n", model) |
|
copyUnit(ctx, rdb) |
|
} else { |
|
fmt.Printf("%s准备替换账号信息: \n", model) |
|
updateUnit(ctx, rdb) |
|
} |
|
ticker := time.NewTicker(1 * time.Second) |
|
defer ticker.Stop() |
|
|
|
fmt.Println("程序仍在运行...(输入 'ctrl-c' 退出)") |
|
for { |
|
select { |
|
case <-ticker.C: |
|
// 这里可以添加条件检查(如读取文件、网络请求等) |
|
default: |
|
// 非阻塞检查用户输入(需配合 goroutine) |
|
} |
|
} |
|
} |
|
|
|
func copyUnit(ctx context.Context, rdb *redis.Client) { |
|
var destUid string |
|
fmt.Print("输入需要复制的uid: ") |
|
_, err := fmt.Scan(&destUid) // 读取输入并赋值给变量 |
|
if err != nil { |
|
fmt.Println("输入错误:", err) |
|
return |
|
} |
|
|
|
fmt.Printf("正在复制账号: %s\n", destUid) |
|
rdb.Do(ctx, "select", 0) |
|
destId, err := rdb.HGet(ctx, "public_id_for_uid", fmt.Sprint(destUid)).Result() |
|
if err != nil { |
|
fmt.Println("public_id_for_uid: err", err) |
|
return |
|
} |
|
if destId == "" { |
|
fmt.Println("public_id_for_uid: value is nil") |
|
return |
|
} |
|
filename := "Unit.json" |
|
storeFilename := "StoreComponent.json" |
|
userDir := "./" + filename |
|
storeDir := "./" + storeFilename |
|
name := "mongoexport" |
|
args := []string{} |
|
args1 := []string{} |
|
if notAccount { |
|
args = append(args, fmt.Sprintf("--uri=\"%s\"", link)) |
|
args1 = append(args1, fmt.Sprintf("--uri=\"%s\"", link)) |
|
} else { |
|
args = append(args, fmt.Sprintf("--uri=\"%s\"", linkAccount)) |
|
args = append(args, "--authenticationDatabase=admin") |
|
args1 = append(args1, fmt.Sprintf("--uri=\"%s\"", linkAccount)) |
|
args1 = append(args1, "--authenticationDatabase=admin") |
|
} |
|
|
|
args = append(args, "--collection=Unit") |
|
args = append(args, "--jsonArray") |
|
args = append(args, fmt.Sprintf("--out=\"%s\"", userDir)) |
|
args = append(args, fmt.Sprintf("--query=\"{\\\"UniqueId\\\":%s}\"", destUid)) |
|
args = append(args, "--jsonFormat=canonical") |
|
args = append(args, "--type=json") |
|
args1 = append(args1, "--collection=StoreComponent") |
|
args1 = append(args1, "--jsonArray") |
|
args1 = append(args1, fmt.Sprintf("--out=\"%s\"", storeDir)) |
|
args1 = append(args1, fmt.Sprintf("--query=\"{\\\"_id\\\":%s}\"", destId)) |
|
args1 = append(args1, "--jsonFormat=canonical") |
|
args1 = append(args1, "--type=json") |
|
|
|
cmdCommand(ctx, name, args...) |
|
cmdCommand(ctx, name, args1...) |
|
} |
|
|
|
func updateUnit(ctx context.Context, rdb *redis.Client) { |
|
var filename string |
|
var storeFilename string |
|
var destUid string |
|
fmt.Print("输入需要修改的uid: ") |
|
_, err := fmt.Scan(&destUid) // 读取输入并赋值给变量 |
|
if err != nil { |
|
fmt.Println("输入错误:", err) |
|
return |
|
} |
|
fmt.Print("输入需要复制的unit文件名(需加后缀): ") |
|
_, err = fmt.Scan(&filename) // 读取输入并赋值给变量 |
|
if err != nil { |
|
fmt.Println("输入错误:", err) |
|
|
|
return |
|
} |
|
fmt.Printf("正在复制账号: %s\n", filename) |
|
fmt.Print("输入需要复制的store文件名(需加后缀): ") |
|
_, err = fmt.Scan(&storeFilename) // 读取输入并赋值给变量 |
|
if err != nil { |
|
fmt.Println("输入错误:", err) |
|
return |
|
} |
|
fmt.Printf("正在复制账号: %s\n", storeFilename) |
|
fmt.Printf("现在修改账号: %s\n", destUid) |
|
destId, err := rdb.HGet(ctx, "public_id_for_uid", fmt.Sprint(destUid)).Result() |
|
if err != nil { |
|
fmt.Println("public_id_for_uid: err", err) |
|
return |
|
} |
|
if destId == "" { |
|
fmt.Println("public_id_for_uid: value is nil") |
|
return |
|
} |
|
userDir := "./Unit" + fmt.Sprint(destId) + ".json" |
|
srcUserDir := "./" + filename |
|
|
|
writeByte, err1 := os.ReadFile(srcUserDir) |
|
if err1 != nil { |
|
return |
|
} |
|
|
|
srcUid := "" |
|
srcId := "" |
|
var v1 map[string]interface{} |
|
if strings.HasPrefix(string(writeByte), "[") { |
|
user := []interface{}{} |
|
json.Unmarshal(writeByte, &user) |
|
v1 = user[0].(map[string]interface{}) |
|
} else { |
|
json.Unmarshal(writeByte, &v1) |
|
} |
|
|
|
if _, ok := v1["UniqueId"]; ok { |
|
UniqueId := v1["UniqueId"].(map[string]interface{}) |
|
srcUid = fmt.Sprint(UniqueId["$numberInt"]) |
|
} |
|
if _, ok := v1["_id"]; ok { |
|
UniqueId := v1["_id"].(map[string]interface{}) |
|
srcId = fmt.Sprint(UniqueId["$numberLong"]) |
|
} |
|
|
|
userStoreDir := "./StoreComponent" + fmt.Sprint(destUid) + ".json" |
|
srcStoreDir := "./" + storeFilename |
|
writeStoreByte, err1 := os.ReadFile(srcStoreDir) |
|
if err1 != nil { |
|
return |
|
} |
|
|
|
var v2 map[string]interface{} |
|
if strings.HasPrefix(string(writeStoreByte), "[") { |
|
user := []interface{}{} |
|
json.Unmarshal(writeStoreByte, &user) |
|
v2 = user[0].(map[string]interface{}) |
|
} else { |
|
json.Unmarshal(writeStoreByte, &v2) |
|
} |
|
|
|
//log.Printf("copyUnit string(writeByte): %s ", string(writeByte)) |
|
log.Printf("copyUnit srcid: %s, uid: %s, destid: %s, uid: %s", srcId, srcUid, destId, destUid) |
|
//log.Printf("copyUnit string(writeByte): %s ", gjson.MustEncodeString(user)) |
|
if srcId == "" || srcUid == "" { |
|
return |
|
} |
|
writeUnitString := strings.ReplaceAll(gjson.MustEncodeString(v1), srcId, fmt.Sprint(destId)) |
|
writeStoreString := strings.ReplaceAll(gjson.MustEncodeString(v2), srcId, fmt.Sprint(destId)) |
|
//log.Printf("copyUnit writeString: %s,", writeString) |
|
writeUnitString = ReplaceLastOccurrence(writeUnitString, srcUid, fmt.Sprint(destUid)) |
|
//log.Printf("copyUnit writeString: %s,", writeString) |
|
if err = os.WriteFile(userDir, []byte(writeUnitString), 0666); err != nil { |
|
log.Println(err) |
|
return |
|
} |
|
if err = os.WriteFile(userStoreDir, []byte(writeStoreString), 0666); err != nil { |
|
log.Println(err) |
|
return |
|
} |
|
name := "mongoimport" |
|
|
|
args := []string{} |
|
args1 := []string{} |
|
if notAccount { |
|
args = append(args, fmt.Sprintf("--uri=\"%s\"", link)) |
|
args1 = append(args1, fmt.Sprintf("--uri=\"%s\"", link)) |
|
} else { |
|
args = append(args, fmt.Sprintf("--uri=\"%s\"", linkAccount)) |
|
args = append(args, "--authenticationDatabase=admin") |
|
args1 = append(args1, fmt.Sprintf("--uri=\"%s\"", linkAccount)) |
|
args1 = append(args1, "--authenticationDatabase=admin") |
|
} |
|
|
|
args = append(args, "--collection=Unit") |
|
args = append(args, fmt.Sprintf("--file=\"%s\"", userDir)) |
|
args = append(args, "--type=json") |
|
args = append(args, "--mode=upsert") |
|
args1 = append(args1, "--collection=StoreComponent") |
|
args1 = append(args1, fmt.Sprintf("--file=\"%s\"", userStoreDir)) |
|
args1 = append(args1, "--type=json") |
|
args1 = append(args1, "--mode=upsert") |
|
|
|
cmdCommand(ctx, name, args...) |
|
cmdCommand(ctx, name, args1...) |
|
cmdCommand(ctx, "del", userDir) |
|
cmdCommand(ctx, "del", userStoreDir) |
|
|
|
fmt.Println("CopyUnit GetRoleDelta", destId) |
|
} |
|
|
|
func cmdCommand(ctx context.Context, name string, args ...string) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
cmd := exec.Command(name, args...) |
|
var stdout, stderr bytes.Buffer |
|
cmd.Stdout = &stdout // 标准输出 |
|
cmd.Stderr = &stderr // 标准错误 |
|
err := cmd.Run() |
|
if err != nil { |
|
log.Printf("cmd.Run() failed with %s\n", err) |
|
return |
|
} |
|
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) |
|
fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr) |
|
}) |
|
} |
|
|
|
// ReplaceLastOccurrence 替换最后一个匹配的子字符串 |
|
func ReplaceLastOccurrence(s, oldStr, newStr string) string { |
|
lastIndex := strings.LastIndex(s, oldStr) |
|
if lastIndex == -1 { |
|
return s // 未找到,直接返回原字符串 |
|
} |
|
return s[:lastIndex] + newStr + s[lastIndex+len(oldStr):] |
|
}
|
|
|