git-project-manager/internal/remotes/gitea/gitea.go
2024-12-19 14:55:49 -05:00

54 lines
1.1 KiB
Go

package gitearemote
import (
"fmt"
"net/url"
"code.gitea.io/sdk/gitea"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
)
type GiteaRemote struct {
info *info.RemoteInfo
api *gitea.Client
}
func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
return r.info
}
func (r *GiteaRemote) GetType() string {
return r.info.Type.String()
}
func (r *GiteaRemote) String() string {
return fmt.Sprintf("Gitea %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
// Prepares the Gitea api client
func (r *GiteaRemote) setClient() error {
var err error
if r.api == nil {
r.api, err = gitea.NewClient(r.info.Host,
gitea.SetContext(r.info.Context()),
gitea.SetToken(r.info.Token),
)
}
return err
}
// Does not prepare the client due to the Gitea client making an initial
// http connection. API client to be set on-demand
func NewGiteaRemote(remoteInfo *info.RemoteInfo) (*GiteaRemote, error) {
if _, err := url.Parse(remoteInfo.Host); err != nil {
return nil, err
}
giteaRemote := &GiteaRemote{
info: remoteInfo,
}
return giteaRemote, nil
}