feat: load from curl text files

This commit is contained in:
debugtalk
2022-12-23 18:22:37 +08:00
parent a55a2ff730
commit 59088174de
3 changed files with 24 additions and 49 deletions

View File

@@ -29,6 +29,8 @@ var convertCmd = &cobra.Command{
fromType = convert.FromTypePostman
} else if fromHARFlag {
fromType = convert.FromTypeHAR
} else if fromCurlFlag {
fromType = convert.FromTypeCurl
} else {
fromType = convert.FromTypeJSON
log.Info().Str("fromType", fromType.String()).Msg("set default")
@@ -74,6 +76,7 @@ var (
fromYAMLFlag bool
fromPostmanFlag bool
fromHARFlag bool
fromCurlFlag bool
toJSONFlag bool
toYAMLFlag bool
@@ -87,6 +90,7 @@ func init() {
convertCmd.Flags().BoolVar(&fromYAMLFlag, "from-yaml", false, "load from yaml case format")
convertCmd.Flags().BoolVar(&fromHARFlag, "from-har", false, "load from HAR format")
convertCmd.Flags().BoolVar(&fromPostmanFlag, "from-postman", false, "load from postman format")
convertCmd.Flags().BoolVar(&fromCurlFlag, "from-curl", false, "load from curl format")
convertCmd.Flags().BoolVar(&toJSONFlag, "to-json", true, "convert to JSON case scripts")
convertCmd.Flags().BoolVar(&toYAMLFlag, "to-yaml", false, "convert to YAML case scripts")

View File

@@ -1,7 +1,6 @@
package builtin
import (
"bufio"
"bytes"
"crypto/hmac"
"crypto/sha256"
@@ -352,40 +351,6 @@ func ReadFile(path string) ([]byte, error) {
return file, nil
}
func ReadCmdLines(path string) ([]string, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("convert absolute path failed")
return nil, err
}
file, err := os.Open(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("open file failed")
return nil, err
}
defer file.Close()
var line string
var lines []string
scanner := bufio.NewScanner(file)
// FIXME: resize scanner's capacity for lines over 64K
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text == "" || text == "\n" {
continue
}
if strings.HasSuffix(text, "\\") {
line = line + strings.Trim(text, "\\")
continue
}
line = line + text
lines = append(lines, line)
line = ""
}
return lines, scanner.Err()
}
func GetFileNameWithoutExtension(path string) string {
base := filepath.Base(path)
ext := filepath.Ext(base)

View File

@@ -1,9 +1,11 @@
package convert
import (
"bufio"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"github.com/google/shlex"
@@ -11,7 +13,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/json"
)
@@ -94,12 +95,14 @@ func init() {
// LoadCurlCase loads testcase from one or more curl commands in .txt file
func LoadCurlCase(path string) (*hrp.TCase, error) {
cmds, err := builtin.ReadCmdLines(path)
cmds, err := readFileLines(path)
if err != nil {
return nil, err
}
tCase := &hrp.TCase{
Config: &hrp.TConfig{Name: "testcase converted from curl command"},
Config: &hrp.TConfig{
Name: "testcase converted from curl command",
},
}
for _, cmd := range cmds {
tSteps, err := LoadCurlSteps(cmd)
@@ -115,21 +118,24 @@ func LoadCurlCase(path string) (*hrp.TCase, error) {
return tCase, nil
}
// LoadSingleCurlCase one testcase from one curl command
func LoadSingleCurlCase(cmd string) (*hrp.TCase, error) {
tSteps, err := LoadCurlSteps(cmd)
func readFileLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("open file failed")
return nil, err
}
tCase := &hrp.TCase{
Config: &hrp.TConfig{Name: "testcase converted from curl command"},
TestSteps: tSteps,
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || line == "\n" {
continue
}
lines = append(lines, line)
}
err = tCase.MakeCompat()
if err != nil {
return nil, err
}
return tCase, nil
return lines, scanner.Err()
}
// LoadCurlSteps loads one teststep from one curl command