-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add count helper; change basic->multi; fix depth for lists
Signed-off-by: Tony Worm <tony@hofstadter.io>
- Loading branch information
Showing
16 changed files
with
412 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.