Skip to content

Commit

Permalink
pass opts (flags) as pointer to more places and through recursion; st…
Browse files Browse the repository at this point in the history
…art breaking out op funcs for differnt node types

Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Nov 19, 2021
1 parent 937a998 commit bd4c8b2
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .hof/shadow/Cli/cmd/cuetils/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&(flags.RootPflags.Out), "out", "", "cue", "output encoding [cue,yaml,json]")
RootCmd.PersistentFlags().StringVarP(&(flags.RootPflags.Outname), "outname", "o", "", "output filename when being used")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.Overwrite), "overwrite", "F", false, "overwrite files being processed")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.TypeErrors), "type-errors", "E", false, "error when nodes or leafs have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.AllTypeErrors), "type-errors", "E", false, "error when nodes or leafs have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.NodeTypeErrors), "node-type-errors", "N", false, "error when nodes have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.LeafTypeErrors), "leaf-type-errors", "L", false, "error when leafs have type mismatches")
}
Expand Down
2 changes: 1 addition & 1 deletion .hof/shadow/Cli/cmd/cuetils/flags/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type RootPflagpole struct {
Out string
Outname string
Overwrite bool
TypeErrors bool
AllTypeErrors bool
NodeTypeErrors bool
LeafTypeErrors bool
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cuetils/cmd/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CountRun(globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

counts, err := structural.CountGlobs(globs, flags.RootPflags)
counts, err := structural.CountGlobs(globs, &flags.RootPflags)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cuetils/cmd/depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func DepthRun(globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

depths, err := structural.DepthGlobs(globs, flags.RootPflags)
depths, err := structural.DepthGlobs(globs, &flags.RootPflags)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func DiffRun(orig string, next string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.DiffGlobs(orig, next, flags.RootPflags)
results, err := structural.DiffGlobs(orig, next, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -108,4 +108,4 @@ func init() {
DiffCmd.SetHelpFunc(help)
DiffCmd.SetUsageFunc(usage)

}
}
4 changes: 2 additions & 2 deletions cmd/cuetils/cmd/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func InsertRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.InsertGlobs(code, globs, flags.RootPflags)
results, err := structural.InsertGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func MaskRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.MaskGlobs(code, globs, flags.RootPflags)
results, err := structural.MaskGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -102,4 +102,4 @@ func init() {
MaskCmd.SetHelpFunc(help)
MaskCmd.SetUsageFunc(usage)

}
}
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func PatchRun(patch string, orig string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.PatchGlobs(patch, orig, flags.RootPflags)
results, err := structural.PatchGlobs(patch, orig, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -108,4 +108,4 @@ func init() {
PatchCmd.SetHelpFunc(help)
PatchCmd.SetUsageFunc(usage)

}
}
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func PickRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.PickGlobs(code, globs, flags.RootPflags)
results, err := structural.PickGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -102,4 +102,4 @@ func init() {
PickCmd.SetHelpFunc(help)
PickCmd.SetUsageFunc(usage)

}
}
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func ReplaceRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.ReplaceGlobs(code, globs, flags.RootPflags)
results, err := structural.ReplaceGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -102,4 +102,4 @@ func init() {
ReplaceCmd.SetHelpFunc(help)
ReplaceCmd.SetUsageFunc(usage)

}
}
2 changes: 1 addition & 1 deletion cmd/cuetils/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&(flags.RootPflags.Out), "out", "", "cue", "output encoding [cue,yaml,json]")
RootCmd.PersistentFlags().StringVarP(&(flags.RootPflags.Outname), "outname", "o", "", "output filename when being used")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.Overwrite), "overwrite", "F", false, "overwrite files being processed")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.TypeErrors), "type-errors", "E", false, "error when nodes or leafs have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.AllTypeErrors), "type-errors", "E", false, "error when nodes or leafs have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.NodeTypeErrors), "node-type-errors", "N", false, "error when nodes have type mismatches")
RootCmd.PersistentFlags().BoolVarP(&(flags.RootPflags.LeafTypeErrors), "leaf-type-errors", "L", false, "error when leafs have type mismatches")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TransformRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.TransformGlobs(code, globs, flags.RootPflags)
results, err := structural.TransformGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -102,4 +102,4 @@ func init() {
TransformCmd.SetHelpFunc(help)
TransformCmd.SetUsageFunc(usage)

}
}
6 changes: 3 additions & 3 deletions cmd/cuetils/cmd/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func UpsertRun(code string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.UpsertGlobs(code, globs, flags.RootPflags)
results, err := structural.UpsertGlobs(code, globs, &flags.RootPflags)
if err != nil {
return err
}

err = structural.ProcessOutputs(results, flags.RootPflags)
err = structural.ProcessOutputs(results, &flags.RootPflags)

return err
}
Expand Down Expand Up @@ -102,4 +102,4 @@ func init() {
UpsertCmd.SetHelpFunc(help)
UpsertCmd.SetUsageFunc(usage)

}
}
4 changes: 2 additions & 2 deletions cmd/cuetils/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ValidateRun(schema string, globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.ValidateGlobs(schema, globs, flags.RootPflags)
results, err := structural.ValidateGlobs(schema, globs, &flags.RootPflags)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,4 +108,4 @@ func init() {
ValidateCmd.SetHelpFunc(help)
ValidateCmd.SetUsageFunc(usage)

}
}
2 changes: 1 addition & 1 deletion cmd/cuetils/flags/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type RootPflagpole struct {
Out string
Outname string
Overwrite bool
TypeErrors bool
AllTypeErrors bool
NodeTypeErrors bool
LeafTypeErrors bool
}
Expand Down
2 changes: 1 addition & 1 deletion gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Cli: gen.#HofGenerator & {
Default: "false"
Help: "overwrite files being processed"
},{
Name: "typeErrors"
Name: "allTypeErrors"
Long: "type-errors"
Short: "E"
Type: "bool"
Expand Down
12 changes: 6 additions & 6 deletions structural/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func GetLabel(val cue.Value) cue.Selector {
return s
}

type BinaryOpValueFunc func(lhs, rhs cue.Value) (cue.Value, error)
type BinaryOpValueFunc func(lhs, rhs cue.Value, opts *flags.RootPflagpole) (cue.Value, error)

func BinaryOpGlobs(lhs string, rhs []string, rflags flags.RootPflagpole, fn BinaryOpValueFunc) ([]GlobResult, error) {
func BinaryOpGlobs(lhs string, rhs []string, opts *flags.RootPflagpole, fn BinaryOpValueFunc) ([]GlobResult, error) {
ctx := cuecontext.New()

lv, err := ReadArg(lhs, ctx, nil)
Expand All @@ -78,7 +78,7 @@ func BinaryOpGlobs(lhs string, rhs []string, rflags flags.RootPflagpole, fn Bina
}

// Call our OpValueFunc
v, err := fn(lv.Value, iv)
v, err := fn(lv.Value, iv, opts)
if err != nil {
return nil, err
}
Expand All @@ -92,9 +92,9 @@ func BinaryOpGlobs(lhs string, rhs []string, rflags flags.RootPflagpole, fn Bina
return results, nil
}

type UnaryOpValueFunc func(val cue.Value) (cue.Value, error)
type UnaryOpValueFunc func(val cue.Value, opts *flags.RootPflagpole) (cue.Value, error)

func UnaryOpGlobs(globs []string, rflags flags.RootPflagpole, fn UnaryOpValueFunc) ([]GlobResult, error) {
func UnaryOpGlobs(globs []string, opts *flags.RootPflagpole, fn UnaryOpValueFunc) ([]GlobResult, error) {
ctx := cuecontext.New()

vals, err := LoadGlobs(globs)
Expand All @@ -114,7 +114,7 @@ func UnaryOpGlobs(globs []string, rflags flags.RootPflagpole, fn UnaryOpValueFun
}

// Call our OpValueFunc
v, err := fn(iv)
v, err := fn(iv, opts)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion structural/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CountResult struct {
Count int
}

func CountGlobs(globs []string, rflags flags.RootPflagpole) ([]CountResult, error) {
func CountGlobs(globs []string, opts *flags.RootPflagpole) ([]CountResult, error) {
// no globs, then stdin
if len(globs) == 0 {
globs = []string{"-"}
Expand Down
2 changes: 1 addition & 1 deletion structural/depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DepthResult struct {
Depth int
}

func DepthGlobs(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {
func DepthGlobs(globs []string, opts *flags.RootPflagpole) ([]DepthResult, error) {
// no globs, then stdin
if len(globs) == 0 {
globs = []string{"-"}
Expand Down
18 changes: 9 additions & 9 deletions structural/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ val: #Y: _
diff: val.diff
`

func DiffGlobs(orig string, next string, rflags flags.RootPflagpole) ([]GlobResult, error) {
return DiffGlobsCue(orig, next, rflags)
func DiffGlobs(orig string, next string, opts *flags.RootPflagpole) ([]GlobResult, error) {
return DiffGlobsCue(orig, next, opts)
}

func DiffGlobsGo(orig string, next string, rflags flags.RootPflagpole) ([]GlobResult, error) {
return BinaryOpGlobs(orig, []string{next}, rflags, DiffValues)
func DiffGlobsGo(orig string, next string, opts *flags.RootPflagpole) ([]GlobResult, error) {
return BinaryOpGlobs(orig, []string{next}, opts, DiffValues)
}

func DiffValues(orig, next cue.Value) (cue.Value, error) {
r, _ := diffValues(orig, next)
func DiffValues(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, error) {
r, _ := diffValues(orig, next, opts)
return r, nil
}

func diffValues(orig, next cue.Value) (cue.Value, bool) {
func diffValues(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, bool) {
return orig, false
}

func DiffGlobsCue(orig string, next string, rflags flags.RootPflagpole) ([]GlobResult, error) {
func DiffGlobsCue(orig string, next string, opts *flags.RootPflagpole) ([]GlobResult, error) {
cuest, err := NewCuest([]string{"diff"}, nil)
if err != nil {
return nil, err
Expand All @@ -50,7 +50,7 @@ func DiffGlobsCue(orig string, next string, rflags flags.RootPflagpole) ([]GlobR

// construct reusable val with function
maxiter := ""
if mi := rflags.Maxiter; mi > 0 {
if mi := opts.Maxiter; mi > 0 {
maxiter = fmt.Sprintf(" & { #maxiter: %d }", mi)
}
content := fmt.Sprintf(difffmt, maxiter)
Expand Down
16 changes: 16 additions & 0 deletions structural/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"golang.org/x/text/language"
"golang.org/x/text/message"
Expand Down Expand Up @@ -42,3 +43,18 @@ func FormatCueError(err error) string {
s := w.String()
return s
}

func getErrorAttrMsg(val cue.Value) (string, bool) {
msg, has := "", false
attr := val.Attribute("error")
if attr.Err() == nil {
has = true
if attr.NumArgs() > 0 {
m, _ := attr.String(0)
if m != "" {
msg = m
}
}
}
return msg, has
}
Loading

0 comments on commit bd4c8b2

Please sign in to comment.