github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/contract/contractprepare.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package contract
     7  
     8  import (
     9  	"fmt"
    10  	"os/exec"
    11  
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  	"github.com/iotexproject/iotex-core/ioctl/util"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_prepareCmdShorts = map[config.Language]string{
    22  		config.English: "Prepare solidity compiler",
    23  		config.Chinese: "准备solidity编译器",
    24  	}
    25  )
    26  
    27  // ContractPrepareCmd represents the contract prepare command
    28  var ContractPrepareCmd = &cobra.Command{
    29  	Use:   "prepare",
    30  	Short: config.TranslateInLang(_prepareCmdShorts, config.UILanguage),
    31  	Args:  cobra.ExactArgs(0),
    32  	RunE: func(cmd *cobra.Command, args []string) error {
    33  		cmd.SilenceUsage = true
    34  		err := prepare()
    35  		return err
    36  	},
    37  }
    38  
    39  func prepare() error {
    40  	_, err := util.SolidityVersion(_solCompiler)
    41  	if err != nil {
    42  		cmdString := "curl --silent https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-solc.sh | sh"
    43  		cmd := exec.Command("bash", "-c", cmdString)
    44  		output.PrintResult("Preparing solidity compiler ...\n")
    45  
    46  		err = cmd.Run()
    47  		if err != nil {
    48  			return output.NewError(output.UpdateError, "failed to prepare solc", err)
    49  		}
    50  	}
    51  	solc, err := util.SolidityVersion(_solCompiler)
    52  	if err != nil {
    53  		return output.NewError(output.CompilerError, "solidity compiler not ready", err)
    54  	}
    55  	if !checkCompilerVersion(solc) {
    56  		return output.NewError(output.CompilerError,
    57  			fmt.Sprintf("unsupported solc version %d.%d.%d, expects solc version 0.5.17",
    58  				solc.Major, solc.Minor, solc.Patch), nil)
    59  	}
    60  
    61  	output.PrintResult("Solidity compiler is ready now.")
    62  	return nil
    63  }