Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cmd/cue/cmd: move tag flags handling to central location
Browse files Browse the repository at this point in the history
This refactoring will help implement variable injection (os, user, etc.)

Change-Id: I7fb234f015ab54807c578052752e2d6ddc96b5ea
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9577
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed May 4, 2021
1 parent 3ef90b3 commit 4f7caea
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ Ensure commands are defined in a "_tool.cue" file.
}

cmd.Flags().SetInterspersed(false)
cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

addInjectionFlags(cmd.Flags(), true)

return cmd
}
19 changes: 16 additions & 3 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"regexp"
"strings"

"github.com/spf13/pflag"
"golang.org/x/text/language"
"golang.org/x/text/message"

Expand Down Expand Up @@ -392,7 +393,9 @@ func newBuildPlan(cmd *Command, args []string, cfg *config) (p *buildPlan, err e
}
cfg.reFile = re

cfg.loadCfg.Tags = flagInject.StringArray(cmd)
if err := setTags(cmd.Flags(), cfg.loadCfg); err != nil {
return nil, err
}

return p, nil
}
Expand All @@ -401,6 +404,12 @@ func (p *buildPlan) matchFile(file string) bool {
return p.cfg.reFile.MatchString(file)
}

func setTags(f *pflag.FlagSet, cfg *load.Config) error {
tags, _ := f.GetStringArray(string(flagInject))
cfg.Tags = tags
return nil
}

type decoderInfo struct {
file *build.File
d *encoding.Decoder // may be nil if delayed
Expand Down Expand Up @@ -728,12 +737,16 @@ func buildToolInstances(cmd *Command, binst []*build.Instance) ([]*cue.Instance,
return instances, nil
}

func buildTools(cmd *Command, tags, args []string) (*cue.Instance, error) {
func buildTools(cmd *Command, args []string) (*cue.Instance, error) {

cfg := &load.Config{
Tags: tags,
Tools: true,
}
f := cmd.cmd.Flags()
if err := setTags(f, cfg); err != nil {
return nil, err
}

binst := loadFromArgs(cmd, args, cfg)
if len(binst) == 0 {
return nil, nil
Expand Down
4 changes: 1 addition & 3 deletions cmd/cue/cmd/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ The --expression flag is used to only print parts of a configuration.

addOutFlags(cmd.Flags(), true)
addOrphanFlags(cmd.Flags())
addInjectionFlags(cmd.Flags(), false)

cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "evaluate this expression only")

cmd.Flags().BoolP(string(flagAttributes), "A", false,
"display field attributes")

cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

// TODO: Option to include comments in output.
return cmd
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/cue/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Examples:

addOutFlags(cmd.Flags(), true)
addOrphanFlags(cmd.Flags())
addInjectionFlags(cmd.Flags(), false)

cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "evaluate this expression only")

Expand All @@ -71,9 +72,6 @@ Examples:
cmd.Flags().BoolP(string(flagAll), "a", false,
"show optional and hidden fields")

cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

// TODO: Option to include comments in output.
return cmd
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cue/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ yaml output as YAML

addOutFlags(cmd.Flags(), true)
addOrphanFlags(cmd.Flags())
addInjectionFlags(cmd.Flags(), false)

cmd.Flags().Bool(string(flagEscape), false, "use HTML escaping")
cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "export this expression only")
cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

return cmd
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func addOrphanFlags(f *pflag.FlagSet) {
f.Bool(string(flagMerge), true, "merge non-CUE files")
}

func addInjectionFlags(f *pflag.FlagSet, auto bool) {
f.StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")
}

type flagName string

func (f flagName) Bool(cmd *Command) bool {
Expand Down
13 changes: 2 additions & 11 deletions cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ func New(args []string) (cmd *Command, err error) {
return nil, err
}

tags, err := cmd.cmd.Flags().GetStringArray(string(flagInject))
if err != nil {
return nil, err
}
args = cmd.cmd.Flags().Args()
rootCmd.SetArgs(args)

Expand All @@ -272,7 +268,7 @@ func New(args []string) (cmd *Command, err error) {
return cmd, nil // Forces unknown command message from Cobra.
}

tools, err := buildTools(cmd, tags, args[1:])
tools, err := buildTools(cmd, args[1:])
if err != nil {
return cmd, err
}
Expand All @@ -295,7 +291,6 @@ type subSpec struct {
}

func addSubcommands(cmd *Command, sub map[string]*subSpec, args []string, isHelp bool) error {
var tags []string
if len(args) > 0 {
if _, ok := sub[args[0]]; ok {
oldargs := []string{args[0]}
Expand All @@ -313,10 +308,6 @@ func addSubcommands(cmd *Command, sub map[string]*subSpec, args []string, isHelp
if err != nil {
return err
}
tags, err = cmd.cmd.Flags().GetStringArray(string(flagInject))
if err != nil {
return err
}
args = cmd.cmd.Flags().Args()
cmd.root.SetArgs(append(oldargs, args...))
}
Expand All @@ -330,7 +321,7 @@ func addSubcommands(cmd *Command, sub map[string]*subSpec, args []string, isHelp
args = args[1:]
}

tools, err := buildTools(cmd, tags, args)
tools, err := buildTools(cmd, args)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/cue/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ func newVetCmd(c *Command) *cobra.Command {
}

addOrphanFlags(cmd.Flags())
addInjectionFlags(cmd.Flags(), false)

cmd.Flags().BoolP(string(flagConcrete), "c", false,
"require the evaluation to be concrete")

cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

return cmd
}

Expand Down

0 comments on commit 4f7caea

Please sign in to comment.