github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/apex.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  )
     8  
     9  var cmdApex = &Command{
    10  	Run:   runApex,
    11  	Usage: "apex [file]",
    12  	Short: "Execute anonymous Apex code",
    13  	Long: `
    14  Execute anonymous Apex code
    15  
    16  Examples:
    17  
    18    force apex ~/test.apex
    19  
    20    force apex
    21    >> Start typing Apex code; press CTRL-D when finished
    22  
    23  `,
    24  }
    25  
    26  func init() {
    27  }
    28  
    29  func runApex(cmd *Command, args []string) {
    30  	var code []byte
    31  	var err error
    32  	if len(args) == 1 {
    33  		code, err = ioutil.ReadFile(args[0])
    34  	} else if len(args) > 1 {
    35  		fmt.Println("Got test indication.")
    36  	} else {
    37  		fmt.Println(">> Start typing Apex code; press CTRL-D when finished\n")
    38  		code, err = ioutil.ReadAll(os.Stdin)
    39  		fmt.Println("\n\n>> Executing code...\n")
    40  	}
    41  	if err != nil {
    42  		ErrorAndExit(err.Error())
    43  	}
    44  	force, _ := ActiveForce()
    45  	if len(args) <= 1 {
    46  		output, err := force.Partner.ExecuteAnonymous(string(code))
    47  		if err != nil {
    48  			ErrorAndExit(err.Error())
    49  		}
    50  		fmt.Println(output)
    51  	} else {
    52  		apexclass := args[1]
    53  		fmt.Println(apexclass)
    54  		err := force.GetCodeCoverage("", apexclass)
    55  		if err != nil {
    56  			ErrorAndExit(err.Error())
    57  		}
    58  	}
    59  }