Skip to content

Commit

Permalink
cmd/cue/cmd: remove a few unused parameters
Browse files Browse the repository at this point in the history
As spotted by unparam; they all seemed okay to omit entirely.
While here, I noticed that exitIfErr was entirely unnecessary as well.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I54763d6f6e76de683596053fc36580531f2cb5cd
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1169111
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Sep 19, 2023
1 parent c08344a commit 2f6b27b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
33 changes: 12 additions & 21 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ var defaultConfig = config{

var inTest = false

func exitIfErr(cmd *Command, inst *cue.Instance, err error, fatal bool) {
exitOnErr(cmd, err, fatal)
}

func getLang() language.Tag {
loc := os.Getenv("LC_ALL")
if loc == "" {
Expand Down Expand Up @@ -108,7 +104,7 @@ func exitOnErr(cmd *Command, err error, fatal bool) {
}
}

func loadFromArgs(cmd *Command, args []string, cfg *load.Config) []*build.Instance {
func loadFromArgs(args []string, cfg *load.Config) []*build.Instance {
binst := load.Instances(args, cfg)
if len(binst) == 0 {
return nil
Expand Down Expand Up @@ -376,7 +372,7 @@ type config struct {
loadCfg *load.Config
}

func newBuildPlan(cmd *Command, args []string, cfg *config) (p *buildPlan, err error) {
func newBuildPlan(cmd *Command, cfg *config) (p *buildPlan, err error) {
if cfg == nil {
cfg = &defaultConfig
}
Expand All @@ -400,9 +396,7 @@ func newBuildPlan(cmd *Command, args []string, cfg *config) (p *buildPlan, err e
}
cfg.reFile = re

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

return p, nil
}
Expand All @@ -411,7 +405,7 @@ func (p *buildPlan) matchFile(file string) bool {
return p.cfg.reFile.MatchString(file)
}

func setTags(cfg *load.Config, flags ...*pflag.FlagSet) error {
func setTags(cfg *load.Config, flags ...*pflag.FlagSet) {
// setTags usually receives a subcommmand's flags, plus the parent (root) command's flags,
// so that we can support "cue cmd -t foo=bar mycmd" as well as the short form "cue -t foo=bar mycmd".
// TODO: once we remove "cue cmd" short-hand support, go back to a single FlagSet parameter.
Expand All @@ -422,7 +416,6 @@ func setTags(cfg *load.Config, flags ...*pflag.FlagSet) error {
cfg.TagVars = load.DefaultTagVars()
}
}
return nil
}

type decoderInfo struct {
Expand Down Expand Up @@ -523,14 +516,14 @@ func (p *buildPlan) importFiles(b *build.Instance) error {
}

func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err error) {
p, err = newBuildPlan(cmd, args, cfg)
p, err = newBuildPlan(cmd, cfg)
if err != nil {
return nil, err
}

adt.DebugSort, _ = strconv.Atoi(os.Getenv("CUE_DEBUG_SORT_ARCS"))

builds := loadFromArgs(cmd, args, cfg.loadCfg)
builds := loadFromArgs(args, cfg.loadCfg)
if builds == nil {
return nil, errors.Newf(token.NoPos, "invalid args")
}
Expand Down Expand Up @@ -727,7 +720,7 @@ func buildInstances(cmd *Command, binst []*build.Instance, ignoreErrors bool) []
// If there are no files and User is true, then use those?
// Always use all files in user mode?
instances, err := cmd.ctx.BuildInstances(binst)
exitIfErr(cmd, nil, err, true)
exitOnErr(cmd, err, true)

insts := make([]*instance, len(instances))
for i, v := range instances {
Expand All @@ -747,12 +740,12 @@ func buildInstances(cmd *Command, binst []*build.Instance, ignoreErrors bool) []
for _, inst := range instances {
// TODO: consider merging errors of multiple files, but ensure
// duplicates are removed.
exitIfErr(cmd, nil, inst.Validate(), !flagIgnore.Bool(cmd))
exitOnErr(cmd, inst.Validate(), !flagIgnore.Bool(cmd))
}
return insts
}

func buildToolInstances(cmd *Command, binst []*build.Instance) ([]*cue.Instance, error) {
func buildToolInstances(binst []*build.Instance) ([]*cue.Instance, error) {
instances := cue.Build(binst)
for _, inst := range instances {
if inst.Err != nil {
Expand All @@ -774,11 +767,9 @@ func buildTools(cmd *Command, args []string) (*cue.Instance, error) {
Tools: true,
}
f := cmd.cmd.Flags()
if err := setTags(cfg, f, cmd.cmd.Parent().Flags()); err != nil {
return nil, err
}
setTags(cfg, f, cmd.cmd.Parent().Flags())

binst := loadFromArgs(cmd, args, cfg)
binst := loadFromArgs(args, cfg)
if len(binst) == 0 {
return nil, nil
}
Expand All @@ -801,7 +792,7 @@ func buildTools(cmd *Command, args []string) (*cue.Instance, error) {
inst.Files = inst.Files[:k]
}

insts, err := buildToolInstances(cmd, binst)
insts, err := buildToolInstances(binst)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func doTasks(cmd *Command, typ, command string, root *cue.Instance) error {
c := flow.New(cfg, root, newTaskFunc(cmd))

err := c.Run(context.Background())
exitIfErr(cmd, root, err, true)
exitOnErr(cmd, err, true)

return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func newFmtCmd(c *Command) *cobra.Command {
Long: `Fmt formats the given files or the files for the given packages in place
`,
RunE: mkRunE(c, func(cmd *Command, args []string) error {
plan, err := newBuildPlan(cmd, args, &config{loadCfg: &load.Config{
plan, err := newBuildPlan(cmd, &config{loadCfg: &load.Config{
Tests: true,
Tools: true,
AllCUEFiles: true,
Package: "*",
}})
exitOnErr(cmd, err, true)

builds := loadFromArgs(cmd, args, plan.cfg.loadCfg)
builds := loadFromArgs(args, plan.cfg.loadCfg)
if builds == nil {
exitOnErr(cmd, errors.Newf(token.NoPos, "invalid args"), true)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func extract(cmd *Command, args []string) error {
// command specifies a Go package(s) that belong to the main module
// and where for some reason the
// determine module root:
binst := loadFromArgs(cmd, []string{"."}, nil)[0]
binst := loadFromArgs([]string{"."}, nil)[0]

// TODO: require explicitly set root.
root := binst.Root
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ removal.
}

func runTrim(cmd *Command, args []string) error {
binst := loadFromArgs(cmd, args, nil)
binst := loadFromArgs(args, nil)
if binst == nil {
return nil
}
Expand Down

0 comments on commit 2f6b27b

Please sign in to comment.