pkg/tool: add constraints for tasks

Updates #39

Change-Id: I9036a366a798cb2cd08d6e4695c1ab9e6d32da66
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/1940
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/testdata/tasks/cmd_baddisplay.out b/cmd/cue/cmd/testdata/tasks/cmd_baddisplay.out
index ebb7a3f..d16a8f3 100644
--- a/cmd/cue/cmd/testdata/tasks/cmd_baddisplay.out
+++ b/cmd/cue/cmd/testdata/tasks/cmd_baddisplay.out
@@ -1,4 +1,4 @@
 unsupported op &(number, (string)*):
     $CWD/testdata/tasks/task_tool.cue:29:9
-    tool/cli:4:10
+    tool/cli:4:9
     
diff --git a/cue/builtin.go b/cue/builtin.go
index 3fb7c0c..63d0547 100644
--- a/cue/builtin.go
+++ b/cue/builtin.go
@@ -233,7 +233,13 @@
 
 func initBuiltins(pkgs map[string]*builtinPkg) {
 	ctx := sharedIndex.newContext()
-	for k, b := range pkgs {
+	keys := []string{}
+	for k := range pkgs {
+		keys = append(keys, k)
+	}
+	sort.Strings(keys)
+	for _, k := range keys {
+		b := pkgs[k]
 		e := mustCompileBuiltins(ctx, b, k)
 		builtins[k] = e
 		builtins["-/"+path.Base(k)] = e
diff --git a/cue/builtins.go b/cue/builtins.go
index a8cb704..136a1fd 100644
--- a/cue/builtins.go
+++ b/cue/builtins.go
@@ -1874,21 +1874,17 @@
 		usage?: string
 		short?: string
 		long?:  string
-		var <name>: {
-			value:       _
-			description: "" | string
-		}
 		tasks <name>: Task
 	}
-	Task _kind: =~"\\."
+	Task kind: =~"\\."
 }`,
 	},
 	"tool/cli": &builtinPkg{
 		native: []*builtin{{}},
 		cue: `{
 	Print: {
-		_kind: "tool/cli.Print"
-		text:  string
+		kind: *"tool/cli.Print" | "print"
+		text: string
 	}
 }`,
 	},
@@ -1896,7 +1892,7 @@
 		native: []*builtin{{}},
 		cue: `{
 	Run: {
-		_kind:    "tool/exec.Run"
+		kind:     *"tool/exec.Run" | "exec"
 		cmd:      string | [string, ...string]
 		install?: string | [string, ...string]
 		env <Key>: string
@@ -1905,10 +1901,6 @@
 		stdin?:  string | bytes
 		success: bool
 	}
-	Env: {
-		_kind: "tool/exec.Env"
-		env <Name>: string | number
-	}
 }`,
 	},
 	"tool/file": &builtinPkg{
@@ -1946,7 +1938,7 @@
 		method: "GET"
 	}
 	Do: {
-		_kind:  "tool/http.Do"
+		kind:   *"tool/http.Do" | "http"
 		method: string
 		response: {
 			body: *bytes | string
diff --git a/cue/gen.go b/cue/gen.go
index f7a6824..f2b7832 100644
--- a/cue/gen.go
+++ b/cue/gen.go
@@ -231,6 +231,9 @@
 			switch x.Tok {
 			case token.CONST:
 				for _, spec := range x.Specs {
+					if !ast.IsExported(spec.(*ast.ValueSpec).Names[0].Name) {
+						continue
+					}
 					g.genConst(spec.(*ast.ValueSpec))
 				}
 			case token.IMPORT:
diff --git a/pkg/tool/cli/cli.cue b/pkg/tool/cli/cli.cue
index 986f43a..0318ff0 100644
--- a/pkg/tool/cli/cli.cue
+++ b/pkg/tool/cli/cli.cue
@@ -16,7 +16,7 @@
 
 // Print sends text to the stdout of the current process.
 Print: {
-	kind: "tool/cli.Print"
+	kind: *"tool/cli.Print" | "print" // for backwards compatibility
 
 	// text is the text to be printed.
 	text: string
diff --git a/pkg/tool/exec/exec.cue b/pkg/tool/exec/exec.cue
index b286f17..585bf4f 100644
--- a/pkg/tool/exec/exec.cue
+++ b/pkg/tool/exec/exec.cue
@@ -16,7 +16,7 @@
 
 // Run executes the given shell command.
 Run: {
-	kind: "tool/exec.Run"
+	kind: *"tool/exec.Run" | "exec" // exec for backwards compatibility
 
 	// cmd is the command to run.
 	cmd: string | [string, ...string]
@@ -45,9 +45,11 @@
 	success: bool
 }
 
+/* TODO
 // Env collects the environment variables of the current process.
 Env: {
 	kind: "tool/exec.Env"
 
 	env <Name>: string | number
 }
+*/
diff --git a/pkg/tool/http/http.cue b/pkg/tool/http/http.cue
index 04cee3d..f7d0fd4 100644
--- a/pkg/tool/http/http.cue
+++ b/pkg/tool/http/http.cue
@@ -20,7 +20,7 @@
 Delete: Do & {method: "DELETE"}
 
 Do: {
-	kind: "tool/http.Do"
+	kind: *"tool/http.Do" | "http" // http for backwards compatibility
 
 	method: string
 	url:    string // TODO: make url.URL type
diff --git a/pkg/tool/http/http.go b/pkg/tool/http/http.go
index 65dfdbd..86565b6 100644
--- a/pkg/tool/http/http.go
+++ b/pkg/tool/http/http.go
@@ -46,7 +46,6 @@
 }
 
 func (c *httpCmd) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) {
-	// v.Validate()
 	var header, trailer http.Header
 	method := lookupString(v, "method")
 	u := lookupString(v, "url")
diff --git a/pkg/tool/tool.cue b/pkg/tool/tool.cue
index c9c1b11..ce50b09 100644
--- a/pkg/tool/tool.cue
+++ b/pkg/tool/tool.cue
@@ -12,24 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// Package tool defines statefull operation types for cue commands.
-//
-// This package is only visible in cue files with a _tool.cue or _tool_test.cue
-// ending.
-//
-// CUE configuration files are not influenced by and do not influence anything
-// outside the configuration itself: they are hermetic. Tools solve
-// two problems: allow outside values such as environment variables,
-// file or web contents, random generators etc. to influence configuration,
-// and allow configuration to be actionable from within the tooling itself.
-// Separating these concerns makes it clear to user when outside influences are
-// in play and the tool definition can be strict about what is allowed.
-//
-// Tools are defined in files ending with _tool.cue. These files have a
-// top-level map, "command", which defines all the tools made available through
-// the cue command.
-//
-//
 package tool
 
 // A Command specifies a user-defined command.
@@ -46,125 +28,17 @@
 	// likely contain examples of usage of the command.
 	long?: string
 
-	// A var defines a value that can be set by the command line or an
-	// environment variable.
-	//
-	// Example:
-	//    var fast: {
-	//        description: "run faster than anyone"
-	//        value:       true | bool
-	//    }
-	//
-	var <name>: {
-		value:       _
-		description: "" | string
-	}
+	// TODO: define flags and environment variables.
 
 	// tasks specifies the list of things to do to run command. Tasks are
 	// typically underspecified and completed by the particular internal
 	// handler that is running them. Task de
 	tasks <name>: Task
-
-	// TODO:
-	// timeout?: number // in seconds
 }
 
-// A task defines a step in the execution of a command, server, or fix
-// operation.
+// A Task defines a step in the execution of a command.
 Task: {
+	// kind indicates the operation to run. It must be of the form
+	// packagePath.Operation.
 	kind: =~#"\."#
 }
-
-// import "cue/tool"
-//
-// command <Name>: { // from "cue/tool".Command
-//  // usage gives a short usage pattern of the command.
-//  // Example:
-//  //    fmt [-n] [-x] [packages]
-//  usage: Name | string
-//
-//  // short gives a brief on-line description of the command.
-//  // Example:
-//  //    reformat package sources
-//  short: "" | string
-//
-//  // long gives a detailed description of the command, including a
-//  // description of flags usage and examples.
-//  long: "" | string
-//
-//  // A task defines a single action to be run as part of this command.
-//  // Each task can have inputs and outputs, depending on the type
-//  // task. The outputs are initially unspecified, but are filled out
-//  // by the tooling
-//  //
-//  task <Name>: { // from "cue/tool".Task
-//   // supported fields depend on type
-//  }
-//
-//  VarValue = string | bool | int | float | [...string|int|float]
-//
-//  // var declares values that can be set by command line flags or
-//  // environment variables.
-//  //
-//  // Example:
-//  //   // environment to run in
-//  //   var env: "test" | "prod"
-//  // The tool would print documentation of this flag as:
-//  //   Flags:
-//  //      --env string    environment to run in: test(default) or prod
-//  var <Name>: VarValue
-//
-//  // flag defines a command line flag.
-//  //
-//  // Example:
-//  //   var env: "test" | "prod"
-//  //
-//  //   // augment the flag information for var
-//  //   flag env: {
-//  //       shortFlag:   "e"
-//  //       description: "environment to run in"
-//  //   }
-//  //
-//  // The tool would print documentation of this flag as:
-//  //   Flags:
-//  // -e, --env string    environment to run in: test(default), staging, or prod
-//  //
-//  flag <Name>: { // from "cue/tool".Flag
-//   // value defines the possible values for this flag.
-//   // The default is string. Users can define default values by
-//   // using disjunctions.
-//   value: env[Name].value | VarValue
-//
-//   // name, if set, allows var to be set with the command-line flag
-//   // of the given name. null disables the command line flag.
-//   name: Name | null | string
-//
-//   // short defines an abbreviated version of the flag.
-//   // Disabled by default.
-//   short: null | string
-//  }
-//
-//  // populate flag with the default values for
-//  flag: { "\(k)": { value: v } | null for k, v in var }
-//
-//  // env defines environment variables. It is populated with values
-//  // for var.
-//  //
-//  // To specify a var without an equivalent environment variable,
-//  // either specify it as a flag directly or disable the equally
-//  // named env entry explicitly:
-//  //
-//  //     var foo: string
-//  //     env foo: null  // don't use environment variables for foo
-//  //
-//  env <Name>: {å
-//   // name defines the environment variable that sets this flag.
-//   name: "CUE_VAR_" + strings.Upper(Name) | string | null
-//
-//   // The value retrieved from the environment variable or null
-//   // if not set.
-//   value: null | string | bytes
-//  }
-//  env: { "\(k)": { value: v } | null for k, v in var }
-// }
-//