fix: exit with code

This commit is contained in:
debugtalk
2022-10-17 21:33:15 +08:00
parent ceb4fa2ca9
commit c5f81f2593
11 changed files with 62 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"os"
"time"
"github.com/getsentry/sentry-go"
@@ -20,5 +21,5 @@ func main() {
}
}()
cmd.Execute()
os.Exit(cmd.Execute())
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/httprunner/httprunner/v4/hrp/cmd/adb"
"github.com/httprunner/httprunner/v4/hrp/cmd/ios"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/version"
)
@@ -45,7 +46,8 @@ Copyright 2017 debugtalk`,
}
},
Version: version.VERSION,
TraverseChildren: true,
TraverseChildren: true, // parses flags on all parents before executing child command
SilenceUsage: true, // silence usage when an error occurs
}
var (
@@ -56,7 +58,7 @@ var (
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute() int {
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level")
rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format")
rootCmd.PersistentFlags().StringVar(&venv, "venv", "", "specify python3 venv path")
@@ -64,9 +66,8 @@ func Execute() {
ios.Init(rootCmd)
adb.Init(rootCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
err := rootCmd.Execute()
return code.GetErrorCode(err)
}
func setLogLevel(level string) {

View File

@@ -1,8 +1,6 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp"
@@ -20,17 +18,14 @@ var runCmd = &cobra.Command{
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
var paths []hrp.ITestCase
for _, arg := range args {
path := hrp.TestCasePath(arg)
paths = append(paths, &path)
}
runner := makeHRPRunner()
err := runner.Run(paths...)
if err != nil {
os.Exit(1)
}
return runner.Run(paths...)
},
}