41 lines
711 B
Go
41 lines
711 B
Go
|
|
package api
|
||
|
|
|
||
|
|
import (
|
||
|
|
"code.gitea.io/sdk/gitea"
|
||
|
|
"codeberg.org/romaintb/fgj/internal/config"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Client struct {
|
||
|
|
*gitea.Client
|
||
|
|
hostname string
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewClient(hostname, token string) (*Client, error) {
|
||
|
|
if hostname == "" {
|
||
|
|
hostname = "codeberg.org"
|
||
|
|
}
|
||
|
|
|
||
|
|
client, err := gitea.NewClient("https://"+hostname, gitea.SetToken(token))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &Client{
|
||
|
|
Client: client,
|
||
|
|
hostname: hostname,
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewClientFromConfig(cfg *config.Config, hostname string) (*Client, error) {
|
||
|
|
host, err := cfg.GetHost(hostname)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return NewClient(host.Hostname, host.Token)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Client) Hostname() string {
|
||
|
|
return c.hostname
|
||
|
|
}
|