Skip to content

Commit

Permalink
add count helper; change basic->multi; fix depth for lists
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Oct 9, 2021
1 parent 442fc07 commit 402c2ad
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 8 deletions.
77 changes: 77 additions & 0 deletions .hof/shadow/Cli/cmd/cuetils/cmd/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var countLong = `calculate the node count of a file or glob`

func CountRun(globs []string) (err error) {

// you can safely comment this print out
fmt.Println("not implemented")

return err
}

var CountCmd = &cobra.Command{

Use: "count [globs...]",

Short: "calculate the node count of a file or glob",

Long: countLong,

PreRun: func(cmd *cobra.Command, args []string) {

},

Run: func(cmd *cobra.Command, args []string) {
var err error

// Argument Parsing

var globs []string

if 0 < len(args) {

globs = args[0:]

}

err = CountRun(globs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
extra := func(cmd *cobra.Command) bool {

return false
}

ohelp := CountCmd.HelpFunc()
ousage := CountCmd.UsageFunc()
help := func(cmd *cobra.Command, args []string) {
if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
}
return ousage(cmd)
}

CountCmd.SetHelpFunc(help)
CountCmd.SetUsageFunc(usage)

}
32 changes: 32 additions & 0 deletions .hof/shadow/Cli/cmd/cuetils/cmd/count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd_test

import (
"testing"

"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/script/runtime"

"github.com/hofstadter-io/cuetils/cmd/cuetils/cmd"
)

func TestScriptCountCliTests(t *testing.T) {
// setup some directories

dir := "count"

workdir := ".workdir/cli/" + dir
yagu.Mkdir(workdir)

runtime.Run(t, runtime.Params{
Setup: func(env *runtime.Env) error {
// add any environment variables for your tests here

return nil
},
Funcs: map[string]func(ts *runtime.Script, args []string) error{
"__cuetils": cmd.CallTS,
},
Dir: "hls/cli/count",
WorkdirRoot: workdir,
})
}
7 changes: 7 additions & 0 deletions .hof/shadow/Cli/cmd/cuetils/cmd/hls/cli/count/help.hls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Test "count --help" prints help
call __cuetils count --help

### Test "count -h" prints help
call __cuetils count -h


1 change: 1 addition & 0 deletions .hof/shadow/Cli/cmd/cuetils/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func RootInit() {

RootCmd.AddCommand(CompletionCmd)

RootCmd.AddCommand(CountCmd)
RootCmd.AddCommand(DepthCmd)
RootCmd.AddCommand(DiffCmd)
RootCmd.AddCommand(PatchCmd)
Expand Down
88 changes: 88 additions & 0 deletions cmd/cuetils/cmd/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/hofstadter-io/cuetils/pkg/structural"
)

var countLong = `calculate the node count of a file or glob`

func CountRun(globs []string) (err error) {

// you can safely comment this print out
// fmt.Println("not implemented")

counts, err := structural.Count(globs)
if err != nil {
return err
}

for _, c := range counts {
fmt.Println(c.Filename, c.Count)
}

return err
}

var CountCmd = &cobra.Command{

Use: "count [globs...]",

Short: "calculate the node count of a file or glob",

Long: countLong,

PreRun: func(cmd *cobra.Command, args []string) {

},

Run: func(cmd *cobra.Command, args []string) {
var err error

// Argument Parsing

var globs []string

if 0 < len(args) {

globs = args[0:]

}

err = CountRun(globs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
extra := func(cmd *cobra.Command) bool {

return false
}

ohelp := CountCmd.HelpFunc()
ousage := CountCmd.UsageFunc()
help := func(cmd *cobra.Command, args []string) {
if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
}
return ousage(cmd)
}

CountCmd.SetHelpFunc(help)
CountCmd.SetUsageFunc(usage)

}
32 changes: 32 additions & 0 deletions cmd/cuetils/cmd/count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd_test

import (
"testing"

"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/script/runtime"

"github.com/hofstadter-io/cuetils/cmd/cuetils/cmd"
)

func TestScriptCountCliTests(t *testing.T) {
// setup some directories

dir := "count"

workdir := ".workdir/cli/" + dir
yagu.Mkdir(workdir)

runtime.Run(t, runtime.Params{
Setup: func(env *runtime.Env) error {
// add any environment variables for your tests here

return nil
},
Funcs: map[string]func(ts *runtime.Script, args []string) error{
"__cuetils": cmd.CallTS,
},
Dir: "hls/cli/count",
WorkdirRoot: workdir,
})
}
7 changes: 7 additions & 0 deletions cmd/cuetils/cmd/hls/cli/count/help.hls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Test "count --help" prints help
call __cuetils count --help

### Test "count -h" prints help
call __cuetils count -h


1 change: 1 addition & 0 deletions cmd/cuetils/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func RootInit() {

RootCmd.AddCommand(CompletionCmd)

RootCmd.AddCommand(CountCmd)
RootCmd.AddCommand(DepthCmd)
RootCmd.AddCommand(DiffCmd)
RootCmd.AddCommand(PatchCmd)
Expand Down
15 changes: 15 additions & 0 deletions gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Cli: gen.#HofGenerator & {
OmitRun: true

Commands: [
#CountCommand,
#DepthCommand,
#DiffCommand,
#PatchCommand,
Expand Down Expand Up @@ -67,6 +68,20 @@ Cli: gen.#HofGenerator & {
EnablePProf: true
}

#CountCommand: schema.#Command & {
Name: "count"
Usage: "count [globs...]"
Short: "calculate the node count of a file or glob"
Long: Short

Args: [{
Name: "globs"
Type: "[]string"
Help: "file glob to the operation"
Rest: true
}]
}

#DepthCommand: schema.#Command & {
Name: "depth"
Usage: "depth [globs...]"
Expand Down
72 changes: 72 additions & 0 deletions pkg/structural/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package structural

import (
"fmt"

"cuelang.org/go/cue"

"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
)

type CountResult struct {
Filename string
Count int
}

const countfmt = `
val: #Count%s
val: #in: _
count: val.count
`

func Count(globs []string) ([]CountResult, error) {
// no globs, then stdin
if len(globs) == 0 {
globs = []string{"-"}
}

inputs, err := LoadInputs(globs)
if len(inputs) == 0 {
return nil, fmt.Errorf("no matches found")
}

cuest, err := NewCuest("count")
if err != nil {
return nil, err
}

// construct reusable val with function
maxiter := ""
if mi := flags.RootPflags.Maxiter; mi > 0 {
maxiter = fmt.Sprintf(" & { #maxiter: %d }", mi)
}
content := fmt.Sprintf(countfmt, maxiter)
val := cuest.ctx.CompileString(content, cue.Scope(cuest.orig))

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

// need to handle encodings here

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

result := val.FillPath(cue.ParsePath("val.#in"), iv)

dv := result.LookupPath(cue.ParsePath("count"))
di, err := dv.Int64()
if err != nil {
return nil, err
}

counts = append(counts, CountResult{
Filename: input.Filename,
Count: int(di),
})

}

return counts, nil
}
2 changes: 1 addition & 1 deletion pkg/structural/depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type DepthResult struct {
const depthfmt = `
val: #Depth%s
val: #in: _
depth: val.out
depth: val.depth
`

func Depth(globs []string) ([]DepthResult, error) {
Expand Down
Loading

0 comments on commit 402c2ad

Please sign in to comment.