1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00
woodpecker/remote/coding/internal/file.go

32 lines
637 B
Go
Raw Normal View History

2017-07-22 11:12:09 +02:00
package internal
import (
"encoding/json"
"fmt"
)
type Commit struct {
File *File `json:"file"`
}
type File struct {
Data string `json:"data"`
}
func (c *Client) GetFile(globalKey, projectName, ref, path string) ([]byte, error) {
u := fmt.Sprintf("/user/%s/project/%s/git/blob/%s/%s", globalKey, projectName, ref, path)
resp, err := c.Get(u, nil)
if err != nil {
return nil, err
}
commit := &Commit{}
err = json.Unmarshal(resp, commit)
if err != nil {
return nil, APIClientErr{"fail to parse file data", u, err}
}
if commit == nil || commit.File == nil {
return nil, nil
}
return []byte(commit.File.Data), nil
}