-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.go
88 lines (75 loc) · 1.59 KB
/
command.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
package main
import (
"bytes"
"os"
"os/exec"
"os/user"
"text/template"
"time"
)
const emailTemplate = `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
Cmd: {{.Result.Cmd.Args}}
Start: {{.Result.Start}}
End: {{.Result.End}}
Duration: {{.Result.Duration}} total {{.Result.Cmd.ProcessState.UserTime}} user {{.Result.Cmd.ProcessState.SystemTime}} system
ProcessState: {{.Result.Cmd.ProcessState}}
Error: {{.Result.Error}}
Stderr:{{if .Result.StderrExtra}}
... {{.Result.StderrExtra}} more bytes ...{{end}}
{{.Result.Stderr}}
Stdout:{{if .Result.StdoutExtra}}
... {{.Result.StdoutExtra}} more bytes ...{{end}}
{{.Result.Stdout}}
`
type Message struct {
To string
From string
Subject string
Result *Result
}
func (m *Message) Bytes() []byte {
var buf bytes.Buffer
t := template.New("mail")
t, _ = t.Parse(emailTemplate)
t.Execute(&buf, m)
return buf.Bytes()
}
func (m *Message) String() string {
return string(m.Bytes())
}
type Result struct {
Cmd *exec.Cmd
Error error
Start time.Time
End time.Time
Duration time.Duration
Stdout string
Stderr string
StdoutExtra int
StderrExtra int
}
func identity() string {
var username string
user, err := user.Current()
if err != nil {
username = "root"
} else {
username = user.Username
}
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
return username + "@" + hostname
}
type Mailer interface {
Send([]string, string, []byte) error
}
func findMailer() Mailer {
if path, err := FindSendmail(); err == nil {
return &SendMailMailer{path}
}
return &SMTPMailer{}
}