github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/tasks/format_dappsource_task.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 tasks
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path"
    11  	"path/filepath"
    12  )
    13  
    14  // FormatDappSourceTask 利用Go工具,对生成出来的Go源码进行格式化
    15  type FormatDappSourceTask struct {
    16  	TaskBase
    17  	OutputFolder string
    18  }
    19  
    20  //GetName 获取name
    21  func (f *FormatDappSourceTask) GetName() string {
    22  	return "FormatDappSourceTask"
    23  }
    24  
    25  //Execute 执行
    26  func (f *FormatDappSourceTask) Execute() error {
    27  	mlog.Info("Execute format dapp source task.")
    28  	err := filepath.Walk(f.OutputFolder, func(fpath string, info os.FileInfo, err error) error {
    29  		if info == nil {
    30  			return err
    31  		}
    32  		if info.IsDir() {
    33  			return nil
    34  		}
    35  		ext := path.Ext(fpath)
    36  		if ext != ".go" { // 仅对go的源码文件进行格式化
    37  			return nil
    38  		}
    39  		cmd := exec.Command("gofmt", "-l", "-s", "-w", fpath)
    40  		cmd.Stdout = os.Stdout
    41  		cmd.Stderr = os.Stderr
    42  		return cmd.Run()
    43  	})
    44  	return err
    45  }