From 59088174de18ae191a6d60ccf5e1abbef2af9c14 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 23 Dec 2022 18:22:37 +0800 Subject: [PATCH] feat: load from curl text files --- hrp/cmd/convert.go | 4 ++++ hrp/internal/builtin/utils.go | 35 ----------------------------------- hrp/pkg/convert/from_curl.go | 34 ++++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/hrp/cmd/convert.go b/hrp/cmd/convert.go index e2a73e6e..ac6617c9 100644 --- a/hrp/cmd/convert.go +++ b/hrp/cmd/convert.go @@ -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") diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index 2f109a5b..63dbfd0f 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -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) diff --git a/hrp/pkg/convert/from_curl.go b/hrp/pkg/convert/from_curl.go index 7c60bf76..4ec2c81e 100644 --- a/hrp/pkg/convert/from_curl.go +++ b/hrp/pkg/convert/from_curl.go @@ -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