dreamfactory_cmd/cmd/cmd.go
2023-06-09 21:58:02 +08:00

89 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"go_dreamfactory/cmd/robot"
"go_dreamfactory/pb"
"os"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/spf13/cobra"
)
func emptyRun(*cobra.Command, []string) {}
var RootCmd = &cobra.Command{
Use: "robot",
Short: "命令行",
Long: "命令行工具",
Run: emptyRun,
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stdin, err)
os.Exit(1)
}
}
func init() {
RootCmd.AddCommand(runCmd)
initLog()
robot.InitDb()
}
var account = flag.String("account", "", "登录账号")
var sid = flag.String("sid", "0", "区服ID")
var create = flag.Bool("create", false, "是否创建新账号") //false 不创建新账号
var role = flag.Bool("role", false, "创角")
func CloneNewHero(hero *pb.DBHero) (newHero *pb.DBHero) {
newHero = &*hero
newHero.Id = primitive.NewObjectID().Hex()
return
}
func main() {
Execute()
}
var runCmd = &cobra.Command{
Use: "run",
Short: "启动",
Run: func(cmd *cobra.Command, args []string) {
opts := robot.DefaultOpts()
opts.Create = *create
opts.Account = *account
opts.ServerId = *sid
opts.Role = *role
r := robot.NewRobot(opts)
r.Run()
},
}
func initLog() {
logrus.New()
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: false,
FullTimestamp: false,
})
//设置output,默认为stderr,可以为任何io.Writer比如文件*os.File
// file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
// writers := []io.Writer{
// file,
// os.Stdout}
// //同时写文件和屏幕
// fileAndStdoutWriter := io.MultiWriter(writers...)
// if err == nil {
// logrus.SetOutput(fileAndStdoutWriter)
// } else {
// logrus.Info("failed to log to file.")
// }
}