/
media_script.go
133 lines (114 loc) · 2.86 KB
/
media_script.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
package notices
import (
"bytes"
"errors"
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/utils/string"
"os/exec"
"runtime"
"strings"
)
// 脚本媒介
type NoticeScriptMedia struct {
Path string `yaml:"path" json:"path"`
ScriptType string `yaml:"scriptType" json:"scriptType"` // 脚本类型,可以为path, code
ScriptLang string `yaml:"scriptLang" json:"scriptLang"` // 脚本语言
Script string `yaml:"script" json:"script"` // 脚本代码
Cwd string `yaml:"cwd" json:"cwd"`
Env []*shared.Variable `yaml:"env" json:"env"`
}
// 获取新对象
func NewNoticeScriptMedia() *NoticeScriptMedia {
return &NoticeScriptMedia{}
}
// 添加环境变量
func (this *NoticeScriptMedia) AddEnv(name, value string) {
this.Env = append(this.Env, &shared.Variable{
Name: name,
Value: value,
})
}
// 格式化脚本
func (this *NoticeScriptMedia) FormattedScript() string {
script := this.Script
script = strings.Replace(script, "\r", "", -1)
return script
}
// 保存到本地
func (this *NoticeScriptMedia) Generate(id string) (path string, err error) {
if runtime.GOOS == "windows" {
path = Tea.TmpFile("notice." + id + ".bat")
} else {
path = Tea.TmpFile("notice." + id + ".script")
}
shFile := files.NewFile(path)
if !shFile.Exists() {
err = shFile.WriteString(this.FormattedScript())
if err != nil {
return
}
err = shFile.Chmod(0777)
if err != nil {
return
}
}
return
}
// 发送
func (this *NoticeScriptMedia) Send(user string, subject string, body string) (resp []byte, err error) {
// 脚本
if this.ScriptType == "code" {
path, err := this.Generate(stringutil.Rand(16))
if err != nil {
return nil, err
}
this.Path = path
defer func() {
f := files.NewFile(this.Path)
if f.Exists() {
err := f.Delete()
if err != nil {
logs.Error(err)
}
}
}()
}
if len(this.Path) == 0 {
return nil, errors.New("'path' should be specified")
}
cmd := exec.Command(this.Path)
if len(this.Env) > 0 {
for _, env := range this.Env {
cmd.Env = append(cmd.Env, env.Name+"="+env.Value)
}
}
cmd.Env = append(cmd.Env, "NoticeUser="+user)
cmd.Env = append(cmd.Env, "NoticeSubject="+subject)
cmd.Env = append(cmd.Env, "NoticeBody="+body)
if len(this.Cwd) > 0 {
cmd.Dir = this.Cwd
}
stdout := bytes.NewBuffer([]byte{})
stderr := bytes.NewBuffer([]byte{})
cmd.Stdout = stdout
cmd.Stderr = stderr
err = cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
// do nothing
}
if stderr.Len() > 0 {
return stdout.Bytes(), errors.New(string(stderr.Bytes()))
}
return stdout.Bytes(), nil
}
// 是否需要用户标识
func (this *NoticeScriptMedia) RequireUser() bool {
return false
}