-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
send_vault.go
95 lines (90 loc) · 1.65 KB
/
send_vault.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
package send_vault
import (
"log"
"strconv"
mail_cmd "github.com/sikalabs/slu/cmd/mail"
"github.com/sikalabs/slu/lib/vault_smtp"
"github.com/sikalabs/slu/utils/mail_utils"
"github.com/sikalabs/slu/utils/stdin_utils"
"github.com/spf13/cobra"
)
var FlagVaultKey string
var FlagTo string
var FlagFrom string
var FlagSubject string
var FlagMessage string
var Cmd = &cobra.Command{
Use: "send-vault",
Short: "Send mail (with credentials from vault)",
Aliases: []string{"send-v", "s-v", "sv"},
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
var err error
host, port, user, password, err := vault_smtp.GetSMTPSecrets(FlagVaultKey)
if err != nil {
log.Fatal(err)
}
message := FlagMessage
if message == "-" {
message = stdin_utils.ReadAll()
}
from := user
if FlagFrom != "" {
from = FlagFrom
}
err = mail_utils.SendSimpleMail(
host,
strconv.Itoa(port),
user,
password,
from,
FlagTo,
FlagSubject,
message,
)
if err != nil {
log.Fatal(err)
}
},
}
func init() {
mail_cmd.Cmd.AddCommand(Cmd)
Cmd.Flags().StringVarP(
&FlagVaultKey,
"vault-key",
"k",
"default",
"Key in Vault SMTP passwords secret/data/slu/smtp/<key>",
)
Cmd.Flags().StringVarP(
&FlagTo,
"to",
"t",
"",
"to (john@acme.com)",
)
Cmd.Flags().StringVarP(
&FlagFrom,
"from",
"f",
"",
"from (ben@acme.com)",
)
Cmd.MarkFlagRequired("to")
Cmd.Flags().StringVarP(
&FlagSubject,
"subject",
"s",
"",
"Email subject",
)
Cmd.MarkFlagRequired("subject")
Cmd.Flags().StringVarP(
&FlagMessage,
"message",
"m",
"",
"email message (\"-\" for stdin)",
)
Cmd.MarkFlagRequired("message")
}