-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontents.go
More file actions
104 lines (91 loc) · 3.92 KB
/
contents.go
File metadata and controls
104 lines (91 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package forge
import (
"context"
"iter"
"dappco.re/go/forge/types"
)
// ContentService handles file read/write operations via the Forgejo API.
// No Resource embedding — paths vary by operation.
//
// Usage:
//
// f := forge.NewForge("https://forge.lthn.ai", "token")
// _, err := f.Contents.GetFile(ctx, "core", "go-forge", "README.md")
type ContentService struct {
client *Client
}
func newContentService(c *Client) *ContentService {
return &ContentService{client: c}
}
// ListContents returns the entries in a repository directory.
// If ref is non-empty, the listing is resolved against that branch, tag, or commit.
func (s *ContentService) ListContents(ctx context.Context, owner, repo, ref string) ([]types.ContentsResponse, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/contents", pathParams("owner", owner, "repo", repo))
if ref != "" {
path = appendQuery(path, func(q *queryBuilder) {
q.Set("ref", ref)
})
}
var out []types.ContentsResponse
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return out, nil
}
// IterContents returns an iterator over the entries in a repository directory.
// If ref is non-empty, the listing is resolved against that branch, tag, or commit.
func (s *ContentService) IterContents(ctx context.Context, owner, repo, ref string) iter.Seq2[types.ContentsResponse, error] {
return func(yield func(types.ContentsResponse, error) bool) {
items, err := s.ListContents(ctx, owner, repo, ref)
if err != nil {
yield(*new(types.ContentsResponse), err)
return
}
for _, item := range items {
if !yield(item, nil) {
return
}
}
}
}
// GetFile returns metadata and content for a file in a repository.
func (s *ContentService) GetFile(ctx context.Context, owner, repo, filepath string) (*types.ContentsResponse, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/contents/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath))
var out types.ContentsResponse
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetContents returns metadata and content for a file in a repository.
func (s *ContentService) GetContents(ctx context.Context, owner, repo, filepath string) (*types.ContentsResponse, error) {
return s.GetFile(ctx, owner, repo, filepath)
}
// CreateFile creates a new file in a repository.
func (s *ContentService) CreateFile(ctx context.Context, owner, repo, filepath string, opts *types.CreateFileOptions) (*types.FileResponse, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/contents/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath))
var out types.FileResponse
if err := s.client.Post(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateFile updates an existing file in a repository.
func (s *ContentService) UpdateFile(ctx context.Context, owner, repo, filepath string, opts *types.UpdateFileOptions) (*types.FileResponse, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/contents/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath))
var out types.FileResponse
if err := s.client.Put(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteFile deletes a file from a repository. Uses DELETE with a JSON body.
func (s *ContentService) DeleteFile(ctx context.Context, owner, repo, filepath string, opts *types.DeleteFileOptions) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/contents/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath))
return s.client.DeleteWithBody(ctx, path, opts)
}
// GetRawFile returns the raw file content as bytes.
func (s *ContentService) GetRawFile(ctx context.Context, owner, repo, filepath string) ([]byte, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/raw/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath))
return s.client.GetRaw(ctx, path)
}