git-lfs/lfsapi/lfsapi.go

69 lines
1.6 KiB
Go
Raw Normal View History

package lfsapi
2016-12-16 22:43:05 +00:00
import (
"github.com/git-lfs/git-lfs/v3/creds"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/lfshttp"
"github.com/git-lfs/git-lfs/v3/ssh"
2021-12-14 16:05:42 +00:00
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
2016-12-16 22:43:05 +00:00
)
type Client struct {
2016-12-19 21:38:06 +00:00
Endpoints EndpointFinder
Credentials creds.CredentialHelper
2016-12-20 17:02:25 +00:00
credContext *creds.CredentialHelperContext
client *lfshttp.Client
context lfshttp.Context
}
func NewClient(ctx lfshttp.Context) (*Client, error) {
if ctx == nil {
ctx = lfshttp.NewContext(nil, nil, nil)
2016-12-20 17:29:26 +00:00
}
gitEnv := ctx.GitEnv()
osEnv := ctx.OSEnv()
httpClient, err := lfshttp.NewClient(ctx)
if err != nil {
2021-12-14 16:05:42 +00:00
return nil, errors.Wrap(err, tr.Tr.Get("error creating http client"))
}
c := &Client{
Endpoints: NewEndpointFinder(ctx),
client: httpClient,
context: ctx,
credContext: creds.NewCredentialHelperContext(gitEnv, osEnv),
}
return c, nil
}
func (c *Client) Context() lfshttp.Context {
return c.context
}
// SSHTransfer returns either an suitable transfer object or nil if the
// server is not using an SSH remote or the git-lfs-transfer style of SSH
// remote.
func (c *Client) SSHTransfer(operation, remote string) *ssh.SSHTransfer {
if len(operation) == 0 {
return nil
}
endpoint := c.Endpoints.Endpoint(operation, remote)
if len(endpoint.SSHMetadata.UserAndHost) == 0 {
return nil
}
ctx := c.Context()
tracerx.Printf("attempting pure SSH protocol connection")
sshTransfer, err := ssh.NewSSHTransfer(ctx.OSEnv(), ctx.GitEnv(), &endpoint.SSHMetadata, operation)
if err != nil {
tracerx.Printf("pure SSH protocol connection failed: %s", err)
return nil
}
return sshTransfer
}