/
goshnix.go
326 lines (294 loc) · 8.52 KB
/
goshnix.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package goshnix
import "fmt"
import "strings"
import "os"
import "time"
import "strconv"
// The Goshnix lib client
type Goshnix struct {
client *ssh_client
}
type fileinfo struct {
name string
size int64
mode os.FileMode
modtime time.Time
isdir bool
}
//Initialize the goshnix client
func Init(host, port, uname, pass string) (*Goshnix, error) {
client, err := create_sshclient(host, port, uname, pass)
if err != nil {
return nil, err
}
op, excerr := client.execute_command("echo \"Hello\"")
if excerr != nil {
return nil, excerr
}
if op != "Hello" {
return nil, throw_connerror("Test command execution invalid output")
}
goshnix := &Goshnix{}
goshnix.client = client
return goshnix, nil
}
//Isssherror check if error is due to any ssh connection error
func Isssherror(err error) bool {
_, ok := err.(connerror)
return ok
}
// Iscmderror check if error is due to command execution failed
func Iscmderror(err error) bool {
_, ok := err.(Cmderror)
return ok
}
//Chmod changes the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target
func (goshnix *Goshnix) Chmod(name string, mode os.FileMode) error {
modei := int(mode)
command := fmt.Sprintf("chmod %d %s", modei, name)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
//Chown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target
func (goshnix *Goshnix) Chown(name string, uid, gid int) error {
command := fmt.Sprintf("chown %d:%d %s", uid, gid, name)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
//Environ returns a copy of strings representing the environment, in the form "key=value"
func (goshnix *Goshnix) Environ() ([]string, error) {
command := "env"
op, err := goshnix.client.execute_command(command)
if err != nil {
return nil, err
}
envs := strings.Split(op, "\n")
return envs, nil
}
//Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present
func (goshnix *Goshnix) Getenv(key string) (string, error) {
envs, err := goshnix.Environ()
if err != nil {
return "", err
}
for _, env := range envs {
keyval := strings.Split(env, "=")
if keyval[0] == key {
return keyval[1], nil
}
}
return "", nil
}
//LookupEnv retrieves the value of the environment variable named by the key. If the variable is present in the environment the value (which may be empty) is returned and the boolean is true
func (goshnix *Goshnix) LookupEnv(key string) (string, bool, error) {
envs, err := goshnix.Environ()
if err != nil {
return "", false, err
}
for _, env := range envs {
keyval := strings.Split(env, "=")
if keyval[0] == key {
return keyval[1], true, nil
}
}
return "", false, nil
}
//Hostname returns the host name reported by the kernel
func (goshnix *Goshnix) Hostname() (name string, err error) {
command := "hostname"
op, err := goshnix.client.execute_command(command)
if err != nil {
return "", err
}
return op, nil
}
// Link creates newname as a hard link to the oldname file
func (goshnix *Goshnix) Link(oldname, newname string) error {
command := fmt.Sprintf("link %s %s", oldname, newname)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// Mkdir creates a new directory with the specified name and permission bits
func (goshnix *Goshnix) Mkdir(name string, perm os.FileMode) error {
mode := int(perm)
command := fmt.Sprintf("mkdir %s --mode=%d", name, mode)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// Readlink returns the destination of the named symbolic link
func (goshnix *Goshnix) Readlink(name string) (string, error) {
command := fmt.Sprintf("readlink %s", name)
op, err := goshnix.client.execute_command(command)
if err != nil {
return "", err
}
return op, nil
}
// Remove removes the named file or directory
func (goshnix *Goshnix) Remove(name string) error {
command := fmt.Sprintf("rm -r %s", name)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters
func (goshnix *Goshnix) RemoveAll(path string) error {
command := fmt.Sprintf("rm -rf %s", path)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// Rename renames (moves) oldpath to newpath. If newpath already exists, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories
func (goshnix *Goshnix) Rename(oldpath, newpath string) error {
command := fmt.Sprintf("mv %s %s", oldpath, newpath)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// Symlink creates newname as a symbolic link to oldname
func (goshnix *Goshnix) Symlink(oldname, newname string) error {
command := fmt.Sprintf("ln -s %s %s", oldname, newname)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// Setenv sets the value of the environment variable named by the key (TODO: As the ssh session is new each time, it could make the Setenv useless)
func (goshnix *Goshnix) Setenv(key, value string) error {
command := fmt.Sprintf("export \"%s=%s\"", key, value)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
/* XXX: File utils */
// Stat returns a FileInfo describing the named file
func (goshnix *Goshnix) Stat(name string) (os.FileInfo, error) {
command := fmt.Sprintf("stat %s", name)
statop, staterr := goshnix.client.execute_command(command)
if staterr != nil {
return nil, staterr
}
lines := strings.Split(statop, "\n")
var file string
var isdir bool
var mode os.FileMode
var modtime time.Time
var size int64
for _, line := range lines {
trimline := strings.Trim(line, " ")
trimlines := strings.Fields(trimline)
switch trimlines[0] {
case "File:":
file = strings.Trim(trimlines[1], "'")
case "Size":
sizei, _ := strconv.Atoi(trimlines[1])
size = int64(sizei)
if trimlines[7] == "directory" {
isdir = true
} else {
isdir = false
}
case "Access":
for _, key := range trimlines {
if key == "Uid" {
mdata := strings.Split(strings.Trim(trimlines[1], "()"), "/")[0]
modei, _ := strconv.Atoi(mdata)
mode = os.FileMode(modei)
}
}
case "Modify":
/*
date := trimlines[1]
tym := trimlines[2]
*/
// TODO: Claculate time and set modtime
}
}
finfo := &fileinfo{}
finfo.name = file
finfo.isdir = isdir
finfo.mode = mode
finfo.modtime = modtime
finfo.size = size
return finfo, nil
}
// Get Base name of the file
func (finfo *fileinfo) Name() string {
return finfo.name
}
// Get length in bytes for regular files
func (finfo *fileinfo) Size() int64 {
return finfo.size
}
// Get file mode bits
func (finfo *fileinfo) Mode() os.FileMode {
return finfo.mode
}
// Get the modification time
func (finfo *fileinfo) ModTime() time.Time {
return finfo.modtime
}
// Check if directory
func (finfo *fileinfo) IsDir() bool {
return finfo.isdir
}
// Get underying data source
func (finfo *fileinfo) Sys() interface{} {
return finfo
}
// Kill a running process by its pid
func (goshnix *Goshnix) Kill(pid int) error {
command := fmt.Sprintf("kill -9 %d", pid)
_, err := goshnix.client.execute_command(command)
if err != nil {
return err
}
return nil
}
// ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename
func (goshnix *Goshnix) ReadDir(dirname string) ([]os.FileInfo, error) {
command := fmt.Sprintf("ls -a %s", dirname) // It should sort by default
op, err := goshnix.client.execute_command(command)
if err != nil {
return nil, err
}
files := strings.Split(op, "\n")
var fileinfos []os.FileInfo
for _, file := range files {
fileinfo, err := goshnix.Stat(file)
if err != nil {
return fileinfos, err
}
fileinfos = append(fileinfos, fileinfo)
}
return fileinfos, nil
}
// ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
func (goshnix *Goshnix) ReadFile(filename string) ([]byte, error) {
op, err := goshnix.client.get_file_content(filename)
if err != nil {
return nil, err
}
return []byte(op), nil
}