-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
126 lines (101 loc) · 2.27 KB
/
app.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
package main
import (
"fmt"
"os"
"strconv"
log "github.com/sirupsen/logrus"
)
func main() {
c := Config{
Stdout: os.Stdout,
Stdin: os.Stdin,
Stderr: os.Stderr,
Args: os.Args[1:],
}
os.Exit(run(c))
}
func run(c Config) (exitcode int) {
var err error
cmd, err := c.ParseArgs()
if err != nil {
goto ERROR
}
switch cmd {
case CMD_HELP:
help(c)
case CMD_LIST:
if err = list(c); err != nil {
goto ERROR
}
case CMD_CONNECT:
if err = connect(c); err != nil {
goto ERROR
}
case CMD_DEAUTH:
if err = deauth(c); err != nil {
goto ERROR
}
default:
log.Errorf("Undefined command: %s", cmd)
goto ERROR
}
return 0
ERROR:
log.Errorf("%v", err)
return 1
}
func connect(c Config) error {
con := &Connect{config: c}
return con.Run()
}
func list(c Config) error {
nova := NewNova(c.NetworkInterface)
if err := nova.Init(c.AuthCache); err != nil {
return err
}
machines, err := nova.List()
if err != nil {
return err
}
if len(machines) == 0 {
fmt.Fprintf(os.Stdout, "No servers found.\n")
return nil
}
width := 0
for _, m := range machines {
if len(m.Name) > width {
width = len(m.Name)
}
}
format := "%" + strconv.Itoa(-width) + "s\t%s\n"
fmt.Fprintf(c.Stdout, format, "[Name]", "[IP Address]")
for _, m := range machines {
fmt.Fprintf(c.Stdout, format, m.Name, m.Ipaddr)
}
return nil
}
func help(c Config) {
fmt.Fprintf(c.Stdout, `NAME:
%s - A client program for OpenStack(Nova) that connect to your instance with the instance name.
USAGE:
%s [ssh-options] user@instance-name [comamnd]
VERSION:
%s
OPTIONS:
--authcache: Store credentials to the cache file ($HOME/.novassh).
--command: Specify SSH command (default: "ssh").
--console: Use an serial console connection instead of SSH.
--deauth: Remove credential cache.
--debug: Output some debug messages.
--list: Display instances.
--help: Print this message.
Any other options will pass to the SSH command.
ENVIRONMENTS:
NOVASSH_COMMAND: Specify SSH command (default: "ssh").
NOVASSH_INTERFACE: Specify network interface of instance (default: blank strings which means the auto detection).
`, APPNAME, APPNAME, VERSION)
}
func deauth(c Config) error {
nova := NewNova(c.NetworkInterface)
return nova.RemoveCredentialCache()
}