Skip to content

Commit

Permalink
Validate Anonymous Apex
Browse files Browse the repository at this point in the history
Update `force apex` to parse apex locally by default, and report parse
errors, before sending it to Salesforce to execute.
  • Loading branch information
cwarden committed Nov 16, 2023
1 parent 80058db commit 54307da
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 84 deletions.
14 changes: 14 additions & 0 deletions command/apex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
"os"

. "github.com/ForceCLI/force/error"
"github.com/ForceCLI/force/lib/apex"
"github.com/spf13/cobra"
)

var skipValidation bool

func init() {
apexCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "do not validate the apex before executing")
apexCmd.Flags().BoolP("test", "t", false, "run in test context")
RootCmd.AddCommand(apexCmd)
}
Expand Down Expand Up @@ -40,6 +44,11 @@ var apexCmd = &cobra.Command{
func runApexFromStdin(testContext bool) {
fmt.Println(">> Start typing Apex code; press CTRL-D(for Mac/Linux) / Ctrl-Z (for Windows) when finished")
code, err := ioutil.ReadAll(os.Stdin)
if !skipValidation {
if err = apex.ValidateAnonymous(code); err != nil {
ErrorAndExit(err.Error())
}
}
fmt.Println("\n\n>> Executing code...")
var output string
if testContext {
Expand All @@ -58,6 +67,11 @@ func runApexInFile(filename string, testContext bool) {
if err != nil {
ErrorAndExit(err.Error())
}
if !skipValidation {
if err = apex.ValidateAnonymous(code); err != nil {
ErrorAndExit(err.Error())
}
}
var output string
if testContext {
output, err = executeAsTest(code)
Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ require (
github.com/hamba/avro/v2 v2.16.0
github.com/linkedin/goavro/v2 v2.12.0
github.com/obeattie/ohmyglob v0.0.0-20150811221449-290764208a0d
github.com/octoberswimmer/go-tree-sitter-sfapex v0.0.0-20230223121415-39a3ac1557ab
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.10.0
github.com/onsi/gomega v1.23.0
github.com/pkg/errors v0.9.1
github.com/rgalanakis/golangal v0.0.0-20210923203926-e36008487518
github.com/smacker/go-tree-sitter v0.0.0-20230223055714-4d4cba2a4780
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.14.0
google.golang.org/grpc v1.39.0-dev
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
)
Expand All @@ -37,9 +39,10 @@ require (
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down Expand Up @@ -67,11 +70,9 @@ require (
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/genproto v0.0.0-20230222225845-10f96fb3dbec // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 54307da

Please sign in to comment.