Skip to content

Commit

Permalink
feat: polish mod sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Oct 31, 2024
1 parent f452937 commit 1b83b55
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/mod_management/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit 1b83b55

Please sign in to comment.