mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
fix: test case issue
This commit is contained in:
@@ -2,9 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/crawlab-team/crawlab/core/cmd"
|
||||
"github.com/crawlab-team/crawlab/core/config"
|
||||
"github.com/crawlab-team/crawlab/core/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
config.InitConfig()
|
||||
}
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
err := cmd.Execute()
|
||||
|
||||
@@ -3,45 +3,18 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// config instance
|
||||
c := Config{Name: ""}
|
||||
|
||||
// init config file
|
||||
if err := c.Init(); err != nil {
|
||||
log.Warn("unable to init config")
|
||||
return
|
||||
}
|
||||
|
||||
// watch config change and load responsively
|
||||
c.WatchConfig()
|
||||
|
||||
// init log level
|
||||
c.initLogLevel()
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type InitConfigOptions struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c *Config) WatchConfig() {
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
log.Infof("Config file changed: %s", e.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Config) Init() (err error) {
|
||||
func (c *Config) Init() {
|
||||
// Set default values
|
||||
c.setDefaults()
|
||||
|
||||
@@ -74,7 +47,15 @@ func (c *Config) Init() (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
// init log level
|
||||
c.initLogLevel()
|
||||
}
|
||||
|
||||
func (c *Config) WatchConfig() {
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
log.Infof("Config file changed: %s", e.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Config) setDefaults() {
|
||||
@@ -95,3 +76,26 @@ func (c *Config) initLogLevel() {
|
||||
}
|
||||
log.SetLevel(l)
|
||||
}
|
||||
|
||||
func newConfig() *Config {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
var _config *Config
|
||||
var _configOnce sync.Once
|
||||
|
||||
func GetConfig() *Config {
|
||||
_configOnce.Do(func() {
|
||||
_config = newConfig()
|
||||
_config.Init()
|
||||
})
|
||||
return _config
|
||||
}
|
||||
|
||||
func InitConfig() {
|
||||
// config instance
|
||||
c := GetConfig()
|
||||
|
||||
// watch config change and load responsively
|
||||
c.WatchConfig()
|
||||
}
|
||||
|
||||
@@ -20,11 +20,7 @@ func init() {
|
||||
|
||||
func TestInitConfig(t *testing.T) {
|
||||
// Create a new Config instance
|
||||
c := Config{Name: ""}
|
||||
|
||||
// Initialize the config
|
||||
err := c.Init()
|
||||
require.NoError(t, err, "Failed to initialize config")
|
||||
InitConfig()
|
||||
|
||||
// Test default values
|
||||
assert.Equal(t, "localhost", viper.GetString("mongo.host"), "Unexpected default value for mongo.host")
|
||||
@@ -58,8 +54,7 @@ server:
|
||||
|
||||
// Create a new Config instance with the config file
|
||||
cWithFile := Config{Name: configPath}
|
||||
err = cWithFile.Init()
|
||||
require.NoError(t, err, "Failed to initialize config with file")
|
||||
cWithFile.Init()
|
||||
|
||||
// Test values from config file
|
||||
assert.Equal(t, "global.edition.pro", viper.GetString("edition"), "Unexpected value for edition from config file")
|
||||
|
||||
@@ -457,8 +457,6 @@ func newTaskHandlerService() *Service {
|
||||
// grpc client
|
||||
svc.c = grpcclient.GetGrpcClient()
|
||||
|
||||
log.Debugf("[NewTaskHandlerService] svc[cfgPath: %s]", svc.cfgSvc.GetConfigPath())
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
|
||||
@@ -81,10 +81,15 @@ func IsPro() bool {
|
||||
}
|
||||
|
||||
func GetWorkspace() string {
|
||||
homedirPath, err := homedir.Dir()
|
||||
if err != nil {
|
||||
log.Warnf("cannot find home directory: %v", err)
|
||||
return DefaultWorkspace
|
||||
}
|
||||
if res := viper.GetString("workspace"); res != "" {
|
||||
return res
|
||||
}
|
||||
return DefaultWorkspace
|
||||
return filepath.Join(homedirPath, DefaultWorkspace)
|
||||
}
|
||||
|
||||
func GetTaskLogPath() string {
|
||||
|
||||
Reference in New Issue
Block a user