Skip to content

Commit 5dafc18

Browse files
Cole BrumleyCole Brumley
Cole Brumley
authored and
Cole Brumley
committed
Swapped out logrus in favor of plain fmt for better windows compatibility
1 parent 350bb46 commit 5dafc18

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

mssh.go

+51-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
log "github.com/Sirupsen/logrus"
65
"github.com/codegangsta/cli"
76
"github.com/fatih/color"
87
"golang.org/x/crypto/ssh"
@@ -13,6 +12,7 @@ import (
1312
// Use of the os/user package prevents cross-compilation
1413
"os/user" // <- https://github.com/golang/go/issues/6376
1514
"path/filepath"
15+
"runtime"
1616
"strings"
1717
)
1818

@@ -21,7 +21,7 @@ func main() {
2121
app.HideVersion = true
2222
app.Name = "mssh"
2323
app.Usage = "Run SSH commands on multiple machines"
24-
app.Version = "0.0.2.1"
24+
app.Version = "0.0.3"
2525
app.Flags = []cli.Flag{
2626
cli.StringFlag{
2727
Name: "user,u",
@@ -47,8 +47,8 @@ func main() {
4747
Usage: "Only show last n lines of output for each cmd",
4848
},
4949
cli.BoolTFlag{
50-
Name: "color,c",
51-
Usage: "Print cmd output in color (use -c=false to disable)",
50+
Name: "color",
51+
Usage: "Print cmd output in color (use --color=false to disable)",
5252
},
5353
}
5454
app.Action = defaultAction
@@ -60,14 +60,14 @@ func defaultAction(c *cli.Context) {
6060

6161
currentUser, err := user.Current()
6262
if err != nil {
63-
log.Errorf("Could not get current user: %v", err)
63+
print(fmt.Sprintf("[X] Could not get current user: %v", err), "")
6464
}
6565
u := c.String("user")
6666
// If no flag, get current username
6767
if len(u) == 0 {
6868
u = currentUser.Username
6969
if len(u) == 0 {
70-
log.Errorln("No username specified!")
70+
print("[X] No username specified!", "")
7171
}
7272
}
7373

@@ -81,7 +81,7 @@ func defaultAction(c *cli.Context) {
8181
if len(key) == 0 {
8282
// If no key provided see if there's an ssh-agent running
8383
if len(os.Getenv("SSH_AUTH_SOCK")) > 0 {
84-
log.Println("Attempting to use existing ssh-agent")
84+
print("[*] Attempting to use existing ssh-agent", "")
8585
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
8686
if err != nil {
8787
return
@@ -90,49 +90,56 @@ func defaultAction(c *cli.Context) {
9090
ag := agent.NewClient(conn)
9191
auths = []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
9292
} else {
93-
// Otherwise use ~/.ssh/id_rsa
94-
key = filepath.FromSlash(currentUser.HomeDir + "/.ssh/id_rsa")
95-
if !fileExists(key) {
96-
log.Errorln("Must specify a key, ~/.ssh/id_rsa does not exist and no ssh-agent is available!")
93+
k := ""
94+
// Otherwise use ~/.ssh/id_rsa or ~/ssh/id_rsa (for windows)
95+
if fileExists(currentUser.HomeDir + string(filepath.Separator) + ".ssh" + string(filepath.Separator) + "id_rsa") {
96+
k = currentUser.HomeDir + string(filepath.Separator) + ".ssh" + string(filepath.Separator) + "id_rsa"
97+
} else if fileExists(currentUser.HomeDir + string(filepath.Separator) + "ssh" + string(filepath.Separator) + "id_rsa") {
98+
k = currentUser.HomeDir + string(filepath.Separator) + "ssh" + string(filepath.Separator) + "id_rsa"
99+
}
100+
if len(k) == 0 {
101+
print("[X] No key specified: "+err.Error(), "")
97102
cli.ShowAppHelp(c)
98103
os.Exit(2)
99104
}
100-
pemBytes, err := ioutil.ReadFile(key)
105+
pemBytes, err := ioutil.ReadFile(k)
101106
if err != nil {
102-
log.Errorf("%v", err)
107+
print("[X] Error reading key: "+err.Error(), "")
108+
os.Exit(2)
103109
}
104110
signer, err := ssh.ParsePrivateKey(pemBytes)
105111
if err != nil {
106-
log.Errorf("%v", err)
112+
print("[X] Error reading key: "+err.Error(), "")
113+
os.Exit(2)
107114
}
108115
auths = []ssh.AuthMethod{ssh.PublicKeys(signer)}
109116
}
110117
} else {
111118
if !fileExists(key) {
112-
log.Errorln("Specified key does not exist!")
119+
print("[X] Specified key does not exist!", "")
113120
os.Exit(1)
114121
}
115122
pemBytes, err := ioutil.ReadFile(key)
116123
if err != nil {
117-
log.Errorf("%v", err)
124+
print("[X] "+err.Error(), "")
118125
}
119126
signer, err := ssh.ParsePrivateKey(pemBytes)
120127
if err != nil {
121-
log.Errorf("%v", err)
128+
print("[X] "+err.Error(), "")
122129
}
123130
auths = []ssh.AuthMethod{ssh.PublicKeys(signer)}
124131
}
125132

126133
// host(s) is required
127134
if len(hosts) == 0 {
128-
log.Warnln("At least one host is required")
135+
print("At least one host is required", "yellow")
129136
cli.ShowAppHelp(c)
130137
os.Exit(2)
131138
}
132139

133140
// At least one command is required
134141
if len(c.Args().First()) == 0 {
135-
log.Warnln("At least one command is required")
142+
print("At least one command is required", "yellow")
136143
cli.ShowAppHelp(c)
137144
os.Exit(2)
138145
}
@@ -152,18 +159,22 @@ func defaultAction(c *cli.Context) {
152159
combined, err := runRemoteCmd(u, host, auth, cmd)
153160
out := tail(string(combined), c.Int("lines"))
154161
if err != nil {
155-
pretty := prettyOutput(
156-
fmt.Sprintf("Execution of `%s` on %s@%s failed. Error message: %v", cmd, u, host, err),
157-
out, true, c.Bool("color"))
162+
col := ""
163+
print(fmt.Sprintf("[X] Execution of `%s` on %s@%s failed. Error message: %v", cmd, u, host, err), "")
164+
if c.Bool("color") {
165+
col = "red"
166+
}
167+
print(out, col)
158168
if c.Bool("fail") {
159-
log.Fatalln(pretty)
169+
os.Exit(1)
160170
}
161-
log.Errorf(pretty)
162171
} else {
163-
pretty := prettyOutput(
164-
fmt.Sprintf("Execution of `%s` on %s@%s succeeded:", cmd, u, host),
165-
out, false, c.Bool("color"))
166-
log.Println(pretty)
172+
print(fmt.Sprintf("[*] Execution of `%s` on %s@%s succeeded:", cmd, u, host), "")
173+
col := ""
174+
if c.Bool("color") {
175+
col = "green"
176+
}
177+
print(out, col)
167178
}
168179
done <- true
169180
}
@@ -176,21 +187,20 @@ func defaultAction(c *cli.Context) {
176187
}
177188
}
178189

179-
func prettyOutput(status string, out string, isErr bool, useColor bool) (formatted string) {
180-
181-
formatted = status
182-
if useColor {
183-
if isErr {
184-
formatted = fmt.Sprintf("%s\n%s", formatted, color.RedString("%s", out))
185-
} else {
186-
formatted = fmt.Sprintf("%s\n%s", formatted, color.GreenString("%s", out))
187-
}
190+
func print(str string, c string) {
191+
if len(c) == 0 || runtime.GOOS == "windows" {
192+
fmt.Printf("%s\n", str)
188193
return
189194
}
190-
if isErr {
191-
formatted = fmt.Sprintf("%s\n%s", formatted, out)
192-
} else {
193-
formatted = fmt.Sprintf("%s\n%s", formatted, out)
195+
switch c {
196+
case "red":
197+
fmt.Printf("%s\n", color.RedString("%s", str))
198+
case "green":
199+
fmt.Printf("%s\n", color.GreenString("%s", str))
200+
case "yellow":
201+
fmt.Printf("%s\n", color.YellowString("%s", str))
202+
default:
203+
fmt.Printf("%s\n", str)
194204
}
195205
return
196206
}

0 commit comments

Comments
 (0)