-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspy.go
More file actions
151 lines (122 loc) · 2.85 KB
/
spy.go
File metadata and controls
151 lines (122 loc) · 2.85 KB
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
package spy
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"time"
"bitbucket.org/creachadair/shell"
"github.com/fatih/color"
)
type option func(*session) error
type session struct {
TimestampMode bool
ShellPrompt string
Colour color.Attribute
Input io.Reader
Recorder io.Writer
Terminal io.Writer
}
func WithTimestamps() option {
return func(s *session) error {
s.TimestampMode = true
return nil
}
}
func WithUserPrompt(userPrompt string) option {
return func(s *session) error {
s.ShellPrompt = userPrompt + ":~$"
return nil
}
}
func WithTerminalColour(userColour color.Attribute) option {
return func(s *session) error {
s.Colour = userColour
return nil
}
}
// ParseCommand accepts a string and returns a command name and a slice of args
func ParseCommand(cmd string) (string, []string, error) {
stringResult, ok := shell.Split(cmd)
if !ok {
return "", []string{}, fmt.Errorf("cannot parse command: %q", cmd)
}
if len(stringResult) <= 1 {
return cmd, []string{}, nil
}
return stringResult[0], stringResult[1:], nil
}
// Execute accepts a shell command and writes the result to
// the Session struct writers (Recorder and Terminal)
func (s *session) Execute(commandLine string) error {
name, args, err := ParseCommand(commandLine)
w := io.MultiWriter(s.Terminal, s.Recorder)
fmt.Fprintln(s.Recorder, commandLine)
if err != nil {
fmt.Fprintln(w, "Error parsing command")
return nil
}
cmd := exec.Command(name, args...)
cmd.Stdout = w
err = cmd.Run()
if err != nil {
return err
}
return nil
}
// NewSession creates and returns a new shell session with a file
func NewSession(filepath string, opts ...option) (session, error) {
f, err := os.Create(filepath)
if err != nil {
return session{}, err
}
prompt, err := defaultPrompt()
if err != nil {
return session{}, err
}
s := session{
Input: os.Stdin,
Terminal: os.Stdout,
Recorder: f,
ShellPrompt: prompt,
Colour: color.BgCyan,
}
for _, opt := range opts {
err := opt(&s)
if err != nil {
return session{}, err
}
}
return s, nil
}
func (s *session) Run() error {
scanner := bufio.NewScanner(s.Input)
colour := color.New(s.Colour)
s.RecordTime(time.Now())
colour.Print(s.ShellPrompt)
for scanner.Scan() {
s.Execute(scanner.Text())
colour.Println(s.Terminal)
}
return nil
}
func (s *session) RecordTime(ts time.Time) {
if s.TimestampMode {
formattedTime := ts.Format(time.RFC3339)
fmt.Fprintln(s.Recorder, formattedTime)
}
}
func defaultPrompt() (string, error) {
user, err := user.Current()
if err != nil {
return "", fmt.Errorf("retrieving current user returned err: %v", err)
}
host, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("retrieving host name returned err: %v", err)
}
prompt := fmt.Sprintf("%v@%v:~$", user.Username, host)
return prompt, nil
}