Use a better method to reload command presets

This commit is contained in:
NI
2020-03-12 21:18:08 +08:00
parent b2210ce2b6
commit 1009cd4284
12 changed files with 206 additions and 374 deletions

View File

@@ -21,11 +21,8 @@ import (
"github.com/niruix/sshwifty/application/log"
)
// Reconfigurator reloads configuration
type Reconfigurator func(p Configuration) Configuration
// PresetReloader reloads preset
type PresetReloader func(p Preset) (Preset, error)
// Loader Configuration loader
type Loader func(
log log.Logger,
r Reconfigurator,
) (name string, cfg Configuration, err error)
type Loader func(log log.Logger) (name string, cfg Configuration, err error)

View File

@@ -28,10 +28,7 @@ const (
// Direct creates a loader that return raw configuration data directly.
// Good for integration.
func Direct(cfg Configuration) Loader {
return func(
log log.Logger,
r Reconfigurator,
) (string, Configuration, error) {
return func(log log.Logger) (string, Configuration, error) {
return directTypeName, cfg, nil
}
}

View File

@@ -44,10 +44,7 @@ func parseEviro(name string) string {
// Enviro creates an environment variable based configuration loader
func Enviro() Loader {
return func(
log log.Logger,
r Reconfigurator,
) (string, Configuration, error) {
return func(log log.Logger) (string, Configuration, error) {
log.Info("Loading configuration from environment variables ...")
dialTimeout, _ := strconv.ParseUint(
@@ -123,7 +120,7 @@ func Enviro() Loader {
}
}
return enviroTypeName, r(Configuration{
return enviroTypeName, Configuration{
HostName: cfg.HostName,
SharedKey: cfg.SharedKey,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
@@ -133,6 +130,6 @@ func Enviro() Loader {
Servers: []Server{cfgSer.build()},
Presets: presets,
OnlyAllowPresetRemotes: cfg.OnlyAllowPresetRemotes,
}), nil
}, nil
}
}

View File

@@ -135,10 +135,7 @@ func (f fileCfgCommon) build() (fileCfgCommon, error) {
}, nil
}
func loadFile(
filePath string,
r Reconfigurator,
) (string, Configuration, error) {
func loadFile(filePath string) (string, Configuration, error) {
f, fErr := os.Open(filePath)
if fErr != nil {
@@ -174,7 +171,7 @@ func loadFile(
presets[i] = finalCfg.Presets[i].build()
}
return fileTypeName, r(Configuration{
return fileTypeName, Configuration{
HostName: finalCfg.HostName,
SharedKey: finalCfg.SharedKey,
DialTimeout: time.Duration(finalCfg.DialTimeout) *
@@ -185,19 +182,16 @@ func loadFile(
Servers: servers,
Presets: presets,
OnlyAllowPresetRemotes: cfg.OnlyAllowPresetRemotes,
}), nil
}, nil
}
// File creates a configuration file loader
func File(customPath string) Loader {
return func(
log log.Logger,
r Reconfigurator,
) (string, Configuration, error) {
return func(log log.Logger) (string, Configuration, error) {
if len(customPath) > 0 {
log.Info("Loading configuration from: %s", customPath)
return loadFile(customPath, r)
return loadFile(customPath)
}
log.Info("Loading configuration from one of the default " +
@@ -239,7 +233,7 @@ func File(customPath string) Loader {
log.Info("Configuration file \"%s\" has been selected",
fallbackFileSearchList[f])
return loadFile(fallbackFileSearchList[f], r)
return loadFile(fallbackFileSearchList[f])
}
return fileTypeName, Configuration{}, fmt.Errorf(

View File

@@ -30,14 +30,11 @@ const (
// Redundant creates a group of loaders. They will be executed one by one until
// one of it successfully returned a configuration
func Redundant(loaders ...Loader) Loader {
return func(
log log.Logger,
r Reconfigurator,
) (string, Configuration, error) {
return func(log log.Logger) (string, Configuration, error) {
ll := log.Context("Redundant")
for i := range loaders {
lLoaderName, lCfg, lErr := loaders[i](ll, r)
lLoaderName, lCfg, lErr := loaders[i](ll)
if lErr != nil {
ll.Warning("Unable to load configuration from \"%s\": %s",