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 c71a91c621ee18bed9f2e68e1295fd924648eafa Author: Michele Sciabarra <[email protected]> AuthorDate: Tue Aug 6 23:11:47 2024 +0200 reworked main help menu --- _patches/patch-task.sh | 14 ++++-- _patches/task | 2 +- auth/login.go | 2 +- go.mod | 2 +- go.sum | 2 + main.go | 117 +++++++++++++++++++++++++------------------------ tests/mvrmem.bats | 68 ---------------------------- tools/tools.go | 7 +-- 8 files changed, 76 insertions(+), 138 deletions(-) diff --git a/_patches/patch-task.sh b/_patches/patch-task.sh index b6b1b4a..54a9be8 100644 --- a/_patches/patch-task.sh +++ b/_patches/patch-task.sh @@ -21,18 +21,22 @@ cd "$(dirname $0)" HERE=$PWD SHVER=$(git ls-remote https://github.com/sciabarracom/sh | awk '/refs\/heads\/openserverless/{print $1}') STAG="v3.38.0" -DTAG="v3.38.10" +DTAG="v3.38.11" cd task git reset --hard go clean -cache -modcache git checkout "$STAG" -B openserverless mkdir -p cmd/taskmain + +sed -i -e 's/func init/func FlagInit/' internal/flags/flags.go +sed -i -e 's/func init/func FlagInit/' internal/experiments/experiments.go + cat cmd/task/task.go \ | sed -e 's/package main/package taskmain/' \ | sed -e 's/func main()/func _main()/' \ | tee cmd/taskmain/task.go -sed -i -e 's/func init/func FlagInit/' internal/flags/flags.go -cat <<EOF >>cmd/taskmain/task.go + +cat <<EOF >>cmd/taskmain/task.go var todoFlagInit = true @@ -40,6 +44,7 @@ func Task(_args []string) (int, error) { os.Args = _args if todoFlagInit { flags.FlagInit() + experiments.FlagInit() todoFlagInit = false } if err := run(); err != nil { @@ -77,7 +82,8 @@ sed -i -e '/mvdan.cc/g' go.mod go get github.com/sciabarracom/sh/v3@$SHVER go mod tidy git commit -m "patching sh for ops" -a -go build +go build + git tag $DTAG git push origin-auth openserverless -f --tags VER=$(git rev-parse HEAD) diff --git a/_patches/task b/_patches/task index 2c3fdce..c14dc1e 160000 --- a/_patches/task +++ b/_patches/task @@ -1 +1 @@ -Subproject commit 2c3fdcebad1da59613ec3e30d6fb96fac35e167c +Subproject commit c14dc1e4fdb5203df0a57db124d5ccb75dd47832 diff --git a/auth/login.go b/auth/login.go index 5006fd4..b7864dd 100644 --- a/auth/login.go +++ b/auth/login.go @@ -43,7 +43,7 @@ type LoginResult struct { const usage = `Usage: ops login <apihost> [<user>] -Login to an OpenServerless instance. If no user is specified, the default user "opsolaris" is used. +Login to an OpenServerless instance. If no user is specified, the default user "nuvolaris" is used. You can set the environment variables OPS_APIHOST and OPS_USER to avoid specifying them on the command line. You can set OPS_PASSWORD to avoid entering the password interactively. diff --git a/go.mod b/go.mod index 0a81b76..62b0d83 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/sciabarracom/openwhisk-cli v1.2.1 github.com/sciabarracom/sh/v3 v3.8.3 - github.com/sciabarracom/task/v3 v3.38.10 + github.com/sciabarracom/task/v3 v3.38.11 github.com/stretchr/testify v1.9.0 github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 github.com/zalando/go-keyring v0.2.5 diff --git a/go.sum b/go.sum index 6f1a296..d55390a 100644 --- a/go.sum +++ b/go.sum @@ -347,6 +347,8 @@ github.com/sciabarracom/sh/v3 v3.8.3 h1:5afHdZzNkW5vgUMP7gHUpBYHezn3BoQsyNgKXKdT github.com/sciabarracom/sh/v3 v3.8.3/go.mod h1:CeMVr3PRLhlUrS+VPLTp8dS5/6xNzmmX0h/WVHRR6DA= github.com/sciabarracom/task/v3 v3.38.10 h1:KSu6B2CuHWail7rlTx5W72qM1klJbED3z2k7RzBvMkE= github.com/sciabarracom/task/v3 v3.38.10/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic= +github.com/sciabarracom/task/v3 v3.38.11 h1:TZeaepSVc9Lx7TowAqIV3K9IZQh+jYgKNDpBNIg38uU= +github.com/sciabarracom/task/v3 v3.38.11/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= diff --git a/main.go b/main.go index ad9910d..2f3e1cc 100644 --- a/main.go +++ b/main.go @@ -23,13 +23,13 @@ import ( "os" "os/exec" "path/filepath" - "slices" "strings" "time" "github.com/apache/openserverless-cli/auth" "github.com/apache/openserverless-cli/config" "github.com/apache/openserverless-cli/tools" + "golang.org/x/exp/slices" "github.com/mitchellh/go-homedir" @@ -116,23 +116,22 @@ func InfoAndExit(args []string) { if len(args) < 2 { fmt.Println("Welcome to ops, the all-mighty, extensibile apache OPenServerless CLI Tool.") fmt.Println("Let's start with the basics:") - fmt.Println("-v | --version version (mention this when you ask for help)") - fmt.Println("-h | --help help (the manuale is here)") - fmt.Println("-u | --update update (get the latest version of everything)") - fmt.Println("-i | --info CLI infos (let's check the CLI)") - fmt.Println("-c | --config manage config (let's check the server configuration)") - fmt.Println("-l | --login login (you have to login first, you know)") - fmt.Println("--i-really-want-to-reset (if you are desperate and nothing works)") - fmt.Println("But wait! There is more, much much more. Please RTFM.") + fmt.Println("-h | -help list commands (top level command, start here)") + fmt.Println("-t | -tools list tools (embedded tools, prefixed by '-')") + fmt.Println("-v | -version current version (mention this when you ask for help)") + fmt.Println("-i | -info CLI infos (let's check the CLI)") + fmt.Println("-u | -update download latest (get the latest commands and prerequisites)") + fmt.Println("-l | -login access system (you have to login first)") + fmt.Println("-c | -config manage config (server configuration)") + fmt.Println("-reset clean downloads (if nothing works, try this)") os.Exit(0) } // if we have at least one arg switch args[1] { - case "-v", "--version": + case "-v", "-version": fmt.Println(OpsVersion) os.Exit(0) - - case "-h", "--help": + case "-t", "-tools": tools.Help(mainTools) os.Exit(0) default: @@ -145,7 +144,7 @@ func executeTools(cmd string, args []string, opsHome string) int { switch cmd { case "", "task": - exitCode, err := Task(args[2:]...) + exitCode, err := Task(args...) if err != nil { log.Println(err) } @@ -198,7 +197,7 @@ func executeTools(cmd string, args []string, opsHome string) int { } return 0 - case "i-really-want-to-reset", "reset": + case "reset": home := os.Getenv("OPS_HOME") if home == "" { log.Fatal("cannot determine the ops home dir") @@ -222,7 +221,7 @@ func executeTools(cmd string, args []string, opsHome string) int { if err != nil { log.Fatal("ops reset error:", err.Error()) } - fmt.Println("ops reset complete") + fmt.Println("ops -reset complete - execute ops -update to reload") return 0 case "retry": @@ -258,7 +257,7 @@ func executeTools(cmd string, args []string, opsHome string) int { warn("unknown tool", "-"+cmd) return 1 } - return 0 // unreachable - or it should be! + //return 0 // unreachable - or it should be! } func Main() { @@ -277,6 +276,27 @@ func Main() { trace("WSK_RUNTIMES_JSON len=", len(WSK_RUNTIMES_JSON)) } + // in case args[1] is a wsk wrapper command invoke it and exit + // CLI: ops action ... (wsk wrapper) + if len(os.Args) > 1 { + if expand, ok := IsWskWrapperCommand(os.Args[1]); ok { + trace("wsk wrapper command") + debug("extracted cmd", expand) + rest := os.Args[2:] + debug("extracted args", rest) + + // if "invoke" is in the command, parse all a=b into -p a b + if (len(expand) > 2 && expand[2] == "invoke") || slices.Contains(rest, "invoke") { + rest = parseInvokeArgs(rest) + } + + if err := tools.Wsk(expand, rest...); err != nil { + log.Fatalf("error: %s", err.Error()) + } + os.Exit(0) + } + } + // set runtime version as environment variable if os.Getenv("OPS_VERSION") != "" { OpsVersion = os.Getenv("OPS_VERSION") @@ -368,42 +388,23 @@ func Main() { log.Fatalf("failed preflight check: %s", err.Error()) } - // in case args[1] is a wsk wrapper command invoke it and exit - // CLI: ops action ... (wsk wrapper) - if len(os.Args) > 1 { - if expand, ok := IsWskWrapperCommand(os.Args[1]); ok { - trace("wsk wrapper command") - debug("extracted cmd", expand) - rest := os.Args[2:] - debug("extracted args", rest) - - // if "invoke" is in the command, parse all a=b into -p a b - if (len(expand) > 2 && expand[2] == "invoke") || slices.Contains(rest, "invoke") { - rest = parseInvokeArgs(rest) - } - - if err := tools.Wsk(expand, rest...); err != nil { - log.Fatalf("error: %s", err.Error()) - } - os.Exit(0) - } - } - // first argument with prefix "-" is considered an embedded tool // using "-" or "--" or "-task" invokes the embedded task // CLI: ops -<tool> (embedded tool) - if len(os.Args) > 1 && len(os.Args[1]) > 0 && os.Args[1][0] == '-' { - cmd := os.Args[1] - // remove extra hyphen if any - if len(cmd) > 0 && cmd[0] == '-' { - cmd = cmd[1:] + args := os.Args + if len(args) > 1 && len(args[1]) > 0 && args[1][0] == '-' { + cmd := args[1][1:] + if cmd != "h" && cmd != "help" { + // execute the embeded tool and exit + exitCode := executeTools(cmd, args[2:], opsHome) + os.Exit(exitCode) + } else { + // remove -t to show tasks + args = args[1:] } - // execute the embeded tool and exit - exitCode := executeTools(cmd, os.Args[1:], opsHome) - os.Exit(exitCode) } - if err := runOps(opsRootDir, os.Args[1:]); err != nil { + if err := runOps(opsRootDir, args); err != nil { log.Fatalf("task execution error: %s", err.Error()) } } @@ -522,19 +523,19 @@ func buildConfigMap(opsRootPath string, configPath string) (*config.ConfigMap, e return &configMap, nil } -func IsWskWrapperCommand(name string) ([]string, bool) { - wskWrapperCommands := map[string][]string{ - "action": {"wsk", "action"}, - "activation": {"wsk", "activation"}, - "invoke": {"wsk", "action", "invoke", "-r"}, - "logs": {"wsk", "activation", "logs"}, - "package": {"wsk", "package"}, - "result": {"wsk", "activation", "result"}, - "rule": {"wsk", "rule"}, - "trigger": {"wsk", "trigger"}, - "url": {"wsk", "action", "get", "--url"}, - } +var wskWrapperCommands = map[string][]string{ + "action": {"wsk", "action"}, + "activation": {"wsk", "activation"}, + "invoke": {"wsk", "action", "invoke", "-r"}, + "logs": {"wsk", "activation", "logs"}, + "package": {"wsk", "package"}, + "result": {"wsk", "activation", "result"}, + "rule": {"wsk", "rule"}, + "trigger": {"wsk", "trigger"}, + "url": {"wsk", "action", "get", "--url"}, +} +func IsWskWrapperCommand(name string) ([]string, bool) { cmd, ok := wskWrapperCommands[name] return cmd, ok } diff --git a/tests/mvrmem.bats b/tests/mvrmem.bats deleted file mode 100644 index 96896d9..0000000 --- a/tests/mvrmem.bats +++ /dev/null @@ -1,68 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -setup() { - load 'test_helper/bats-support/load' - load 'test_helper/bats-assert/load' - export NO_COLOR=1 - rm -f empty_file -} - -@test "-rename -remove" { - run ops -rename - assert_line "Usage: rename <source> <destination>" - - run ops -rename missing - assert_line "Usage: rename <source> <destination>" - - run ops -rename missing something - assert_failure - assert_line "rename missing something: no such file or directory" - - touch something - run ops -rename something somethingelse - assert_success - assert_line "renamed something -> somethingelse" - - run ops -rename somethingelse /dev/null - assert_failure - assert_line "rename somethingelse /dev/null: invalid cross-device link" - - run ops -remove - assert_line "Usage: remove <filename>" - - run ops -remove missing - assert_failure - assert_line "remove missing: no such file or directory" - - run ops -remove somethingelse - assert_success - assert_line "removed somethingelse" -} - -@test "-empty" { - run ops -empty - assert_line "Usage: filename" - run ops -empty empty_file - assert test -f empty_file - assert_success - run ops -empty empty_file - assert_failure - assert_line "file already exists" - rm empty_file - -} \ No newline at end of file diff --git a/tools/tools.go b/tools/tools.go index d1d9303..397cea8 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -170,11 +170,11 @@ func RunTool(name string, args []string) (int, error) { return Rename() case "remove": - os.Args = append([]string{"rename"}, args...) + os.Args = append([]string{"remove"}, args...) return Remove() case "extract": - os.Args = append([]string{"untar"}, args...) + os.Args = append([]string{"extract"}, args...) return Extract() case "empty": @@ -188,9 +188,6 @@ func RunTool(name string, args []string) (int, error) { } func Help(mainTools []string) { - fmt.Println("Infos:") - fmt.Println("-v | --version") - fmt.Println("-h | --help") fmt.Println("Tools (use -<tool> -h for help):") availableTools := append(mainTools, ToolList...) slices.Sort(availableTools)
