From b117c191f46eec157d4f60dfc7a816768e2a8dab Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Mon, 7 Oct 2024 00:16:46 -0400 Subject: [PATCH] corectd installplugin params --- blockchain/bl_plugins.go | 25 ++++++++++++++++--------- blockchain/interface.go | 3 ++- mobile/blockchain.go | 9 +++++++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/blockchain/bl_plugins.go b/blockchain/bl_plugins.go index 560d2be..c604c47 100644 --- a/blockchain/bl_plugins.go +++ b/blockchain/bl_plugins.go @@ -80,7 +80,7 @@ func (bl *FxBlockchain) ListPlugins(ctx context.Context) ([]byte, error) { return json.Marshal(detailedPlugins) } -func (bl *FxBlockchain) InstallPlugin(ctx context.Context, pluginName string, params []PluginParam) ([]byte, error) { +func (bl *FxBlockchain) InstallPlugin(ctx context.Context, pluginName string, paramsString string) ([]byte, error) { // Read existing plugins plugins, err := bl.readActivePlugins() if err != nil { @@ -94,10 +94,18 @@ func (bl *FxBlockchain) InstallPlugin(ctx context.Context, pluginName string, pa } } - // Process parameters - if len(params) > 0 { + // Process parameters param1====value1,,,,param2====value2 + if paramsString != "" { + params := strings.Split(paramsString, ",,,,") for _, param := range params { - filePath := fmt.Sprintf("/internal/%s/%s.txt", pluginName, param.Name) + parts := strings.SplitN(param, "=====", 2) + if len(parts) != 2 { + return nil, fmt.Errorf("invalid parameter format: %s", param) + } + name := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + + filePath := fmt.Sprintf("/internal/%s/%s.txt", pluginName, name) dirPath := fmt.Sprintf("/internal/%s", pluginName) // Create directory if it doesn't exist @@ -106,8 +114,7 @@ func (bl *FxBlockchain) InstallPlugin(ctx context.Context, pluginName string, pa } // Write parameter value to file - content := strings.TrimSpace(param.Value) - if err := os.WriteFile(filePath, []byte(content), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(value), 0644); err != nil { return nil, fmt.Errorf("failed to write parameter file for plugin %s: %w", pluginName, err) } } @@ -219,9 +226,9 @@ func (bl *FxBlockchain) showPluginStatusImpl(ctx context.Context, pluginName str func (bl *FxBlockchain) handlePluginAction(ctx context.Context, from peer.ID, w http.ResponseWriter, r *http.Request, action string) { var req struct { - PluginName string `json:"plugin_name"` - Lines int `json:"lines,omitempty"` - Params []PluginParam `json:"params,omitempty"` + PluginName string `json:"plugin_name"` + Lines int `json:"lines,omitempty"` + Params string `json:"params,omitempty"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { diff --git a/blockchain/interface.go b/blockchain/interface.go index 43129fe..39db8a2 100644 --- a/blockchain/interface.go +++ b/blockchain/interface.go @@ -404,6 +404,7 @@ type ListPluginsResponse struct { // InstallPlugin type InstallPluginRequest struct { PluginName string `json:"plugin_name"` + Params string `json:"params"` } type InstallPluginResponse struct { @@ -476,7 +477,7 @@ type Blockchain interface { //Plugins ListPlugins(context.Context) ([]byte, error) - InstallPlugin(context.Context, string) ([]byte, error) + InstallPlugin(context.Context, string, string) ([]byte, error) UninstallPlugin(context.Context, string) ([]byte, error) ShowPluginStatus(context.Context, string, int) ([]byte, error) } diff --git a/mobile/blockchain.go b/mobile/blockchain.go index a2aff46..a9ea484 100644 --- a/mobile/blockchain.go +++ b/mobile/blockchain.go @@ -12,6 +12,11 @@ import ( wifi "github.com/functionland/go-fula/wap/pkg/wifi" ) +type PluginParam struct { + Name string `json:"name"` + Value string `json:"value"` +} + // AccountExists requests blox at Config.BloxAddr to check if the account exists or not. // the addr must be a valid multiaddr that includes peer ID. func (c *Client) AccountExists(account string) ([]byte, error) { @@ -277,9 +282,9 @@ func (c *Client) ListPlugins() ([]byte, error) { } // InstallPlugin requests the blox to install a specific plugin -func (c *Client) InstallPlugin(pluginName string) ([]byte, error) { +func (c *Client) InstallPlugin(pluginName string, params string) ([]byte, error) { ctx := context.TODO() - return c.bl.InstallPlugin(ctx, pluginName) + return c.bl.InstallPlugin(ctx, pluginName, params) } // UninstallPlugin requests the blox to uninstall a specific plugin