github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/strategy/strategy.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 strategy 实现开发者工具实现不同策略的功能
     6  package strategy
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/turingchain2020/turingchain/cmd/tools/types"
    12  	"github.com/turingchain2020/turingchain/common/log/log15"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  var (
    17  	mlog = log15.New("module", "strategy")
    18  )
    19  
    20  //Strategy 策略
    21  type Strategy interface {
    22  	SetParam(key string, value string)
    23  	Run() error
    24  }
    25  
    26  //New new
    27  func New(name string) Strategy {
    28  	switch name {
    29  	case types.KeyImportPackage:
    30  		return &importPackageStrategy{
    31  			strategyBasic: strategyBasic{
    32  				params: make(map[string]string),
    33  			},
    34  		}
    35  	case types.KeyUpdateInit:
    36  		return &updateInitStrategy{
    37  			strategyBasic: strategyBasic{
    38  				params: make(map[string]string),
    39  			},
    40  		}
    41  	case types.KeyCreatePlugin:
    42  		return &createPluginStrategy{
    43  			strategyBasic: strategyBasic{
    44  				params: make(map[string]string),
    45  			},
    46  		}
    47  	case types.KeyGenDapp:
    48  		return &genDappStrategy{
    49  			strategyBasic: strategyBasic{
    50  				params: make(map[string]string),
    51  			},
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  type strategyBasic struct {
    58  	params map[string]string
    59  }
    60  
    61  //SetParam 设置参数
    62  func (s *strategyBasic) SetParam(key string, value string) {
    63  	s.params[key] = value
    64  }
    65  
    66  func (s *strategyBasic) getParam(key string) (string, error) {
    67  	if v, ok := s.params[key]; ok {
    68  		return v, nil
    69  	}
    70  	return "", fmt.Errorf("Key:%v not exist", key)
    71  }
    72  
    73  //Run 运行
    74  func (s *strategyBasic) Run() error {
    75  	return errors.New("NotSupport")
    76  }