Skip to content

Commit

Permalink
proxy st ops and update cli to use
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Nov 17, 2021
1 parent d21b59f commit 2c51178
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 101 deletions.
4 changes: 2 additions & 2 deletions 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.Count(globs, flags.RootPflags)
counts, err := structural.CountGlobs(globs, flags.RootPflags)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,4 +86,4 @@ func init() {
CountCmd.SetHelpFunc(help)
CountCmd.SetUsageFunc(usage)

}
}
4 changes: 2 additions & 2 deletions 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.Depth(globs, flags.RootPflags)
depths, err := structural.DepthGlobs(globs, flags.RootPflags)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,4 +86,4 @@ func init() {
DepthCmd.SetHelpFunc(help)
DepthCmd.SetUsageFunc(usage)

}
}
2 changes: 1 addition & 1 deletion cmd/cuetils/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func DiffRun(orig string, next string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

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

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

results, err := structural.Mask(code, globs, flags.RootPflags)
results, err := structural.MaskGlobs(code, globs, flags.RootPflags)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cuetils/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func PatchRun(patch string, orig string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")

results, err := structural.Patch(patch, orig, flags.RootPflags)
results, err := structural.PatchGlobs(patch, orig, flags.RootPflags)
if err != nil {
return err
}
Expand Down Expand Up @@ -104,4 +104,4 @@ func init() {
PatchCmd.SetHelpFunc(help)
PatchCmd.SetUsageFunc(usage)

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

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

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

results, err := structural.Upsert(code, globs, flags.RootPflags)
results, err := structural.UpsertGlobs(code, globs, flags.RootPflags)
if err != nil {
return err
}
Expand Down
48 changes: 25 additions & 23 deletions 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 Count(globs []string, rflags flags.RootPflagpole) ([]CountResult, error) {
func CountGlobs(globs []string, rflags flags.RootPflagpole) ([]CountResult, error) {
// no globs, then stdin
if len(globs) == 0 {
globs = []string{"-"}
Expand All @@ -34,33 +34,11 @@ func Count(globs []string, rflags flags.RootPflagpole) ([]CountResult, error) {
return nil, fmt.Errorf("no matches found")
}

counter := func(val cue.Value) int {
sum := 0
after := func(v cue.Value) {
switch v.IncompleteKind() {
case cue.StructKind:
s, _ := v.Fields(defaultWalkOptions...)
for s.Next() {
sum += 1
}
case cue.ListKind:
// nothing
default:
sum += 1
}
}

Walk(val, nil, after)
return sum
}

ctx := cuecontext.New()

counts := make([]CountResult, 0)
for _, input := range inputs {

// need to handle encodings here?

iv := ctx.CompileBytes(input.Content, cue.Filename(input.Filename))
if iv.Err() != nil {
return nil, iv.Err()
Expand All @@ -77,3 +55,27 @@ func Count(globs []string, rflags flags.RootPflagpole) ([]CountResult, error) {

return counts, nil
}

func CountValue(val cue.Value) int {
return counter(val)
}

func counter(val cue.Value) int {
sum := 0
after := func(v cue.Value) {
switch v.IncompleteKind() {
case cue.StructKind:
s, _ := v.Fields(defaultWalkOptions...)
for s.Next() {
sum += 1
}
case cue.ListKind:
// nothing
default:
sum += 1
}
}

Walk(val, nil, after)
return sum
}
78 changes: 41 additions & 37 deletions 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 Depth(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {
func DepthGlobs(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {
// no globs, then stdin
if len(globs) == 0 {
globs = []string{"-"}
Expand All @@ -28,41 +28,6 @@ func Depth(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {
return nil, fmt.Errorf("no matches found")
}

depther := func(val cue.Value) int {
var max, depth int

// increase depth, check against max
before := func(v cue.Value) bool {
switch v.IncompleteKind() {
case cue.StructKind:
depth += 1
case cue.ListKind:
// nothing
default:
depth += 1
}

if depth > max {
max = depth
}
return true
}
// decrease depth after
after := func(v cue.Value) {
switch v.IncompleteKind() {
case cue.StructKind:
depth -= 1
case cue.ListKind:
// nothing
default:
depth -= 1
}
}

Walk(val, before, after)
return max
}

ctx := cuecontext.New()

depths := make([]DepthResult, 0)
Expand All @@ -75,7 +40,7 @@ func Depth(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {
return nil, iv.Err()
}

d := depther(iv)
d := depth(iv)

depths = append(depths, DepthResult{
Filename: input.Filename,
Expand All @@ -86,3 +51,42 @@ func Depth(globs []string, rflags flags.RootPflagpole) ([]DepthResult, error) {

return depths, nil
}

func DepthValue(val cue.Value) int {
return depth(val)
}

func depth(val cue.Value) int {
var max, curr int

// increase curr, check against max
before := func(v cue.Value) bool {
switch v.IncompleteKind() {
case cue.StructKind:
curr += 1
case cue.ListKind:
// nothing
default:
curr += 1
}

if curr > max {
max = curr
}
return true
}
// decrease curr after
after := func(v cue.Value) {
switch v.IncompleteKind() {
case cue.StructKind:
curr -= 1
case cue.ListKind:
// nothing
default:
curr -= 1
}
}

Walk(val, before, after)
return max
}
50 changes: 49 additions & 1 deletion structural/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"

"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
)
Expand All @@ -15,7 +16,54 @@ val: #Y: _
diff: val.diff
`

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

func DiffGlobsGo(orig string, next string, rflags flags.RootPflagpole) ([]GlobResult, error) {
ctx := cuecontext.New()

ov, err := ReadArg(orig, rflags.Load, ctx, nil)
if err != nil {
return nil, err
}

inputs, err := LoadGlobs([]string{next})
if len(inputs) == 0 {
return nil, fmt.Errorf("no inputs found")
}

results := make([]GlobResult, 0)
for _, input := range inputs {

iv := ctx.CompileBytes(input.Content, cue.Filename(input.Filename))
if iv.Err() != nil {
return nil, iv.Err()
}

v, err := DiffValues(ov.Value, iv)
if err != nil {
return nil, err
}

results = append(results, GlobResult{
Filename: input.Filename,
Value: v,
})
}

return results, nil
}

func DiffValues(orig, next cue.Value) (cue.Value, error) {
return diffValues(orig, next)
}

func diffValues(orig, next cue.Value) (cue.Value, error) {
return orig, nil
}

func DiffGlobsCue(orig string, next string, rflags flags.RootPflagpole) ([]GlobResult, error) {
cuest, err := NewCuest([]string{"diff"}, nil)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion structural/extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ val: #E: _
extend: val.extend
`

func Extend(code string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
func ExtendGlobs(code string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
return ExtendGlobsCue(code, globs, rflags)
}

func ExtendGlobsGo(code string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
return nil, nil
}

func ExtendGlobsCue(code string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
cuest, err := NewCuest([]string{"extend"}, nil)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion structural/mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ val: #M: _
mask: val.mask
`

func Mask(mask string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
// MaskGlobs will mask a subobject from globs on disk
func MaskGlobs(mask string, globs []string, rflags flags.RootPflagpole) ([]GlobResult, error) {
return MaskGlobsCue(mask, globs, rflags)
}

Expand Down
9 changes: 5 additions & 4 deletions structural/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ type GlobResult struct {
Filename string
Content string
Value cue.Value
Error error
}

func ProcessOutputs(results []GlobResult, rflags flags.RootPflagpole) (err error) {
//if rflags.Accum != "" {
//results, err = AccumOutputs(results, rflags.Accum)
//if err != nil {
//return err
//}
//results, err = AccumOutputs(results, rflags.Accum)
//if err != nil {
//return err
//}
//}
w := os.Stdout
for _, r := range results {
Expand Down
Loading

0 comments on commit 2c51178

Please sign in to comment.