1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

style: move internal/config to modules/config

config should not be internal, maybe some module need it.
This commit is contained in:
wdvxdr 2021-09-26 19:41:03 +08:00
parent ddd51e6ca3
commit 1337d3f1f3
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
9 changed files with 101 additions and 121 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
// CQBot CQBot结构体,存储Bot实例相关配置 // CQBot CQBot结构体,存储Bot实例相关配置

View File

@ -3,12 +3,14 @@ package base
import ( import (
"flag" "flag"
"os"
"path"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
// command flags // command flags
@ -49,14 +51,15 @@ var (
// Parse parse flags // Parse parse flags
func Parse() { func Parse() {
flag.StringVar(&LittleC, "c", config.DefaultConfigFile, "configuration filename") wd, _ := os.Getwd()
dc := path.Join(wd, "config.yml")
flag.StringVar(&LittleC, "c", dc, "configuration filename")
flag.BoolVar(&LittleD, "d", false, "running as a daemon") flag.BoolVar(&LittleD, "d", false, "running as a daemon")
flag.BoolVar(&LittleH, "h", false, "this help") flag.BoolVar(&LittleH, "h", false, "this help")
flag.StringVar(&LittleWD, "w", "", "cover the working directory") flag.StringVar(&LittleWD, "w", "", "cover the working directory")
d := flag.Bool("D", false, "debug mode") d := flag.Bool("D", false, "debug mode")
flag.Parse() flag.Parse()
config.DefaultConfigFile = LittleC // cover config file
if *d { if *d {
Debug = true Debug = true
} }
@ -64,7 +67,7 @@ func Parse() {
// Init read config from yml file // Init read config from yml file
func Init() { func Init() {
conf := config.Get() conf := config.Parse(LittleC)
{ // bool config { // bool config
if conf.Output.Debug { if conf.Output.Debug {
Debug = true Debug = true

View File

@ -30,8 +30,8 @@ import (
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/terminal" "github.com/Mrs4s/go-cqhttp/global/terminal"
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/config"
"github.com/Mrs4s/go-cqhttp/internal/selfupdate" "github.com/Mrs4s/go-cqhttp/internal/selfupdate"
"github.com/Mrs4s/go-cqhttp/modules/config"
"github.com/Mrs4s/go-cqhttp/server" "github.com/Mrs4s/go-cqhttp/server"
) )

View File

@ -6,10 +6,8 @@ import (
_ "embed" // embed the default config file _ "embed" // embed the default config file
"fmt" "fmt"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/Mrs4s/go-cqhttp/internal/param" "github.com/Mrs4s/go-cqhttp/internal/param"
@ -21,11 +19,6 @@ import (
//go:embed default_config.yml //go:embed default_config.yml
var defaultConfig string var defaultConfig string
var currentPath = getCurrentPath()
// DefaultConfigFile 默认配置文件路径
var DefaultConfigFile = path.Join(currentPath, "config.yml")
// Reconnect 重连配置 // Reconnect 重连配置
type Reconnect struct { type Reconnect struct {
Disabled bool `yaml:"disabled"` Disabled bool `yaml:"disabled"`
@ -144,28 +137,22 @@ type LevelDBConfig struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
} }
var ( // Parse 从默认配置文件路径中获取
config *Config func Parse(path string) *Config {
once sync.Once fromEnv := os.Getenv("GCQ_UIN") != ""
)
// Get 从默认配置文件路径中获取 file, err := os.Open(path)
func Get() *Config { config := &Config{}
once.Do(func() {
hasEnvironmentConf := os.Getenv("GCQ_UIN") != ""
file, err := os.Open(DefaultConfigFile)
config = &Config{}
if err == nil { if err == nil {
defer func() { _ = file.Close() }() defer func() { _ = file.Close() }()
if err = yaml.NewDecoder(file).Decode(config); err != nil && !hasEnvironmentConf { if err = yaml.NewDecoder(file).Decode(config); err != nil && !fromEnv {
log.Fatal("配置文件不合法!", err) log.Fatal("配置文件不合法!", err)
} }
} else if !hasEnvironmentConf { } else if !fromEnv {
generateConfig() generateConfig()
os.Exit(0) os.Exit(0)
} }
if hasEnvironmentConf { if fromEnv {
// type convert tools // type convert tools
toInt64 := func(str string) int64 { toInt64 := func(str string) int64 {
i, _ := strconv.ParseInt(str, 10, 64) i, _ := strconv.ParseInt(str, 10, 64)
@ -240,19 +227,9 @@ func Get() *Config {
config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node}) config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node})
} }
} }
})
return config return config
} }
// getCurrentPath 获取当前文件的路径直接返回string
func getCurrentPath() string {
cwd, e := os.Getwd()
if e != nil {
panic(e)
}
return cwd
}
// generateConfig 生成配置文件 // generateConfig 生成配置文件
func generateConfig() { func generateConfig() {
fmt.Println("未找到配置文件,正在为您生成配置文件中!") fmt.Println("未找到配置文件,正在为您生成配置文件中!")

View File

@ -21,7 +21,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
type httpServer struct { type httpServer struct {

View File

@ -9,7 +9,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
// RunPprofServer 启动 pprof 性能分析服务器 // RunPprofServer 启动 pprof 性能分析服务器

View File

@ -16,7 +16,7 @@ import (
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
type lambdaClient struct { type lambdaClient struct {

View File

@ -12,7 +12,7 @@ import (
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/config" "github.com/Mrs4s/go-cqhttp/modules/config"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"