1// Copyright 2018 The CUE Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package cmd 16 17// This file contains code or initializing and running custom commands. 18 19import ( 20 "context" 21 "encoding/json" 22 "io/ioutil" 23 "net/http" 24 "net/http/httptest" 25 "strings" 26 "sync" 27 28 "github.com/spf13/cobra" 29 30 "cuelang.org/go/cue" 31 "cuelang.org/go/cue/errors" 32 itask "cuelang.org/go/internal/task" 33 "cuelang.org/go/internal/value" 34 _ "cuelang.org/go/pkg/tool/cli" // Register tasks 35 _ "cuelang.org/go/pkg/tool/exec" 36 _ "cuelang.org/go/pkg/tool/file" 37 _ "cuelang.org/go/pkg/tool/http" 38 _ "cuelang.org/go/pkg/tool/os" 39 "cuelang.org/go/tools/flow" 40) 41 42const ( 43 commandSection = "command" 44) 45 46func lookupString(obj cue.Value, key, def string) string { 47 str, err := obj.Lookup(key).String() 48 if err == nil { 49 def = str 50 } 51 return strings.TrimSpace(def) 52} 53 54// splitLine splits the first line and the rest of the string. 55func splitLine(s string) (line, tail string) { 56 line = s 57 if p := strings.IndexByte(s, '\n'); p >= 0 { 58 line, tail = strings.TrimSpace(s[:p]), strings.TrimSpace(s[p+1:]) 59 } 60 return 61} 62 63func addCustom(c *Command, parent *cobra.Command, typ, name string, tools *cue.Instance) (*cobra.Command, error) { 64 if tools == nil { 65 return nil, errors.New("no commands defined") 66 } 67 68 // TODO: validate allowing incomplete. 69 o := tools.Lookup(typ, name) 70 if !o.Exists() { 71 return nil, o.Err() 72 } 73 74 // Ensure there is at least one tool file. 75 // TODO: remove this block to allow commands to be defined in any file. 76outer: 77 for _, v := range []cue.Value{tools.Lookup(typ), o} { 78 _, w := value.ToInternal(v) 79 for _, c := range w.Conjuncts { 80 src := c.Source() 81 if src == nil { 82 continue 83 } 84 if strings.HasSuffix(src.Pos().Filename(), "_tool.cue") { 85 break outer 86 } 87 } 88 return nil, errors.New("no command %q defined in a *_tool.cue file") 89 } 90 91 docs := o.Doc() 92 var usage, short, long string 93 if len(docs) > 0 { 94 txt := docs[0].Text() 95 short, txt = splitLine(txt) 96 short = lookupString(o, "short", short) 97 if strings.HasPrefix(txt, "Usage:") { 98 usage, txt = splitLine(txt[len("Usage:"):]) 99 } 100 usage = lookupString(o, "usage", usage) 101 usage = lookupString(o, "$usage", usage) 102 long = lookupString(o, "long", txt) 103 } 104 if !strings.HasPrefix(usage, name+" ") { 105 usage = name 106 } 107 sub := &cobra.Command{ 108 Use: usage, 109 Short: lookupString(o, "$short", short), 110 Long: lookupString(o, "$long", long), 111 RunE: mkRunE(c, func(cmd *Command, args []string) error { 112 // TODO: 113 // - parse flags and env vars 114 // - constrain current config with config section 115 116 return doTasks(cmd, typ, name, tools) 117 }), 118 } 119 parent.AddCommand(sub) 120 121 // TODO: implement var/flag handling. 122 return sub, nil 123} 124 125func doTasks(cmd *Command, typ, command string, root *cue.Instance) error { 126 cfg := &flow.Config{ 127 Root: cue.MakePath(cue.Str(commandSection), cue.Str(command)), 128 InferTasks: true, 129 IgnoreConcrete: true, 130 } 131 132 c := flow.New(cfg, root, newTaskFunc(cmd)) 133 134 err := c.Run(context.Background()) 135 exitIfErr(cmd, root, err, true) 136 137 return err 138} 139 140// func (r *customRunner) tagReference(t *task, ref cue.Value) error { 141// inst, path := ref.Reference() 142// if len(path) == 0 { 143// return errors.Newf(ref.Pos(), 144// "$after must be a reference or list of references, found %s", ref) 145// } 146// if inst != r.root { 147// return errors.Newf(ref.Pos(), 148// "reference in $after must refer to value in same package") 149// } 150// // TODO: allow referring to group of tasks. 151// if !r.tagDependencies(t, path) { 152// return errors.Newf(ref.Pos(), 153// "reference %s does not refer to task or task group", 154// strings.Join(path, "."), // TODO: more correct representation. 155// ) 156 157// } 158// return nil 159// } 160 161func isTask(v cue.Value) bool { 162 // This mimics the v0.2 behavior. The cutoff is really quite arbitrary. A 163 // sane implementation should not use InferTasks, really. 164 if len(v.Path().Selectors()) == 0 { 165 return false 166 } 167 return v.Kind() == cue.StructKind && 168 (v.Lookup("$id").Exists() || v.Lookup("kind").Exists()) 169} 170 171var legacyKinds = map[string]string{ 172 "exec": "tool/exec.Run", 173 "http": "tool/http.Do", 174 "print": "tool/cli.Print", 175 "testserver": "cmd/cue/cmd.Test", 176} 177 178func newTaskFunc(cmd *Command) flow.TaskFunc { 179 return func(v cue.Value) (flow.Runner, error) { 180 if !isTask(v) { 181 return nil, nil 182 } 183 184 kind, err := v.Lookup("$id").String() 185 if err != nil { 186 // Lookup kind for backwards compatibility. 187 // TODO: consider at some point whether kind can be removed. 188 var err1 error 189 kind, err1 = v.Lookup("kind").String() 190 if err1 != nil { 191 return nil, errors.Promote(err1, "newTask") 192 } 193 } 194 if k, ok := legacyKinds[kind]; ok { 195 kind = k 196 } 197 rf := itask.Lookup(kind) 198 if rf == nil { 199 return nil, errors.Newf(v.Pos(), "runner of kind %q not found", kind) 200 } 201 202 // Verify entry against template. 203 v = value.UnifyBuiltin(v, kind) 204 if err := v.Err(); err != nil { 205 return nil, errors.Promote(err, "newTask") 206 } 207 208 runner, err := rf(v) 209 if err != nil { 210 return nil, errors.Promote(err, "errors running task") 211 } 212 213 return flow.RunnerFunc(func(t *flow.Task) error { 214 c := &itask.Context{ 215 Context: t.Context(), 216 Stdin: cmd.InOrStdin(), 217 Stdout: cmd.OutOrStdout(), 218 Stderr: cmd.OutOrStderr(), 219 Obj: t.Value(), 220 } 221 value, err := runner.Run(c) 222 if err != nil { 223 return err 224 } 225 if value != nil { 226 _ = t.Fill(value) 227 } 228 return nil 229 }), nil 230 } 231} 232 233func init() { 234 itask.Register("cmd/cue/cmd.Test", newTestServerCmd) 235} 236 237var testOnce sync.Once 238 239func newTestServerCmd(v cue.Value) (itask.Runner, error) { 240 server := "" 241 testOnce.Do(func() { 242 s := httptest.NewServer(http.HandlerFunc( 243 func(w http.ResponseWriter, req *http.Request) { 244 data, _ := ioutil.ReadAll(req.Body) 245 d := map[string]interface{}{ 246 "data": string(data), 247 "when": "now", 248 } 249 enc := json.NewEncoder(w) 250 _ = enc.Encode(d) 251 })) 252 server = s.URL 253 }) 254 return testServerCmd(server), nil 255} 256 257type testServerCmd string 258 259func (s testServerCmd) Run(ctx *itask.Context) (x interface{}, err error) { 260 return map[string]interface{}{"url": string(s)}, nil 261} 262