feat: dump2YAML

This commit is contained in:
debugtalk
2021-10-09 16:39:25 +08:00
parent d8a1a6ef24
commit bbe33e0dd0
4 changed files with 115 additions and 75 deletions

View File

@@ -1,10 +1,13 @@
package httpboomer
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"path/filepath"
"gopkg.in/yaml.v3"
)
func (tc *TestCase) toStruct() *TCase {
@@ -33,3 +36,31 @@ func (tc *TestCase) dump2JSON(path string) error {
}
return nil
}
func (tc *TestCase) dump2YAML(path string) error {
path, err := filepath.Abs(path)
if err != nil {
log.Printf("convert absolute path error: %v, path: %v", err, path)
return err
}
log.Printf("dump testcase to yaml path: %s", path)
// init yaml encoder
buffer := new(bytes.Buffer)
encoder := yaml.NewEncoder(buffer)
encoder.SetIndent(4)
// encode
tcStruct := tc.toStruct()
err = encoder.Encode(tcStruct)
if err != nil {
return err
}
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
if err != nil {
log.Printf("dump yaml path error: %v", err)
return err
}
return nil
}