Update Go module path from forge.lthn.ai/core/go-forge to dappco.re/go/core/forge. Migrate all import paths and dependency references (go-io → dappco.re/go/core/io, go-log → dappco.re/go/core/log). Update documentation (CLAUDE.md, README.md, docs/) to reflect new paths. Co-Authored-By: Virgil <virgil@lethean.io>
60 lines
2.1 KiB
Go
60 lines
2.1 KiB
Go
package forge
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"dappco.re/go/core/forge/types"
|
|
)
|
|
|
|
// ContentService handles file read/write operations via the Forgejo API.
|
|
// No Resource embedding — paths vary by operation.
|
|
type ContentService struct {
|
|
client *Client
|
|
}
|
|
|
|
func newContentService(c *Client) *ContentService {
|
|
return &ContentService{client: c}
|
|
}
|
|
|
|
// 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 := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, filepath)
|
|
var out types.ContentsResponse
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// 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 := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, 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 := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, 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 := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, 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 := fmt.Sprintf("/api/v1/repos/%s/%s/raw/%s", owner, repo, filepath)
|
|
return s.client.GetRaw(ctx, path)
|
|
}
|