Skip to content

Commit

Permalink
feat: add ToFile support to write the response into a file
Browse files Browse the repository at this point in the history
  • Loading branch information
chengxiangdong committed Jun 27, 2024
1 parent c494153 commit 919e19d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
52 changes: 52 additions & 0 deletions huaweicloud/helper/httphelper/http_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"reflect"
"strconv"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/tidwall/gjson"

"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/pagination"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

type HttpHelper struct {
Expand Down Expand Up @@ -265,6 +268,55 @@ func (c *HttpHelper) ExtractInto(to any) error {
return json.Unmarshal(c.responseBody, to)
}

func (c *HttpHelper) ToFile(dataPath, filePath string) error {
err := utils.WriteToFile(filePath, "", false)
if err != nil {
return err
}

var fileErr error
count := 0
err = c.EachPage(func(jsonData *gjson.Result, err error) bool {

Check warning on line 279 in huaweicloud/helper/httphelper/http_helper.go

View workflow job for this annotation

GitHub Actions / golangci

unused-parameter: parameter 'err' seems to be unused, consider removing or renaming it as _ (revive)
arr := jsonData.Get(dataPath).Array()
for _, v := range arr {
if err := utils.WriteToFile(filePath, fmt.Sprintf("%s\n", v.Raw), true); err != nil {
log.Printf("[ERROR] unable to write file: %s, error: %s", filePath, err)
count++
fileErr = fmt.Errorf("%v items that failed to be written to the file %s", count, err)
}
}
return true
})

mErr := multierror.Append(nil, err, fileErr)
return mErr.ErrorOrNil()
}

func (c *HttpHelper) EachPage(handler func(*gjson.Result, error) bool) error {
if c.method == "" {
c.result.Err = fmt.Errorf("`method` is empty, please specify the client through Client(method string)")
}
if c.result.Err != nil {
return c.result.Err
}

if c.pager == nil {
return fmt.Errorf("`EachPage` only supports paginated query data")
}

c.buildURL()
c.appendQueryParams()

pager := pagination.NewPager(c.client, c.url, c.pager)
pager.Headers = c.requestOpts.MoreHeaders

return pager.EachPage(func(page pagination.Page) (bool, error) {
jsonData, err := bodyToGJson(page.GetBody())
b := handler(jsonData, err)
return b, nil
})
}

func (c *HttpHelper) requestWithPage() {
body := make(map[string]any)
pager := pagination.NewPager(c.client, c.url, c.pager)
Expand Down
62 changes: 62 additions & 0 deletions huaweicloud/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,65 @@ func IsUUID(uuid string) bool {
match, _ := regexp.MatchString(pattern, uuid)
return match
}

func mkdirParentDir(filePath string) error {
// 替换路径中的反斜杠为正斜杠,确保路径在不同操作系统中的兼容性
filePath = strings.ReplaceAll(filePath, "\\", "/")

// 获取目录路径
pos := strings.LastIndexByte(filePath, '/')
if pos == -1 {
return nil
}
directory := filePath[:pos]

if err := os.MkdirAll(directory, os.ModePerm); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create directory: %s, error: %s", directory, err)
}
return nil
}

func WriteToFile(path, content string, append bool) error {
if err := mkdirParentDir(path); err != nil {
return err
}

_, err := os.Stat(path)
if os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("[ERROR] failed to create file: %s, error: %s", path, err)
}
defer file.Close()

_, err = file.WriteString(content)
if err != nil {
return fmt.Errorf("[ERROR] failed to write data to %s, error: %s", path, err)
}
return nil
}
if err != nil {
return fmt.Errorf("[ERROR] failed to stat file: %s, error: %s", path, err)
}

var file *os.File
if append {
file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("[ERROR] failed to open file for appending: %s, error: %s", path, err)
}
} else {
file, err = os.OpenFile(path, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("[ERROR] failed to open file for overwriting: %s, error: %s", path, err)
}
}
defer file.Close()

// 写入内容
_, err = file.WriteString(content)
if err != nil {
return fmt.Errorf("[ERROR] failed to write data to %s, error: %s", path, err)
}
return nil
}

0 comments on commit 919e19d

Please sign in to comment.