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 0a08f7f536954d788b3a8090772b497d65c9517f Author: Michele Sciabarra <[email protected]> AuthorDate: Tue Aug 6 10:56:56 2024 +0200 change: reworking documentation --- Taskfile.yml | 13 +++- install.sh | 11 ++-- main.go | 184 +++++++++++++++++++++++++++++++++++---------------------- tools/tools.go | 5 +- 4 files changed, 133 insertions(+), 80 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 55cfc4f..3642490 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -73,11 +73,20 @@ tasks: - dlv exec ./ops -- {{.CLI_ARGS}} install: - desc: install ops in usr/local/bin + desc: install ops in ~./local/bin deps: - build cmds: - - sudo cp ops /usr/local/bin + - cp ops $HOME/.local/bin + - | + if ! which ops | grep $HOME/.local/bin + then + echo "$HOME/.local/bin is not in the path - adding it" + echo 'export PATH="$HOME/.local/bin:$PATH"' >>$HOME/.bashrc + echo 'export PATH="$HOME/.local/bin:$PATH"' >>$HOME/.zshrc + echo please restart your terminal to find ops in your path + fi + tests: desc: all tests diff --git a/install.sh b/install.sh index d213583..f611d54 100644 --- a/install.sh +++ b/install.sh @@ -53,11 +53,12 @@ then echo "cannot install ops - download and unpack it in a folder in the path f exit 1 fi -if ! which ops +if ! which ops | grep $HOME/.local/bin then - echo "$HOME/.local/bin is not in the path - adding it" - echo 'export PATH=~/.local/bin:"$PATH"' >>~/.bashrc - echo 'export PATH=~/.local/bin:"$PATH"' >>~/.zshrc - echo please restart your terminal to find ops in your path + echo "$HOME/.local/bin is not in the path - adding it" + echo 'export PATH="$HOME/.local/bin:$PATH"' >>$HOME/.bashrc + echo 'export PATH="$HOME/.local/bin:$PATH"' >>$HOME/.zshrc + echo please restart your terminal to find ops in your path fi + exit 0 \ No newline at end of file diff --git a/main.go b/main.go index 7234d2c..ad9910d 100644 --- a/main.go +++ b/main.go @@ -91,7 +91,7 @@ func setOpsRootPluginEnv() { } func info() { - fmt.Println("OPS_CMD:", os.Getenv("OPS_CMD")) + fmt.Println("OPS & OPS_CMD:", os.Getenv("OPS_CMD")) fmt.Println("OPS_VERSION:", os.Getenv("OPS_VERSION")) fmt.Println("OPS_BRANCH:", os.Getenv("OPS_BRANCH")) fmt.Println("OPS_BIN:", os.Getenv("OPS_BIN")) @@ -106,39 +106,56 @@ func info() { //fmt.Println("OPS_COREUTILS:", os.Getenv("OPS_COREUTILS")) } -// not available in taskfiles var mainTools = []string{ - "task", "version", "info", "help", "serve", "update", "retry", "login", "config", "plugin", "reset", + "task", "info", "update", "login", "config", + "retry", "plugin", "reset", "serve", } -func executeMainToolsAndExit(cmd string, args []string, opsHome string) int { - if cmd == "" || cmd == "-" || cmd == "task" { +// simple tools provide info and exit +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.") + os.Exit(0) + } + // if we have at least one arg + switch args[1] { + case "-v", "--version": + fmt.Println(OpsVersion) + os.Exit(0) + + case "-h", "--help": + tools.Help(mainTools) + os.Exit(0) + default: + return + } +} + +// CLI: ops -<cmd> <args>... +func executeTools(cmd string, args []string, opsHome string) int { + + switch cmd { + case "", "task": exitCode, err := Task(args[2:]...) if err != nil { log.Println(err) } return exitCode - } - - switch cmd { - case "version": - fmt.Println(OpsVersion) - case "v": - fmt.Println(OpsVersion) - case "info": + case "i", "info": info() + return 0 - case "help": - tools.Help(mainTools) - - case "serve": - opsRootDir := getRootDirOrExit() - if err := Serve(opsRootDir, args[1:]); err != nil { - log.Fatalf("error: %v", err) - } - - case "update": + case "u", "update": // ok no up, nor down, let's download it dir, err := pullTasks(true, true) if err != nil { @@ -147,8 +164,41 @@ func executeMainToolsAndExit(cmd string, args []string, opsHome string) int { if err := setOpsOlarisHash(dir); err != nil { log.Fatal("unable to set OPS_OLARIS...", err.Error()) } + return 0 + + case "l", "login": + os.Args = args + loginResult, err := auth.LoginCmd() + if err != nil { + log.Fatalf("error: %s", err.Error()) + } + + if loginResult == nil { + return 1 + } + + fmt.Println("Successfully logged in as " + loginResult.Login + ".") + if err := wskPropertySet(loginResult.ApiHost, loginResult.Auth); err != nil { + log.Fatalf("error: %s", err.Error()) + } + fmt.Println("OpenServerless host and auth set successfully. You are now ready to use ops!") + return 0 - case "reset": + case "c", "config": + os.Args = args + opsRootPath := joinpath(getRootDirOrExit(), OPSROOT) + configPath := joinpath(opsHome, CONFIGFILE) + configMap, err := buildConfigMap(opsRootPath, configPath) + if err != nil { + log.Fatalf("error: %s", err.Error()) + } + + if err := config.ConfigTool(*configMap); err != nil { + log.Fatalf("error: %s", err.Error()) + } + return 0 + + case "i-really-want-to-reset", "reset": home := os.Getenv("OPS_HOME") if home == "" { log.Fatal("cannot determine the ops home dir") @@ -179,47 +229,26 @@ func executeMainToolsAndExit(cmd string, args []string, opsHome string) int { if err := tools.ExpBackoffRetry(args[1:]); err != nil { log.Fatalf("error: %s", err.Error()) } - - case "login": - os.Args = args[1:] - loginResult, err := auth.LoginCmd() - if err != nil { - log.Fatalf("error: %s", err.Error()) - } - - if loginResult == nil { - os.Exit(1) - } - - fmt.Println("Successfully logged in as " + loginResult.Login + ".") - if err := wskPropertySet(loginResult.ApiHost, loginResult.Auth); err != nil { - log.Fatalf("error: %s", err.Error()) - } - fmt.Println("OpenServerless host and auth set successfully. You are now ready to use ops!") - - case "config": - os.Args = args[1:] - opsRootPath := joinpath(getRootDirOrExit(), OPSROOT) - configPath := joinpath(opsHome, CONFIGFILE) - configMap, err := buildConfigMap(opsRootPath, configPath) - if err != nil { - log.Fatalf("error: %s", err.Error()) - } - - if err := config.ConfigTool(*configMap); err != nil { - log.Fatalf("error: %s", err.Error()) - } + return 0 case "plugin": - os.Args = args[1:] + os.Args = args if err := pluginTool(); err != nil { log.Fatalf("error: %s", err.Error()) } + return 0 + + case "serve": + opsRootDir := getRootDirOrExit() + if err := Serve(opsRootDir, args); err != nil { + log.Fatalf("error: %v", err) + } + return 0 default: // check if it is an embedded to and invoke it if tools.IsTool(cmd) { - code, err := tools.RunTool(cmd, args[2:]) + code, err := tools.RunTool(cmd, args[1:]) if err != nil { log.Print(err.Error()) } @@ -227,17 +256,21 @@ func executeMainToolsAndExit(cmd string, args []string, opsHome string) int { } // no embeded tool found warn("unknown tool", "-"+cmd) + return 1 } - return 0 + return 0 // unreachable - or it should be! } func Main() { var err error - args := os.Args // disable log prefix log.SetFlags(0) + // CLI: ops -v | --version | -h | --help + // provide infos without downloading anything + InfoAndExit(os.Args) + // set default runtime.json if os.Getenv("WSK_RUNTIMES_JSON") == "" { os.Setenv("WSK_RUNTIMES_JSON", WSK_RUNTIMES_JSON) @@ -253,7 +286,8 @@ func Main() { } // setup OPS_CMD - me := args[0] + // CLI: ops ... + me := os.Args[0] if strings.Contains("ops ops.exe", filepath.Base(me)) { _, err = setupCmd(me) if err != nil { @@ -307,8 +341,8 @@ func Main() { if err != nil { log.Fatalf("cannot locate or download OPS_ROOT: %s", err.Error()) } - // just updated, do not repeat - if len(args) == 2 && args[1] == "-update" { + // if just updated, do not repeat + if len(os.Args) > 1 && os.Args[1] == "-update" { os.Exit(0) } } else { @@ -316,6 +350,7 @@ func Main() { checkUpdated(opsHome, 24*time.Hour) } } + if err = setOpsOlarisHash(olarisDir); err != nil { os.Setenv("OPS_OLARIS", "<local>") } @@ -334,19 +369,20 @@ func Main() { } // in case args[1] is a wsk wrapper command invoke it and exit - if len(args) > 1 { - if cmd, ok := IsWskWrapperCommand(args[1]); ok { + // 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", cmd) - rest := args[2:] + 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(cmd) > 2 && cmd[2] == "invoke") || slices.Contains(rest, "invoke") { + if (len(expand) > 2 && expand[2] == "invoke") || slices.Contains(rest, "invoke") { rest = parseInvokeArgs(rest) } - if err := tools.Wsk(cmd, rest...); err != nil { + if err := tools.Wsk(expand, rest...); err != nil { log.Fatalf("error: %s", err.Error()) } os.Exit(0) @@ -355,15 +391,19 @@ func Main() { // first argument with prefix "-" is considered an embedded tool // using "-" or "--" or "-task" invokes the embedded task - if len(args) > 1 && len(args[1]) > 0 && args[1][0] == '-' { - cmd := args[1][1:] - trace("executing embedded tool", cmd, args) + // 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:] + } // execute the embeded tool and exit - exitCode := executeMainToolsAndExit(cmd, args, opsHome) + exitCode := executeTools(cmd, os.Args[1:], opsHome) os.Exit(exitCode) } - if err := runOps(opsRootDir, args); err != nil { + if err := runOps(opsRootDir, os.Args[1:]); err != nil { log.Fatalf("task execution error: %s", err.Error()) } } diff --git a/tools/tools.go b/tools/tools.go index 204b7fb..d1d9303 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -188,7 +188,10 @@ func RunTool(name string, args []string) (int, error) { } func Help(mainTools []string) { - fmt.Println("Available tools:") + 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) for _, x := range availableTools {
