-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers.go
181 lines (160 loc) 路 3.22 KB
/
helpers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package utils
import (
"bufio"
"fmt"
"log"
"net"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
)
const (
defaultPort = 22
defaultHost = "127.0.0.1"
)
type sshUrl struct {
Username string
Host string
Port int
}
// ParseSSHUrl build an sshUrl object from an url string
func ParseSSHUrl(url string) *sshUrl {
parts := strings.Split(url, "@")
usr := CurrentUser()
conf := &sshUrl{}
var hostPort string
if len(parts) == 2 {
conf.Username = parts[0]
hostPort = parts[1]
} else {
conf.Username = usr.Username
hostPort = parts[0]
}
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
// error could be "missing port in address" so try again appending defaultPort
host, port, err = net.SplitHostPort(fmt.Sprintf("%s:%d", hostPort, defaultPort))
if err != nil {
log.Fatalln(err)
}
}
conf.Host = defaultHost
if host != "" {
conf.Host = host
ip := net.ParseIP(host)
if ip != nil { // it could be a domain name
if ip.To4() == nil {
conf.Host = fmt.Sprintf("[%s]", host)
}
}
}
conf.Port = defaultPort
if port != "" {
port, err := strconv.Atoi(port)
if err != nil {
log.Fatalln(err)
}
conf.Port = port
}
return conf
}
// ExpandUserHome resolve paths like "~/.ssh/id_rsa"
func ExpandUserHome(path string) (string, error) {
usr := CurrentUser()
ret := path
// supports paths like "~/.ssh/id_rsa"
if strings.HasPrefix(path, "~/") {
ret = filepath.Join(usr.HomeDir, path[2:])
}
return ret, nil
}
// GetUserDefaultShell try to get the best shell for the user
func GetUserDefaultShell(username string) string {
if runtime.GOOS == "windows" {
return "c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe"
}
fallback := "/bin/sh"
file, err := os.Open("/etc/passwd")
if err != nil {
return fallback
}
defer file.Close()
lines := bufio.NewReader(file)
for {
line, _, err := lines.ReadLine()
if err != nil {
break
}
fs := strings.Split(string(line), ":")
if len(fs) != 7 {
continue
}
if fs[0] != username {
continue
}
shell := fs[6]
return shell
}
return fallback
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
var currentUserCache struct {
sync.Once
u *user.User
}
func CurrentUser() *user.User {
currentUserCache.Do(func() {
currentUserCache.u = func() *user.User {
if cu, err := user.Current(); err == nil {
return cu
}
// error fallback
userName := func() string {
if runtime.GOOS == "windows" {
if user, ok := os.LookupEnv("USERNAME"); ok {
return user
}
} else {
if user, ok := os.LookupEnv("USER"); ok {
return user
}
}
return "root"
}()
homeDir := func() string {
if home, err := os.UserHomeDir(); err == nil {
return home
}
if wd, err := os.Getwd(); err == nil {
return wd
}
return "/"
}()
u := &user.User{
Uid: "?",
Gid: "?",
Username: userName,
Name: userName,
HomeDir: homeDir,
}
return u
}()
})
return currentUserCache.u
}