github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/strategy/updateinit.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  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  
    16  	"github.com/turingchain2020/turingchain/cmd/tools/tasks"
    17  )
    18  
    19  type updateInitStrategy struct {
    20  	strategyBasic
    21  	consRootPath    string
    22  	dappRootPath    string
    23  	storeRootPath   string
    24  	cryptoRootPath  string
    25  	mempoolRootPath string
    26  }
    27  
    28  func (up *updateInitStrategy) Run() error {
    29  	mlog.Info("Begin run turingchain update init.go.")
    30  	defer mlog.Info("Run turingchain update init.go finish.")
    31  	if err := up.initMember(); err != nil {
    32  		return err
    33  	}
    34  	return up.runImpl()
    35  }
    36  
    37  func (up *updateInitStrategy) initMember() error {
    38  	path, err := up.getParam("path")
    39  	packname, _ := up.getParam("packname")
    40  	gopath := os.Getenv("GOPATH")
    41  	if err != nil || path == "" {
    42  		if len(gopath) > 0 {
    43  			path = filepath.Join(gopath, "/src/github.com/turingchain2020/turingchain/plugin/")
    44  		}
    45  	}
    46  	if packname == "" {
    47  		packname = strings.Replace(path, gopath+"/src/", "", 1)
    48  	}
    49  	if len(path) == 0 {
    50  		return errors.New("Turingchain Plugin Not Existed")
    51  	}
    52  	up.consRootPath = fmt.Sprintf("%s/consensus/", path)
    53  	up.dappRootPath = fmt.Sprintf("%s/dapp/", path)
    54  	up.storeRootPath = fmt.Sprintf("%s/store/", path)
    55  	up.cryptoRootPath = fmt.Sprintf("%s/crypto/", path)
    56  	up.mempoolRootPath = fmt.Sprintf("%s/mempool/", path)
    57  	mkdir(up.consRootPath)
    58  	mkdir(up.dappRootPath)
    59  	mkdir(up.storeRootPath)
    60  	mkdir(up.cryptoRootPath)
    61  	mkdir(up.mempoolRootPath)
    62  	buildInit(path, packname)
    63  	return nil
    64  }
    65  
    66  func mkdir(path string) {
    67  	path += "init"
    68  	if _, err := os.Stat(path); os.IsNotExist(err) {
    69  		os.MkdirAll(path, 0755)
    70  	}
    71  }
    72  
    73  func buildInit(path string, packname string) {
    74  	if packname == "" {
    75  		return
    76  	}
    77  	path += "/init.go"
    78  	if _, err := os.Stat(path); os.IsNotExist(err) {
    79  		var data = []byte(`package plugin
    80  
    81  import (
    82  	_ "${packname}/consensus/init" //consensus init
    83  	_ "${packname}/crypto/init"    //crypto init
    84  	_ "${packname}/dapp/init"      //dapp init
    85  	_ "${packname}/store/init"     //store init
    86      _ "${packname}/mempool/init"   //mempool init
    87  )
    88  `)
    89  		data = bytes.Replace(data, []byte("${packname}"), []byte(packname), -1)
    90  		ioutil.WriteFile(path, data, 0666)
    91  	}
    92  }
    93  
    94  func (up *updateInitStrategy) runImpl() error {
    95  	var err error
    96  	task := up.buildTask()
    97  	for {
    98  		if task == nil {
    99  			break
   100  		}
   101  		err = task.Execute()
   102  		if err != nil {
   103  			mlog.Error("Execute command failed.", "error", err, "taskname", task.GetName())
   104  			break
   105  		}
   106  		task = task.Next()
   107  	}
   108  	return err
   109  }
   110  
   111  func (up *updateInitStrategy) buildTask() tasks.Task {
   112  	taskSlice := make([]tasks.Task, 0)
   113  	taskSlice = append(taskSlice,
   114  		&tasks.UpdateInitFileTask{
   115  			Folder: up.consRootPath,
   116  		},
   117  		&tasks.UpdateInitFileTask{
   118  			Folder: up.dappRootPath,
   119  		},
   120  		&tasks.UpdateInitFileTask{
   121  			Folder: up.storeRootPath,
   122  		},
   123  		&tasks.UpdateInitFileTask{
   124  			Folder: up.cryptoRootPath,
   125  		},
   126  		&tasks.UpdateInitFileTask{
   127  			Folder: up.mempoolRootPath,
   128  		},
   129  	)
   130  
   131  	task := taskSlice[0]
   132  	sliceLen := len(taskSlice)
   133  	for n := 1; n < sliceLen; n++ {
   134  		task.SetNext(taskSlice[n])
   135  		task = taskSlice[n]
   136  	}
   137  	return taskSlice[0]
   138  }