From 1b83b554338f3b582ac4540652866ed387fcaebc Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Thu, 31 Oct 2024 16:22:19 +0800 Subject: [PATCH] feat: polish mod sdk --- pkg/mod_management/sdk.go | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/pkg/mod_management/sdk.go b/pkg/mod_management/sdk.go index 20e849b..f39663e 100644 --- a/pkg/mod_management/sdk.go +++ b/pkg/mod_management/sdk.go @@ -4,8 +4,10 @@ import ( "context" "fmt" "net/http" + "strconv" "github.com/IceWhaleTech/CasaOS-Common/codegen/mod_management" + "github.com/IceWhaleTech/CasaOS-Common/external" ) var ErrNoDataInResponse = fmt.Errorf("no data in response") @@ -102,3 +104,49 @@ func (c *ModManagementClient) UninstallModule(name string) error { } return nil } + +func RequireModule(name string, runtimePath string) error { + gatway, err := external.NewManagementService(runtimePath) + if err != nil { + return err + } + + err, port := gatway.GetPort() + if err != nil { + return err + } + portInt, err := strconv.Atoi(port) + if err != nil { + return err + } + + client, err := NewClient(ModManagementClientOpts{ + Port: &portInt, + }) + if err != nil { + return err + } + + modules, err := client.InstalledModules() + if err != nil { + return err + } + + // 判断是否已经安装 + for _, module := range modules { + if module.Name == nil { + continue + } + if *module.Name == name { + return nil + } + } + + // 安装 + err = client.InstallModule(name) + if err != nil { + return err + } + + return nil +}