github.com/0xsequence/ethkit@v1.25.0/cmd/ethkit/artifacts.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/0xsequence/ethkit/ethartifact"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func init() {
    12  	artifacts := &artifacts{}
    13  	cmd := &cobra.Command{
    14  		Use:   "artifacts",
    15  		Short: "Print the contract abi or bytecode from a truffle artifacts file",
    16  		Run:   artifacts.Run,
    17  	}
    18  
    19  	cmd.Flags().String("file", "", "path to truffle contract artifacts file (required)")
    20  	cmd.Flags().Bool("abi", false, "abi")
    21  	cmd.Flags().Bool("bytecode", false, "bytecode")
    22  
    23  	rootCmd.AddCommand(cmd)
    24  }
    25  
    26  type artifacts struct {
    27  }
    28  
    29  func (c *artifacts) Run(cmd *cobra.Command, args []string) {
    30  	fFile, _ := cmd.Flags().GetString("file")
    31  	fAbi, _ := cmd.Flags().GetBool("abi")
    32  	fBytecode, _ := cmd.Flags().GetBool("bytecode")
    33  
    34  	if fFile == "" {
    35  		fmt.Println("error: please pass --file")
    36  		help(cmd)
    37  		return
    38  	}
    39  	if !fAbi && !fBytecode {
    40  		fmt.Println("error: please pass either --abi or --bytecode")
    41  		help(cmd)
    42  		return
    43  	}
    44  	if fAbi && fBytecode {
    45  		fmt.Println("error: please pass either --abi or --bytecode, not both")
    46  		help(cmd)
    47  		return
    48  	}
    49  
    50  	artifacts, err := ethartifact.ParseArtifactFile(fFile)
    51  	if err != nil {
    52  		log.Fatal(err)
    53  		return
    54  	}
    55  
    56  	if fAbi {
    57  		fmt.Println(string(artifacts.ABI))
    58  	}
    59  
    60  	if fBytecode {
    61  		fmt.Println(artifacts.Bytecode)
    62  	}
    63  }