forked from gitlabhq/gitlab-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshenv.go
50 lines (43 loc) · 1.27 KB
/
sshenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Package sshenv provides functionality for handling SSH environment variables
package sshenv
import (
"os"
"strings"
)
const (
// GitProtocolEnv defines the ENV name holding the git protocol used
GitProtocolEnv = "GIT_PROTOCOL"
// SSHConnectionEnv defines the ENV holding the SSH connection
SSHConnectionEnv = "SSH_CONNECTION"
// SSHOriginalCommandEnv defines the ENV containing the original SSH command
SSHOriginalCommandEnv = "SSH_ORIGINAL_COMMAND"
)
// Env represents the SSH environment variables
type Env struct {
GitProtocolVersion string
IsSSHConnection bool
OriginalCommand string
RemoteAddr string
NamespacePath string
}
// NewFromEnv creates a new Env instance based on the current environment variables
func NewFromEnv() Env {
isSSHConnection := false
if ok := os.Getenv(SSHConnectionEnv); ok != "" {
isSSHConnection = true
}
return Env{
GitProtocolVersion: os.Getenv(GitProtocolEnv),
IsSSHConnection: isSSHConnection,
RemoteAddr: remoteAddrFromEnv(),
OriginalCommand: os.Getenv(SSHOriginalCommandEnv),
}
}
// remoteAddrFromEnv returns the connection address from ENV string
func remoteAddrFromEnv() string {
address := os.Getenv(SSHConnectionEnv)
if address != "" {
return strings.Fields(address)[0]
}
return ""
}