-
Notifications
You must be signed in to change notification settings - Fork 0
/
axs.go
155 lines (124 loc) · 2.85 KB
/
axs.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
/*
Axs (pronounced access) implements a simple machine manager. The
configuration file by default is found at $HOME/.axsrc.json. An example of
the configuration file format:
{
"cc1": {
"bay-a": {
"host": "ssh://ben@bay-a:22",
"serial": "telnet://bay-a:23"
},
"bay-b": {
"host": "ssh://root@192.168.0.1",
"bmc": "ssh://admin@192.168.0.2"
}
}
}
*/
package main
import (
"flag"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"strings"
"github.com/spf13/viper"
)
func resolveConfigTarget(target string) (*url.URL, error) {
dest := viper.GetString(target)
if len(dest) == 0 {
return nil, fmt.Errorf("target not found")
}
return url.Parse(dest)
}
func prepareSSHCommand(target *url.URL) ([]string, error) {
command := []string{target.Scheme}
if p := target.Port(); p != "" {
command = append(command, "-p", p)
}
if u := target.User; u != nil && len(u.Username()) > 0 {
user := u.Username()
formatted := fmt.Sprintf("%s@%s", user, target.Hostname())
command = append(command, formatted)
} else {
command = append(command, target.Hostname())
}
return command, nil
}
func prepareTelnetCommand(target *url.URL) ([]string, error) {
command := []string{target.Scheme}
if u := target.User; u != nil && len(u.Username()) > 0 {
user := u.Username()
command = append(command, "-l", user)
}
command = append(command, target.Hostname())
if p := target.Port(); len(p) > 0 {
command = append(command, p)
}
return command, nil
}
func prepareTargetCommand(target *url.URL) ([]string, error) {
switch strings.ToLower(target.Scheme) {
case "ssh":
return prepareSSHCommand(target)
case "telnet":
return prepareTelnetCommand(target)
default:
return nil, fmt.Errorf("unsupported scheme")
}
}
func listTargets() {
targets := viper.AllKeys()
for _, target := range targets {
fmt.Println(target)
}
}
func initConfig(name string) {
expanded := os.ExpandEnv(name)
viper.SetConfigFile(expanded)
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
}
func usage() {
fmt.Printf("Usage: %s [OPTIONS] target\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
list := flag.Bool("list", false, "List targets.")
resolve := flag.Bool("resolve", false, "Resolve command.")
configFile := flag.String("config", "$HOME/.axsrc.json", "Config file.")
flag.Parse()
if !*list && flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
target := flag.Arg(0)
initConfig(*configFile)
if *list {
listTargets()
return
}
url, err := resolveConfigTarget(target)
if err != nil {
log.Fatal(err)
}
command, err := prepareTargetCommand(url)
if err != nil {
log.Fatal(err)
}
if *resolve {
fmt.Println(strings.Join(command, " "))
return
}
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err = cmd.Run(); err != nil {
log.Fatal(err)
}
}