github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/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  	"os/exec"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    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  // NewContractPrepareCmd represents the contract prepare command
    28  func NewContractPrepareCmd(client ioctl.Client) *cobra.Command {
    29  	short, _ := client.SelectTranslation(_prepareCmdShorts)
    30  
    31  	return &cobra.Command{
    32  		Use:   "prepare",
    33  		Short: short,
    34  		Args:  cobra.ExactArgs(0),
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			cmd.SilenceUsage = true
    37  			_, err := util.SolidityVersion(_solCompiler)
    38  			if err != nil {
    39  				cmdString := "curl --silent https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-solc.sh | sh"
    40  				installCmd := exec.Command("bash", "-c", cmdString)
    41  				cmd.Println("Preparing solidity compiler ...")
    42  
    43  				err = installCmd.Run()
    44  				if err != nil {
    45  					return errors.Wrap(err, "failed to prepare solc")
    46  				}
    47  			}
    48  			solc, err := util.SolidityVersion(_solCompiler)
    49  			if err != nil {
    50  				return errors.Wrap(err, "solidity compiler not ready")
    51  			}
    52  			if !checkCompilerVersion(solc) {
    53  				return errors.Errorf("unsupported solc version %d.%d.%d, expects solc version 0.8.17\n",
    54  					solc.Major, solc.Minor, solc.Patch)
    55  			}
    56  
    57  			cmd.Println("Solidity compiler is ready now.")
    58  			return nil
    59  		},
    60  	}
    61  }