Skip to content

Commit

Permalink
corectd installplugin params
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Oct 7, 2024
1 parent 52a21d1 commit b117c19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
25 changes: 16 additions & 9 deletions blockchain/bl_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ type ListPluginsResponse struct {
// InstallPlugin
type InstallPluginRequest struct {
PluginName string `json:"plugin_name"`
Params string `json:"params"`
}

type InstallPluginResponse struct {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b117c19

Please sign in to comment.