cmd/cue/cmd: factor out task code

Move to directories in which the corresponding
.cue files will be put.

Updates #39

Change-Id: If3cb498b9e3fe6e10905c4d461c2942d9fbbd997
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/1923
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/internal/task/task.go b/internal/task/task.go
new file mode 100644
index 0000000..b494a1a
--- /dev/null
+++ b/internal/task/task.go
@@ -0,0 +1,62 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package task provides a registry for tasks to be used by commands.
+package task
+
+import (
+	"context"
+	"io"
+	"sync"
+
+	"cuelang.org/go/cue"
+)
+
+// A Context provides context for running a task.
+type Context struct {
+	Context context.Context
+	Stdout  io.Writer
+	Stderr  io.Writer
+}
+
+// A RunnerFunc creates a Runner.
+type RunnerFunc func(v cue.Value) (Runner, error)
+
+// A Runner defines a command type.
+type Runner interface {
+	// Init is called with the original configuration before any task is run.
+	// As a result, the configuration may be incomplete, but allows some
+	// validation before tasks are kicked off.
+	// Init(v cue.Value)
+
+	// Runner runs given the current value and returns a new value which is to
+	// be unified with the original result.
+	Run(ctx *Context, v cue.Value) (results interface{}, err error)
+}
+
+// Register registers a task for cue commands.
+func Register(key string, f RunnerFunc) {
+	runners.Store(key, f)
+}
+
+// Lookup returns the RunnerFunc for a key.
+func Lookup(key string) RunnerFunc {
+	v, ok := runners.Load(key)
+	if !ok {
+		return nil
+	}
+	return v.(RunnerFunc)
+}
+
+var runners sync.Map