cmd/cue/cmd: allow proceeding on validation error

this is especially useful for the eval command, where it allows
viewing errors within context.

Change-Id: I39ac0a182c291200f915f3e81126405e9bce6525
Reviewed-on: https://cue-review.googlesource.com/c/1542
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index e8933eb..5720d4e 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -24,11 +24,13 @@
 	"github.com/spf13/cobra"
 )
 
-func exitIfErr(cmd *cobra.Command, inst *cue.Instance, err error) {
+func exitIfErr(cmd *cobra.Command, inst *cue.Instance, err error, fatal bool) {
 	if err != nil {
 		fmt.Fprintf(cmd.OutOrStderr(), "--- %s\n", inst.Dir)
 		errors.Print(cmd.OutOrStderr(), err)
-		exit()
+		if fatal {
+			exit()
+		}
 	}
 }
 
@@ -54,13 +56,14 @@
 	for _, inst := range instances {
 		// TODO: consider merging errors of multiple files, but ensure
 		// duplicates are removed.
-		exitIfErr(cmd, inst, inst.Err)
+		exitIfErr(cmd, inst, inst.Err, true)
 	}
 
+	// TODO check errors after the fact in case of ignore.
 	for _, inst := range instances {
 		// TODO: consider merging errors of multiple files, but ensure
 		// duplicates are removed.
-		exitIfErr(cmd, inst, inst.Value().Validate())
+		exitIfErr(cmd, inst, inst.Value().Validate(), !*fIgnore)
 	}
 	return instances
 }
@@ -84,6 +87,6 @@
 	}
 
 	inst := cue.Merge(buildInstances(cmd, binst)...).Build(ti)
-	exitIfErr(cmd, inst, inst.Err)
+	exitIfErr(cmd, inst, inst.Err, true)
 	return inst
 }
diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go
index 2f9dbdc..db9152a 100644
--- a/cmd/cue/cmd/root.go
+++ b/cmd/cue/cmd/root.go
@@ -156,6 +156,7 @@
 	fDryrun   = RootCmd.PersistentFlags().BoolP("dryrun", "n", false, "only run simulation")
 	fPackage  = RootCmd.PersistentFlags().StringP("package", "p", "", "CUE package to evaluate")
 	fSimplify = RootCmd.PersistentFlags().BoolP("simplify", "s", false, "simplify output")
+	fIgnore   = RootCmd.PersistentFlags().BoolP("ignore", "i", false, "proceed in the presence of errors")
 )
 
 // initConfig reads in config file and ENV variables if set.