mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 07:21:23 +08:00
feat: support curl command
This commit is contained in:
@@ -61,6 +61,19 @@ var traceRouteCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var curlCmd = &cobra.Command{
|
||||||
|
Use: "curl $url",
|
||||||
|
Short: "run integrated curl command",
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
DisableFlagParsing: true,
|
||||||
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
setLogLevel(logLevel)
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return dial.DoCurl(args)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(pingCmd)
|
rootCmd.AddCommand(pingCmd)
|
||||||
pingCmd.Flags().IntVarP(&pingOptions.Count, "count", "c", 10, "Stop after sending (and receiving) N packets")
|
pingCmd.Flags().IntVarP(&pingOptions.Count, "count", "c", 10, "Stop after sending (and receiving) N packets")
|
||||||
@@ -78,4 +91,6 @@ func init() {
|
|||||||
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.MaxTTL, "max-hops", "m", 30, "Set the max number of hops (max TTL to be reached)")
|
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.MaxTTL, "max-hops", "m", 30, "Set the max number of hops (max TTL to be reached)")
|
||||||
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.Queries, "queries", "q", 1, "Set the number of probes per each hop")
|
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.Queries, "queries", "q", 1, "Set the number of probes per each hop")
|
||||||
traceRouteCmd.Flags().BoolVar(&traceRouteOptions.SaveTests, "save-tests", false, "Save traceroute result as json")
|
traceRouteCmd.Flags().BoolVar(&traceRouteOptions.SaveTests, "save-tests", false, "Save traceroute result as json")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(curlCmd)
|
||||||
}
|
}
|
||||||
|
|||||||
72
hrp/internal/dial/curl.go
Normal file
72
hrp/internal/dial/curl.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package dial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
normalResult = "STDOUT"
|
||||||
|
errorResult = "STDERR"
|
||||||
|
failedResult = "FAILED"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CurlResult struct {
|
||||||
|
Result string `json:"result"`
|
||||||
|
ErrorMsg string `json:"errorMsg"`
|
||||||
|
ResultType string `json:"resultType"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DoCurl(args []string) (err error) {
|
||||||
|
var saveTests bool
|
||||||
|
for i, arg := range args {
|
||||||
|
if arg == "--save-tests" {
|
||||||
|
args = append(args[:i], args[i+1:]...)
|
||||||
|
saveTests = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var curlResult CurlResult
|
||||||
|
defer func() {
|
||||||
|
if saveTests {
|
||||||
|
dir, _ := os.Getwd()
|
||||||
|
curlResultName := fmt.Sprintf("curl_result_%v.json", time.Now().Format("20060102150405"))
|
||||||
|
curlResultPath := filepath.Join(dir, curlResultName)
|
||||||
|
err = builtin.Dump2JSON(curlResult, curlResultPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("save dns resolution result failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
cmd := exec.Command("curl", args...)
|
||||||
|
var stdout, stderr bytes.Buffer
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msgf("fail to run curl command")
|
||||||
|
curlResult.ErrorMsg = err.Error()
|
||||||
|
curlResult.Result = stderr.String()
|
||||||
|
curlResult.ResultType = errorResult
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if stdout.String() != "" {
|
||||||
|
fmt.Printf(stdout.String())
|
||||||
|
curlResult.Result = stdout.String()
|
||||||
|
curlResult.ResultType = normalResult
|
||||||
|
} else if stderr.String() != "" {
|
||||||
|
fmt.Printf(stderr.String())
|
||||||
|
curlResult.ErrorMsg = stderr.String()
|
||||||
|
curlResult.ResultType = errorResult
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user