-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.go
178 lines (153 loc) · 3.3 KB
/
shell.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
package main
import (
"fmt"
"github.com/masterzen/winrm/winrm"
"github.com/mitchellh/cli"
"github.com/peterh/liner"
"log"
"os"
"runtime"
"strings"
)
type Request struct {
command string
elevated bool
}
type Response struct {
exitCode int
err error
stdOut string
stdErr string
}
type GoShell struct {
buffer []byte
client *winrm.Client
config *ConnectionConfig
ui cli.Ui
}
type ConnectionConfig struct {
Hostname string
Port int
Username string
Password string
Timeout string
}
var historyFile = "/tmp/.liner_history"
func NewShell(config *ConnectionConfig) (*GoShell, error) {
client, err := winrm.NewClientWithParameters(&winrm.Endpoint{Host: config.Hostname, Port: config.Port, HTTPS: false, Insecure: true, CACert: nil}, config.Username, config.Password, winrm.NewParameters(config.Timeout, "en-US", 153600))
if err != nil {
return nil, err
}
var ui cli.Ui
if runtime.GOOS == "windows" {
ui = &cli.BasicUi{Writer: os.Stdout, Reader: os.Stdin, ErrorWriter: os.Stderr}
} else {
ui = &cli.ColoredUi{
Ui: &cli.BasicUi{Writer: os.Stdout, Reader: os.Stdin, ErrorWriter: os.Stderr},
OutputColor: cli.UiColorYellow,
InfoColor: cli.UiColorNone,
ErrorColor: cli.UiColorRed,
}
}
return &GoShell{
buffer: make([]byte, 0),
config: config,
client: client,
ui: ui,
}, nil
}
func setupLiner() *liner.State {
line := liner.NewLiner()
if f, err := os.Open(historyFile); err == nil {
line.ReadHistory(f)
f.Close()
}
return line
}
func (s *GoShell) readInput() (string, error) {
liner := setupLiner()
liner.SetCtrlCAborts(true)
defer liner.Close()
input, err := liner.Prompt("$ ")
if err != nil {
log.Print("Error reading line: ", err)
} else {
liner.AppendHistory(input)
// Save to file, but do it asynchronously
go func() {
if f, err := os.Create(historyFile); err != nil {
log.Print("Error writing history file: ", err)
} else {
liner.WriteHistory(f)
f.Close()
}
}()
}
liner.Close()
return input, err
}
// Create a prompt and read from it
func (s *GoShell) waitForInput(fp *os.File, writeChan chan string, quitChan chan<- bool) {
go func() {
for {
line, err := s.readInput()
switch {
case strings.TrimSpace(line) == "":
break
}
if err != nil || line == "exit" || line == "quit" {
if err != nil {
fmt.Printf("Error: %s", err.Error())
}
quitChan <- true
return
}
writeChan <- line
return
}
}()
}
func (s *GoShell) shell(fp *os.File) {
// main shell loop
if fp == nil {
fp = os.Stdin
}
// quit channel
quitChan := make(chan bool)
// inputChan channel
inputChan := make(chan string)
loop:
for {
s.waitForInput(fp, inputChan, quitChan)
select {
case i := <-inputChan:
r := &Request{command: i, elevated: false}
// Parse input grammer
switch {
case strings.Index(i, "sudo") == 0:
r.command = strings.SplitAfter(r.command, "sudo")[1]
r.elevated = true
}
posh := &Powershell{s.client}
res := posh.runCommand(r)
if res.stdOut != "" {
s.ui.Output(res.stdOut)
}
if res.stdErr != "" {
s.ui.Error(res.stdErr)
}
if res.err != nil {
s.ui.Error(res.err.Error())
}
fmt.Println()
case <-quitChan:
s.ui.Info("Quitting...")
break loop
}
}
return
}
func (s *GoShell) Close() {
//s.input.Close()
return
}