forgejo-sdk/gitea/agent.go
6543 a0127ed0e7 use build tag to support darwin again (#600)
fix https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/build/3344/11

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/600
Reviewed-by: John Olheiser <john+gitea@jolheiser.com>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
2022-08-31 08:41:39 +08:00

38 lines
729 B
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build !windows
package gitea
import (
"fmt"
"net"
"os"
"golang.org/x/crypto/ssh/agent"
)
// hasAgent returns true if the ssh agent is available
func hasAgent() bool {
if _, err := os.Stat(os.Getenv("SSH_AUTH_SOCK")); err != nil {
return false
}
return true
}
// GetAgent returns a ssh agent
func GetAgent() (agent.Agent, error) {
if !hasAgent() {
return nil, fmt.Errorf("no ssh agent available")
}
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, err
}
return agent.NewClient(sshAgent), nil
}