2024-02-23 00:26:49 +00:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-07-12 17:45:08 +01:00
|
|
|
// 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.
|
|
|
|
|
2022-08-31 01:41:39 +01:00
|
|
|
//go:build !windows
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
package forgejo
|
2022-07-12 17:45:08 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|