-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
96 lines (79 loc) · 2.06 KB
/
server.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Server type with host, user and port info
type Server struct {
Name string `yaml:"name"`
Username string `yaml:"username,omitempty"`
Hostname string `yaml:"hostname"`
Port uint `yaml:"port,omitempty"`
}
func (s *Server) String() string {
return fmt.Sprintf("%s, %s@%s:%d", s.Name, s.Username, s.Hostname, s.Port)
}
// normalize sets default port and username if empty
func (s *Server) normalize(defaultUsername string, defaultPort uint) {
if len(s.Username) == 0 {
s.Username = defaultUsername
}
if s.Port == 0 {
s.Port = defaultPort
}
}
// validate server params
func (s *Server) validate() error {
var err string
if len(s.Name) == 0 {
err = err + "Name is empty. "
}
if len(s.Username) == 0 {
err = err + "Username is empty. "
}
if len(s.Hostname) == 0 {
err = err + "Hostname is empty. "
}
if len(err) > 0 {
return fmt.Errorf("Invalid configuration file. Error: %s Server: %s", err, s.String())
}
return nil
}
// match server name, hostname and username against pattern
func (s *Server) match(pattern string) bool {
return pattern != "" && (strings.Contains(s.Name, pattern) || strings.Contains(s.Hostname, pattern) || strings.Contains(s.Username, pattern))
}
// match server name, hostname and username against all patterns
func (s *Server) matchAll(patterns []string) bool {
for _, p := range patterns {
if s.match(p) == false {
return false
}
}
return true
}
// create ssh command
func (s *Server) createCmdSSH() *exec.Cmd {
return exec.Command("ssh", "-p", fmt.Sprintf("%d", s.Port), fmt.Sprintf("%s@%s", s.Username, s.Hostname))
}
// get ssh connection string
func (s *Server) getConnectionString() string {
cmd := s.createCmdSSH()
return strings.Join(cmd.Args, " ")
}
// connect to server with ssh
func (s *Server) connect() {
s.exec(s.createCmdSSH())
}
// excecute command
func (s *Server) exec(cmd *exec.Cmd) {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
}