diff --git a/cmd/ratte/run.go b/cmd/ratte/run.go index 7bb646a..b56b832 100644 --- a/cmd/ratte/run.go +++ b/cmd/ratte/run.go @@ -50,14 +50,14 @@ func runHandle(_ *cobra.Command, _ []string) { log.WithField("path", config).Info("Loaded.") log.Info("Init core plugin...") - err = startCores(c.Core) + err = startCores(c.Plugin.Core, c.Core) if err != nil { log.WithError(err).Fatal("Init core plugin failed") } log.Info("Done.") log.Info("Init panel plugin...") - err = startPanel(c.Panel) + err = startPanel(c.Plugin.Panel) if err != nil { log.WithError(err).Fatal("Init panel plugin failed") } @@ -106,11 +106,11 @@ func runHandle(_ *cobra.Command, _ []string) { if err != nil { log.WithError(err).Fatal("Close panel failed") } - err = startCores(c.Core) + err = startCores(c.Plugin.Core, c.Core) if err != nil { log.WithError(err).Fatal("Start core failed") } - err = startPanel(c.Panel) + err = startPanel(c.Plugin.Panel) if err != nil { log.WithError(err).Fatal("Start panel failed") } @@ -146,19 +146,25 @@ func runHandle(_ *cobra.Command, _ []string) { log.Info("Done.") } -func startCores(coresP []conf.Core) error { +func startCores(coresP []conf.Plugin, cc []conf.Core) error { // new cores cores = make(map[string]*core.PluginClient, len(coresP)) - for _, co := range coresP { - c, err := core.NewClient(nil, exec.Command(co.Path)) - if err != nil { - return fmt.Errorf("new core error: %w", err) + for _, ccV := range cc { + for _, co := range coresP { + if co.Name != ccV.Type { + continue + } + c, err := core.NewClient(nil, exec.Command(co.Path)) + if err != nil { + return fmt.Errorf("new core error: %w", err) + } + err = c.Start(ccV.DataPath, ccV.Config) + if err != nil { + return fmt.Errorf("start core error: %w", err) + } + cores[co.Name] = c + break } - err = c.Start(co.DataPath, co.Config) - if err != nil { - return fmt.Errorf("start core error: %w", err) - } - cores[co.Name] = c } return nil } @@ -173,7 +179,7 @@ func closeCore() error { return nil } -func startPanel(panelsP []conf.Panel) error { +func startPanel(panelsP []conf.Plugin) error { panels = make(map[string]*panel.PluginClient, len(panelsP)) for _, p := range panelsP { pn, err := panel.NewClient(nil, exec.Command(p.Path)) diff --git a/conf/conf.go b/conf/conf.go index 7e00e29..5fb2226 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -21,9 +21,9 @@ type Conf struct { // config fields Log Log `json:"Log,omitempty"` Watcher Watcher `json:"Watcher,omitempty"` + Plugin Plugins `json:"Plugin,omitempty"` // Only accept from file path Core []Core `json:"Core,omitempty"` Acme []ACME `json:"Acme,omitempty"` - Panel []Panel `json:"Panel,omitempty"` Node []Node `json:"Node,omitempty"` } @@ -34,11 +34,11 @@ func New(path string) *Conf { WatchLocalConfig: true, WatchRemoteConfig: true, }, - Log: newLog(), - Core: make([]Core, 0), - Acme: make([]ACME, 0), - Panel: make([]Panel, 0), - Node: make([]Node, 0), + Log: newLog(), + Core: make([]Core, 0), + Acme: make([]ACME, 0), + Plugin: Plugins{}, + Node: make([]Node, 0), } } diff --git a/conf/core.go b/conf/core.go deleted file mode 100644 index 31e0ba4..0000000 --- a/conf/core.go +++ /dev/null @@ -1,25 +0,0 @@ -package conf - -import ( - "fmt" - "github.com/goccy/go-json" -) - -type Core struct { - Name string `json:"Name,omitempty"` - Path string `json:"Path,omitempty"` - DataPath string `json:"DataPath,omitempty"` - Config json.RawMessage `json:"Config,omitempty"` -} -type _core Core - -func (c *Core) UnmarshalJSON(data []byte) error { - err := json.Unmarshal(data, (*_core)(c)) - if err != nil { - return fmt.Errorf("failed to unmarshal core: %v", err) - } - if len(c.Config) == 0 { - c.Config = data - } - return nil -} diff --git a/conf/panel.go b/conf/panel.go deleted file mode 100644 index 450a04f..0000000 --- a/conf/panel.go +++ /dev/null @@ -1,6 +0,0 @@ -package conf - -type Panel struct { - Name string `json:"name"` - Path string `json:"path"` -} diff --git a/conf/plugin.go b/conf/plugin.go new file mode 100644 index 0000000..388877e --- /dev/null +++ b/conf/plugin.go @@ -0,0 +1,49 @@ +package conf + +import ( + "fmt" + "github.com/goccy/go-json" +) + +type Core struct { + Type string `json:"type"` + DataPath string `json:"DataPath,omitempty"` + Config json.RawMessage `json:"Config,omitempty"` +} +type _core Core + +func (c *Core) UnmarshalJSON(data []byte) error { + err := json.Unmarshal(data, (*_core)(c)) + if err != nil { + return fmt.Errorf("failed to unmarshal core: %v", err) + } + if len(c.Config) == 0 { + c.Config = data + } + return nil +} + +type Plugins struct { + Core []Plugin `json:"core"` + Panel []Plugin `json:"panel"` +} + +type Plugin struct { + Name string `json:"Name,omitempty"` + Path string `json:"Path,omitempty"` +} + +type _plugins Plugins + +func (p *Plugins) UnmarshalJSON(data []byte) error { + var path string + err := json.Unmarshal(data, path) + if err != nil { + return err + } + err = json.Unmarshal(data, (*_plugins)(p)) + if err != nil { + return fmt.Errorf("failed to unmarshal plugin: %v", err) + } + return nil +}