github.com/Mrs4s/go-cqhttp@v1.2.0/internal/base/flag.go (about) 1 // Package base provides base config for go-cqhttp 2 package base 3 4 import ( 5 "flag" 6 "fmt" 7 "os" 8 "time" 9 10 log "github.com/sirupsen/logrus" 11 "gopkg.in/yaml.v3" 12 13 "github.com/Mrs4s/go-cqhttp/modules/config" 14 ) 15 16 // command flags 17 var ( 18 LittleC string // config file 19 LittleD bool // daemon 20 LittleH bool // Help 21 LittleWD string // working directory 22 ) 23 24 // config file flags 25 var ( 26 Debug bool // 是否开启 debug 模式 27 RemoveReplyAt bool // 是否删除reply后的at 28 ExtraReplyData bool // 是否上报额外reply信息 29 IgnoreInvalidCQCode bool // 是否忽略无效CQ码 30 SplitURL bool // 是否分割URL 31 ForceFragmented bool // 是否启用强制分片 32 SkipMimeScan bool // 是否跳过Mime扫描 33 ConvertWebpImage bool // 是否转换Webp图片 34 ReportSelfMessage bool // 是否上报自身消息 35 UseSSOAddress bool // 是否使用服务器下发的新地址进行重连 36 LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志 37 LogColorful bool // 是否启用日志颜色 38 FastStart bool // 是否为快速启动 39 AllowTempSession bool // 是否允许发送临时会话信息 40 UpdateProtocol bool // 是否更新协议 41 SignServers []config.SignServer // 使用特定的服务器进行签名 42 IsBelow110 bool // 签名服务器版本是否低于1.1.0及以下 43 HTTPTimeout int // download 超时时间 44 SignServerTimeout int // 签名服务器超时时间 45 46 PostFormat string // 上报格式 string or array 47 Proxy string // 存储 proxy_rewrite,用于设置代理 48 PasswordHash [16]byte // 存储QQ密码哈希供登录使用 49 AccountToken []byte // 存储 AccountToken 供登录使用 50 Account *config.Account // 账户配置 51 Reconnect *config.Reconnect // 重连配置 52 LogLevel string // 日志等级 53 LogAging = time.Hour * 24 * 365 // 日志时效 54 HeartbeatInterval = time.Second * 5 // 心跳间隔 55 56 Servers []map[string]yaml.Node // 连接服务列表 57 Database map[string]yaml.Node // 数据库列表 58 ) 59 60 // Parse parse flags 61 func Parse() { 62 flag.StringVar(&LittleC, "c", "config.yml", "configuration filename") 63 flag.BoolVar(&LittleD, "d", false, "running as a daemon") 64 flag.BoolVar(&LittleH, "h", false, "this Help") 65 flag.StringVar(&LittleWD, "w", "", "cover the working directory") 66 d := flag.Bool("D", false, "debug mode") 67 flag.BoolVar(&FastStart, "faststart", false, "skip waiting 5 seconds") 68 flag.BoolVar(&UpdateProtocol, "update-protocol", false, "update protocol") 69 flag.Parse() 70 71 if *d { 72 Debug = true 73 } 74 } 75 76 // Init read config from yml file 77 func Init() { 78 conf := config.Parse(LittleC) 79 { // bool config 80 if conf.Output.Debug { 81 Debug = true 82 } 83 IgnoreInvalidCQCode = conf.Message.IgnoreInvalidCQCode 84 SplitURL = conf.Message.FixURL 85 RemoveReplyAt = conf.Message.RemoveReplyAt 86 ExtraReplyData = conf.Message.ExtraReplyData 87 ForceFragmented = conf.Message.ForceFragment 88 SkipMimeScan = conf.Message.SkipMimeScan 89 ConvertWebpImage = conf.Message.ConvertWebpImage 90 ReportSelfMessage = conf.Message.ReportSelfMessage 91 UseSSOAddress = conf.Account.UseSSOAddress 92 AllowTempSession = conf.Account.AllowTempSession 93 SignServers = conf.Account.SignServers 94 IsBelow110 = conf.Account.IsBelow110 95 HTTPTimeout = conf.Message.HTTPTimeout 96 SignServerTimeout = int(conf.Account.SignServerTimeout) 97 } 98 { // others 99 Proxy = conf.Message.ProxyRewrite 100 Account = conf.Account 101 Reconnect = conf.Account.ReLogin 102 Servers = conf.Servers 103 Database = conf.Database 104 LogLevel = conf.Output.LogLevel 105 LogColorful = conf.Output.LogColorful == nil || *conf.Output.LogColorful 106 if conf.Message.PostFormat != "string" && conf.Message.PostFormat != "array" { 107 log.Warnf("post-format 配置错误, 将自动使用 string") 108 PostFormat = "string" 109 } else { 110 PostFormat = conf.Message.PostFormat 111 } 112 if conf.Output.LogAging > 0 { 113 LogAging = time.Hour * 24 * time.Duration(conf.Output.LogAging) 114 } 115 if conf.Heartbeat.Interval > 0 { 116 HeartbeatInterval = time.Second * time.Duration(conf.Heartbeat.Interval) 117 } 118 if conf.Heartbeat.Disabled || conf.Heartbeat.Interval < 0 { 119 HeartbeatInterval = 0 120 } 121 } 122 } 123 124 // Help cli命令行-h的帮助提示 125 func Help() { 126 fmt.Printf(`go-cqhttp service 127 version: %s 128 Usage: 129 server [OPTIONS] 130 Options: 131 `, Version) 132 133 flag.PrintDefaults() 134 os.Exit(0) 135 }