this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Revert "cmd/cue: rip out cue.Instance and cue.Merge from 'cue cmd'"

This reverts https://cuelang.org/cl/1193718.

We broke `cue cmd ls ./...` in cuelang.org's k8s tutorial.
We will add a regression test and attempt these changes a second time.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iba30ccd77fefce05f2f98301ce360ec3b23880a3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193747
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+76 -20
+63 -12
cmd/cue/cmd/common.go
··· 39 39 "cuelang.org/go/internal/core/adt" 40 40 "cuelang.org/go/internal/encoding" 41 41 "cuelang.org/go/internal/filetypes" 42 + "cuelang.org/go/internal/value" 42 43 ) 43 44 44 45 var requestedVersion = os.Getenv("CUE_SYNTAX_OVERRIDE") ··· 764 765 return insts 765 766 } 766 767 767 - func buildTools(cmd *Command, args []string) (cue.Value, error) { 768 + func buildToolInstances(binst []*build.Instance) ([]*cue.Instance, error) { 769 + instances := cue.Build(binst) 770 + for _, inst := range instances { 771 + if inst.Err != nil { 772 + return nil, inst.Err 773 + } 774 + } 775 + 776 + // TODO check errors after the fact in case of ignore. 777 + for _, inst := range instances { 778 + if err := inst.Value().Validate(); err != nil { 779 + return nil, err 780 + } 781 + } 782 + return instances, nil 783 + } 784 + 785 + func buildTools(cmd *Command, args []string) (*cue.Instance, error) { 768 786 cfg, err := defaultConfig() 769 787 if err != nil { 770 - return cue.Value{}, err 788 + return nil, err 771 789 } 772 790 loadCfg := *cfg.loadCfg 773 791 loadCfg.Tools = true 774 792 f := cmd.cmd.Flags() 775 793 setTags(&loadCfg, f, cmd.cmd.Parent().Flags()) 776 794 777 - builds := loadFromArgs(args, &loadCfg) 795 + binst := loadFromArgs(args, &loadCfg) 796 + if len(binst) == 0 { 797 + return nil, nil 798 + } 799 + included := map[string]bool{} 778 800 779 - values, err := cmd.ctx.BuildInstances(builds) 801 + ti := binst[0].Context().NewInstance(binst[0].Root, nil) 802 + for _, inst := range binst { 803 + k := 0 804 + for _, f := range inst.Files { 805 + if strings.HasSuffix(f.Filename, "_tool.cue") { 806 + if !included[f.Filename] { 807 + _ = ti.AddSyntax(f) 808 + included[f.Filename] = true 809 + } 810 + continue 811 + } 812 + inst.Files[k] = f 813 + k++ 814 + } 815 + inst.Files = inst.Files[:k] 816 + } 817 + 818 + insts, err := buildToolInstances(binst) 780 819 if err != nil { 781 - return cue.Value{}, err 820 + return nil, err 821 + } 822 + 823 + inst := insts[0] 824 + if len(insts) > 1 { 825 + inst = cue.Merge(insts...) 782 826 } 783 - // 'cue cmd' with multiple package arguments, such as './...', 784 - // currently runs a single command on the unification of all given packages. 785 - // See https://cuelang.org/issue/1325. 786 - v := values[0] 787 - for _, v2 := range values[1:] { 788 - v = v.Unify(v2) 827 + 828 + r := value.ConvertToRuntime(inst.Value().Context()) 829 + for _, b := range binst { 830 + for _, i := range b.Imports { 831 + if _, err := r.Build(i); err != nil { 832 + return nil, err 833 + } 834 + } 789 835 } 790 - return v, nil 836 + 837 + // Set path equal to the package from which it is loading. 838 + ti.ImportPath = binst[0].ImportPath 839 + 840 + inst = inst.Build(ti) 841 + return inst, inst.Err 791 842 } 792 843 793 844 func shortFile(root string, f *build.File) string {
+9 -5
cmd/cue/cmd/custom.go
··· 43 43 const commandSection = "command" 44 44 45 45 func lookupString(obj cue.Value, key, def string) string { 46 - str, err := obj.LookupPath(cue.MakePath(cue.Str(key))).String() 46 + str, err := obj.Lookup(key).String() 47 47 if err == nil { 48 48 def = str 49 49 } ··· 62 62 // addCustomCommands iterates over all commands defined under field typ 63 63 // and adds them as cobra subcommands to cmd. 64 64 // The func is only used in `cue help cmd`, which doesn't show errors. 65 - func addCustomCommands(c *Command, cmd *cobra.Command, typ string, tools cue.Value) { 66 - commands := tools.LookupPath(cue.MakePath(cue.Str(typ))) 65 + func addCustomCommands(c *Command, cmd *cobra.Command, typ string, tools *cue.Instance) { 66 + commands := tools.Lookup(typ) 67 67 if !commands.Exists() { 68 68 return 69 69 } ··· 80 80 } 81 81 82 82 // customCommand creates a cobra.Command out of a CUE command definition. 83 - func customCommand(c *Command, typ, name string, tools cue.Value) (*cobra.Command, error) { 83 + func customCommand(c *Command, typ, name string, tools *cue.Instance) (*cobra.Command, error) { 84 + if tools == nil { 85 + return nil, errors.New("no commands defined") 86 + } 87 + 84 88 // TODO: validate allowing incomplete. 85 89 o := tools.Lookup(typ, name) 86 90 if !o.Exists() { ··· 145 149 return sub, nil 146 150 } 147 151 148 - func doTasks(cmd *Command, typ, command string, root cue.Value) error { 152 + func doTasks(cmd *Command, typ, command string, root *cue.Instance) error { 149 153 cfg := &flow.Config{ 150 154 Root: cue.MakePath(cue.Str(commandSection), cue.Str(command)), 151 155 InferTasks: true,
+4 -3
cmd/cue/cmd/testdata/script/cmd_many.txtar
··· 20 20 exec cue export ./... 21 21 cmp stdout stdout.export-post.golden 22 22 23 - # Verify that cue cmd ls ./... still works the same. 24 - exec cue cmd ls ./... 25 - cmp stdout stdout.ls-pre.golden 23 + # Attempt to ls in this state, which fails, as each of the CUE packages 24 + # hold an items list with different lengths. 25 + ! exec cue cmd ls ./... 26 + stderr 'itemsList: incompatible list lengths \(0 and 1\)' 26 27 27 28 -- cue.mod/module.cue -- 28 29 module: "mod.com"