github.com/aergoio/aergo@v1.3.1/cmd/aergoluac/main.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package main
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"os"
    12  
    13  	"github.com/aergoio/aergo/cmd/aergoluac/util"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	rootCmd *cobra.Command
    19  	abiFile string
    20  	payload bool
    21  	version bool
    22  )
    23  
    24  var githash = "No git hash provided"
    25  
    26  func init() {
    27  	rootCmd = &cobra.Command{
    28  		Use:   "aergoluac --payload srcfile\n  aergoluac --abi abifile srcfile bcfile",
    29  		Short: "Compile a lua contract",
    30  		Long:  "Compile a lua contract. This command makes a bytecode file and a ABI file or prints a payload data.",
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			var err error
    33  
    34  			if version {
    35  				cmd.Printf("Aergoluac %s\n", githash)
    36  				return nil
    37  			}
    38  			if payload {
    39  				if len(args) == 0 {
    40  					err = util.DumpFromStdin()
    41  				} else {
    42  					err = util.DumpFromFile(args[0])
    43  				}
    44  			} else {
    45  				if len(args) < 2 {
    46  					return errors.New("2 arguments required: <srcfile> <bcfile>")
    47  				}
    48  				err = util.CompileFromFile(args[0], args[1], abiFile)
    49  			}
    50  			if err != nil {
    51  				fmt.Fprintln(os.Stderr, "Error:", err)
    52  			}
    53  			return nil
    54  		},
    55  	}
    56  	rootCmd.PersistentFlags().StringVarP(&abiFile, "abi", "a", "", "abi filename")
    57  	rootCmd.PersistentFlags().BoolVar(&payload, "payload", false, "print the compilation result consisting of bytecode and abi")
    58  	rootCmd.PersistentFlags().BoolVar(&version, "version", false, "print the version number of aergoluac")
    59  }
    60  
    61  func main() {
    62  	if err := rootCmd.Execute(); err != nil {
    63  		os.Exit(1)
    64  	}
    65  }