-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (121 loc) · 2.43 KB
/
main.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 main
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
//go:embed smtp.json.example
smtpTPL string
sentDir = "sent"
argFrom *string
argTo *string
argSMTP *string
argVerbose = false
argTemplate = false
send sender
cmd = &cobra.Command{
Short: "Command line tool to send eml files",
Use: "sendeml [-c smtp.json] [-f from] [-t address] *.eml",
Run: func(cmd *cobra.Command, args []string) {
if argTemplate {
fmt.Print(smtpTPL)
return
}
if argVerbose {
logrus.SetLevel(logrus.DebugLevel)
}
// parse smtp.json
cpath := *argSMTP
fd, err := os.Open(cpath)
if err == nil {
err = json.NewDecoder(fd).Decode(&send.config)
}
if errors.Is(err, fs.ErrNotExist) {
logrus.Errorf("%s not found", cpath)
_ = ioutil.WriteFile(cpath, []byte(smtpTPL), 0766)
logrus.Infof("%s created", cpath)
return
}
if err != nil {
logrus.WithError(err).Errorf("parse %s failed", cpath)
return
}
if len(args) == 0 {
args, _ = filepath.Glob("*.eml")
}
if len(args) == 0 {
logrus.Fatalf("no .eml files given")
return
}
// create sent dir
err = os.MkdirAll(sentDir, 0766)
if err != nil {
logrus.WithError(err).Fatalf("can not create sent directory")
return
}
send.sendAndMove(args)
},
}
)
func defaultConfigDir() string {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
return filepath.Join(home, ".sendeml")
}
func init() {
cmd.Flags().SortFlags = false
cmd.PersistentFlags().SortFlags = false
argFrom = cmd.PersistentFlags().StringP(
"from",
"",
"",
"email address from",
)
argTo = cmd.PersistentFlags().StringP(
"to",
"",
"",
"email address to",
)
argSMTP = cmd.PersistentFlags().StringP(
"smtp",
"c",
filepath.Join(defaultConfigDir(), "smtp.json"),
"smtp config",
)
cmd.PersistentFlags().BoolVarP(
&argTemplate,
"print-template",
"p",
false,
"print smtp.json template",
)
cmd.PersistentFlags().BoolVarP(
&argVerbose,
"verbose",
"v",
false,
"verbose",
)
logrus.SetFormatter(&formatter.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
//NoColors: true,
HideKeys: true,
CallerFirst: true,
FieldsOrder: []string{"feed", "article", "source"},
})
}
func main() {
_ = cmd.Execute()
}