git-project-manager/internal/remotes/gitea/gitea.go

54 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-01-16 18:23:38 +00:00
package gitearemote
import (
2024-01-16 19:26:56 +00:00
"fmt"
2024-03-05 21:55:01 +00:00
"net/url"
2024-01-16 19:26:56 +00:00
2024-01-16 18:23:38 +00:00
"code.gitea.io/sdk/gitea"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
2024-01-16 18:23:38 +00:00
)
type GiteaRemote struct {
info *info.RemoteInfo
2024-01-16 18:23:38 +00:00
api *gitea.Client
}
func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
2024-01-16 18:23:38 +00:00
return r.info
}
func (r *GiteaRemote) GetType() string {
2024-01-17 21:56:41 +00:00
return r.info.Type.String()
}
2024-01-16 19:26:56 +00:00
func (r *GiteaRemote) String() string {
return fmt.Sprintf("Gitea %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
2024-03-05 21:55:01 +00:00
// Prepares the Gitea api client
func (r *GiteaRemote) setClient() error {
var err error
2024-03-06 21:21:52 +00:00
if r.api == nil {
r.api, err = gitea.NewClient(r.info.Host,
gitea.SetContext(r.info.Context()),
gitea.SetToken(r.info.Token),
)
}
2024-03-05 21:55:01 +00:00
return err
}
2024-01-16 18:23:38 +00:00
2024-03-05 21:55:01 +00:00
// 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 {
2024-01-16 18:23:38 +00:00
return nil, err
}
giteaRemote := &GiteaRemote{
info: remoteInfo,
}
return giteaRemote, nil
}