Skip to content

Commit

Permalink
fix: fix require modules fail (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Nov 4, 2024
1 parent 8f327c5 commit b3ec120
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pkg/mod_management/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/IceWhaleTech/CasaOS-Common/codegen/mod_management"
"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/tidwall/gjson"
)

var ErrNoDataInResponse = fmt.Errorf("no data in response")
var (
ErrNoDataInResponse = fmt.Errorf("no data in response")
ErrModuleNoInStore = fmt.Errorf("module not in store")
)

type ModManagementClient struct {
Client *mod_management.ClientWithResponses
Expand Down Expand Up @@ -73,7 +77,7 @@ func (c *ModManagementClient) InstallModule(name string) error {
}

if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("failed to get installable modules: %s", resp.Status())
return fmt.Errorf("failed to install module: %s, %s", resp.Status(), string(resp.Body))
}
return nil
}
Expand All @@ -86,7 +90,7 @@ func (c *ModManagementClient) InstallModuleAsync(name string) error {
}

if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("failed to get installable modules: %s", resp.Status())
return fmt.Errorf("failed to install module: %s, %s", resp.Status(), string(resp.Body))
}
return nil
}
Expand All @@ -111,17 +115,16 @@ func RequireModule(name string, runtimePath string) error {
return err
}

err, port := gatway.GetPort()
if err != nil {
return err
}
portInt, err := strconv.Atoi(port)
if err != nil {
return err
port := 80
if err, portStr := gatway.GetPort(); err == nil {
newPort := gjson.Get(portStr, "data").Int()
if newPort != 0 && int(newPort) != port {
port = int(newPort)
}
}

client, err := NewClient(ModManagementClientOpts{
Port: &portInt,
Port: &port,
})
if err != nil {
return err
Expand All @@ -145,6 +148,9 @@ func RequireModule(name string, runtimePath string) error {
// Install module
err = client.InstallModule(name)
if err != nil {
if strings.Contains(err.Error(), "module not exist") {
return ErrModuleNoInStore
}
return err
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/mod_management/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ func TestInstallableModules(t *testing.T) {

t.Log(modules)
}

func TestInstallModule(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip()
}
err := modmanagement.RequireModule("doconverter", "/var/run/casaos")
assert.NoError(t, err)
}

func TestInstallNoExistModule(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip()
}
err := modmanagement.RequireModule("abc", "/var/run/casaos")
assert.ErrorIs(t, err, modmanagement.ErrModuleNoInStore)
}

0 comments on commit b3ec120

Please sign in to comment.