diff --git a/automation/server/cmd/commands/server.go b/automation/server/cmd/commands/server.go index 9e2481010a68f91251e9a2ab0a37ad8779829096..b9d1a8223ebd74fb067d68c7d630b3823aba9883 100644 --- a/automation/server/cmd/commands/server.go +++ b/automation/server/cmd/commands/server.go @@ -1,9 +1,10 @@ package commands import ( - "fmt" - "github.com/spf13/cobra" + "openeuler.org/PilotGo/PilotGo-plugin-automation/cmd/config/options" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service/app" ) func NewServerCommand() *cobra.Command { @@ -24,6 +25,19 @@ func NewServerCommand() *cobra.Command { return cmd } func Run() error { - fmt.Println("jinlaile") + opt, err := options.NewOptions().TryLoadFromDisk() + if err != nil { + return err + } + + manager := service.NewServiceManager( + &app.LoggerService{Conf: opt.Config.Logopts}, + &app.MySQLService{Conf: opt.Config.Mysql}, + ) + if err := manager.InitAll(); err != nil { + return err + } + defer manager.CloseAll() + return nil } diff --git a/automation/server/internal/service/interface.go b/automation/server/internal/service/interface.go new file mode 100644 index 0000000000000000000000000000000000000000..ed0f599a3fd5d413fc1f993184380fecda55b82d --- /dev/null +++ b/automation/server/internal/service/interface.go @@ -0,0 +1,9 @@ +package service + +import "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" + +type AppService interface { + Name() string + Init(ctx *global.AppContext) error + Close() error +} diff --git a/automation/server/internal/service/manager.go b/automation/server/internal/service/manager.go new file mode 100644 index 0000000000000000000000000000000000000000..fce4ac2149d4b8d23fcc45625f4100c270a94c5c --- /dev/null +++ b/automation/server/internal/service/manager.go @@ -0,0 +1,32 @@ +package service + +import ( + "fmt" + + "gitee.com/openeuler/PilotGo/sdk/logger" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" +) + +type ServiceManager struct { + services []AppService +} + +func NewServiceManager(svcs ...AppService) *ServiceManager { + return &ServiceManager{services: svcs} +} + +func (sm *ServiceManager) InitAll() error { + for _, svc := range sm.services { + if err := svc.Init(global.App); err != nil { + return fmt.Errorf("failed to init %s: %w", svc.Name(), err) + } + logger.Debug("Service %s initialized successfully", svc.Name()) + } + return nil +} + +func (sm *ServiceManager) CloseAll() { + for _, svc := range sm.services { + _ = svc.Close() + } +}