github.com/turingchain2020/turingchain@v1.1.21/pluginmgr/manager.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pluginmgr
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/turingchain2020/turingchain/rpc/types"
    11  	typ "github.com/turingchain2020/turingchain/types"
    12  	wcom "github.com/turingchain2020/turingchain/wallet/common"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var pluginItems = make(map[string]Plugin)
    17  
    18  var once = &sync.Once{}
    19  
    20  // InitExec init exec
    21  func InitExec(cfg *typ.TuringchainConfig) {
    22  	once.Do(func() {
    23  		for _, item := range pluginItems {
    24  			item.InitExec(cfg)
    25  		}
    26  	})
    27  }
    28  
    29  //GetExecList 获取插件名字列表
    30  func GetExecList() (datas []string) {
    31  	for _, plugin := range pluginItems {
    32  		datas = append(datas, plugin.GetExecutorName())
    33  	}
    34  	return
    35  }
    36  
    37  // InitWallet init wallet plugin
    38  func InitWallet(wallet wcom.WalletOperate, sub map[string][]byte) {
    39  	once.Do(func() {
    40  		for _, item := range pluginItems {
    41  			item.InitWallet(wallet, sub)
    42  		}
    43  	})
    44  }
    45  
    46  // HasExec check is have the name exec
    47  func HasExec(name string) bool {
    48  	for _, item := range pluginItems {
    49  		if item.GetExecutorName() == name {
    50  			return true
    51  		}
    52  	}
    53  	return false
    54  }
    55  
    56  // Register Register plugin
    57  func Register(p Plugin) {
    58  	if p == nil {
    59  		panic("plugin param is nil")
    60  	}
    61  	packageName := p.GetName()
    62  	if len(packageName) == 0 {
    63  		panic("plugin package name is empty")
    64  	}
    65  	if _, ok := pluginItems[packageName]; ok {
    66  		panic("execute plugin item is existed. name = " + packageName)
    67  	}
    68  	pluginItems[packageName] = p
    69  }
    70  
    71  // AddCmd add Command for plugin
    72  func AddCmd(rootCmd *cobra.Command) {
    73  	for _, item := range pluginItems {
    74  		item.AddCmd(rootCmd)
    75  	}
    76  }
    77  
    78  // AddRPC add Rpc
    79  func AddRPC(s types.RPCServer) {
    80  	for _, item := range pluginItems {
    81  		item.AddRPC(s)
    82  	}
    83  }