-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbox-rotate.go
66 lines (54 loc) · 1.55 KB
/
inbox-rotate.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
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
ent "github.com/supriyo-biswas/postbox/entities"
"github.com/supriyo-biswas/postbox/utils"
"gorm.io/gorm"
)
func runInboxRotateCmd(cmd *cobra.Command, args []string) error {
cfg, err := readConfig(cmd.Root().PersistentFlags())
if err != nil {
return fmt.Errorf("failed to read config: %s", err)
}
d, err := openDb(cfg.Database.Path)
if err != nil {
return err
}
var inbox ent.Inbox
err = d.Where("name = ?", args[0]).First(&inbox).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("inbox %s not found", args[0])
}
return fmt.Errorf("failed to query inbox: %s", err)
}
path, _ := cmd.Flags().GetString("credential-file")
newCreds, err := newCredentials(path)
if err != nil {
return err
}
inbox.SmtpPass = utils.HashSecret(newCreds.SmtpPass)
inbox.ApiKey = utils.HashSecret(newCreds.ApiKey)
if err = d.Save(&inbox).Error; err != nil {
return fmt.Errorf("failed to update inbox: %s", err)
}
fmt.Printf("Inbox ID: %d\n", inbox.Id)
fmt.Printf("SMTP username: %s\n", args[0])
if path == "" {
fmt.Printf("SMTP password: %s\n", newCreds.SmtpPass)
fmt.Printf("API key: %s\n", newCreds.ApiKey)
}
return nil
}
var inboxRotateCmd = &cobra.Command{
Use: "rotate inbox",
Short: "Rotate an inbox's SMTP password and API key",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: runInboxRotateCmd,
}
func init() {
inboxRotateCmd.Flags().StringP("credential-file", "f", "", "Use credentials from file")
}