This is an automated email from the ASF dual-hosted git repository. msciabarra pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/openserverless-cli.git
commit 224feaaa4c4ecd8435b33c0dceff8bb74bdc6778 Author: Michele Sciabarra <[email protected]> AuthorDate: Thu Aug 1 13:10:23 2024 +0200 almost all fixed --- go.mod | 2 +- tests/auto_setup.bats | 10 +++--- tests/plugin.bats | 6 ++-- tests/update.bats | 2 +- tools/sh.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/tools.go | 20 ++++++++++- 6 files changed, 123 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 14d9b33..f9561ae 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/nuvolaris/jj v0.0.0-20231020143327-10b90948a5d2 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/sciabarracom/openwhisk-cli v1.2.1 + github.com/sciabarracom/sh/v3 v3.8.2 github.com/sciabarracom/task/v3 v3.38.9 github.com/stretchr/testify v1.9.0 github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 @@ -84,7 +85,6 @@ require ( github.com/rivo/uniseg v0.4.7 // indirect github.com/sajari/fuzzy v1.0.0 // indirect github.com/sciabarracom/openwhisk-wskdeploy v1.2.1 // indirect - github.com/sciabarracom/sh/v3 v3.8.2 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect github.com/spf13/cobra v1.1.3 // indirect diff --git a/tests/auto_setup.bats b/tests/auto_setup.bats index 9982025..173394f 100644 --- a/tests/auto_setup.bats +++ b/tests/auto_setup.bats @@ -26,7 +26,7 @@ setup() { run rm -rf ~/.nuv run nuv assert_success - assert_output --partial "Welcome to nuv! Setting up..." + assert_output --partial "Welcome to ops! Setting up..." run ls ~/.nuv assert_success } @@ -36,17 +36,17 @@ setup() { export NUV_BRANCH=wrong run nuv assert_failure - assert_output --partial "Welcome to nuv! Setting up..." + assert_output --partial "Welcome to ops! Setting up..." assert_output --partial "failed to clone olaris on branch 'wrong'" } @test "correct branch setups" { run rm -rf ~/.nuv - export NUV_BRANCH=0.1.0-testing + export NUV_BRANCH=0.1.0 run nuv assert_success - assert_output --partial "Welcome to nuv! Setting up..." + assert_output --partial "Welcome to ops! Setting up..." run ls ~/.nuv assert_success - assert_output --partial "0.1.0-testing" + assert_output --partial "0.1.0" } diff --git a/tests/plugin.bats b/tests/plugin.bats index fe5c23d..aa39414 100644 --- a/tests/plugin.bats +++ b/tests/plugin.bats @@ -20,6 +20,7 @@ setup() { load 'test_helper/bats-assert/load' export NO_COLOR=1 export NUV_BRANCH="$(cat ../branch.txt)" + run rm -rf ~/.nuv } @test "nuv prints 'Plugins:'" { @@ -63,7 +64,6 @@ setup() { run nuv assert_line 'Plugins:' assert_line " other (local)" - run rm -rf ~/.nuv/olaris } @test "other sub simple prints simple" { @@ -71,7 +71,6 @@ setup() { run nuv -update run nuv other sub simple assert_line 'simple' - run rm -rf ~/.nuv/olaris } @test "other tool runs nuv tool" { @@ -79,7 +78,7 @@ setup() { run nuv -update run nuv other tool assert_line 'hello' - run rm -rf ~/.nuv/olaris + run rm -rf ~/.nuv } @test "other command runs nuv command" { @@ -87,7 +86,6 @@ setup() { run nuv -update run nuv other command assert_line --partial 'nothing installed yet' - run rm -rf ~/.nuv/olaris } # Plugin Tool Tests diff --git a/tests/update.bats b/tests/update.bats index 780dcaa..a0721f0 100644 --- a/tests/update.bats +++ b/tests/update.bats @@ -49,7 +49,7 @@ setup() { @test "nuv -update on branch" { run rm -rf ~/.nuv - export NUV_BRANCH=0.1.0-testing + export NUV_BRANCH=0.1.0 run nuv -update assert_line "Nuvfiles downloaded successfully" assert_success diff --git a/tools/sh.go b/tools/sh.go new file mode 100644 index 0000000..540b6b3 --- /dev/null +++ b/tools/sh.go @@ -0,0 +1,95 @@ +// Copyright (c) 2017, Daniel Martà <[email protected]> +// See LICENSE for licensing information + +// gosh is a proof of concept shell built on top of [interp]. +package tools + +import ( + "context" + "fmt" + "io" + "os" + + "golang.org/x/term" + + "github.com/sciabarracom/sh/v3/interp" + "github.com/sciabarracom/sh/v3/syntax" +) + +func Sh() (int, error) { + path := "" + if len(os.Args) > 1 { + path = os.Args[1] + if path == "-h" || path == "--help" { + fmt.Println("Sh is the mvdan shell using the ops environment.\nUsage: [<script>|-h|--help]\nWithout args starts an interactive shell otherwise execute the script.") + return 0, nil + } + } + err := runAll(path) + if e, ok := interp.IsExitStatus(err); ok { + return int(e), err + } + if err != nil { + return 1, err + } + return 0, nil +} + +func runAll(path string) error { + r, err := interp.New(interp.StdIO(os.Stdin, os.Stdout, os.Stderr)) + if err != nil { + return err + } + + if path != "" { + return runPath(r, path) + } + if term.IsTerminal(int(os.Stdin.Fd())) { + return runInteractive(r, os.Stdin, os.Stdout, os.Stderr) + } + return run(r, os.Stdin, "") +} + +func run(r *interp.Runner, reader io.Reader, name string) error { + prog, err := syntax.NewParser().Parse(reader, name) + if err != nil { + return err + } + r.Reset() + ctx := context.Background() + return r.Run(ctx, prog) +} + +func runPath(r *interp.Runner, path string) error { + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + return run(r, f, path) +} + +func runInteractive(r *interp.Runner, stdin io.Reader, stdout, stderr io.Writer) error { + parser := syntax.NewParser() + fmt.Fprintf(stdout, "@ ") + var runErr error + fn := func(stmts []*syntax.Stmt) bool { + if parser.Incomplete() { + fmt.Fprintf(stdout, "> ") + return true + } + ctx := context.Background() + for _, stmt := range stmts { + runErr = r.Run(ctx, stmt) + if r.Exited() { + return false + } + } + fmt.Fprintf(stdout, "$ ") + return true + } + if err := parser.Interactive(stdin, fn); err != nil { + return err + } + return runErr +} diff --git a/tools/tools.go b/tools/tools.go index 364e7f3..204b7fb 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -42,7 +42,7 @@ func trace(args ...any) { // available in taskfiles // note some of them are implemented in main.go (config, retry) var ToolList = []string{ - "wsk", "awk", "jq", + "wsk", "awk", "jq", "sh", "envsubst", "filetype", "random", "datefmt", "die", "urlenc", "replace", "base64", "validate", "echoif", "echoifempty", "echoifexists", @@ -67,67 +67,85 @@ func RunTool(name string, args []string) (int, error) { if err := Wsk(cmd); err != nil { return 1, err } + case "awk": // fmt.Println("=== awk ===") os.Args = append([]string{"goawk"}, args...) if err := goawk.AwkMain(); err != nil { return 1, err } + case "jq": os.Args = append([]string{"gojq"}, args...) return gojq.Run(), nil + + case "sh": + os.Args = append([]string{"sh"}, args...) + return Sh() + case "envsubst": os.Args = append([]string{"envsubst"}, args...) if err := envsubst.EnvsubstMain(); err != nil { return 1, err } + case "filetype": os.Args = append([]string{"mkdir"}, args...) if err := Filetype(); err != nil { return 1, err } + case "random": if err := RandTool(args...); err != nil { return 1, err } + case "datefmt": os.Args = append([]string{"datefmt"}, args...) if err := DateFmtTool(); err != nil { return 1, err } + case "die": if len(args) > 0 { fmt.Println(strings.Join(args, " ")) } return 1, nil + case "urlenc": os.Args = append([]string{"urlenc"}, args...) if err := URLEncTool(); err != nil { return 1, err } + case "replace": os.Args = append([]string{"replace"}, args...) return replace.ReplaceMain() + case "base64": os.Args = append([]string{"base64"}, args...) if err := base64Tool(); err != nil { return 1, err } + case "validate": os.Args = append([]string{"validate"}, args...) if err := validateTool(); err != nil { return 1, err } + case "echoif": os.Args = append([]string{"echoif"}, args...) if err := echoIfTool(); err != nil { return 1, err } + case "echoifempty": os.Args = append([]string{"echoifempty"}, args...) if err := echoIfEmptyTool(); err != nil { return 1, err } + case "echoifexists": os.Args = append([]string{"echoifexists"}, args...) if err := echoIfExistsTool(); err != nil {
