forked from go-shiori/shiori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
132 lines (111 loc) · 2.78 KB
/
account.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"syscall"
)
var (
accountCmd = &cobra.Command{
Use: "account",
Short: "Manage account for accessing web interface.",
}
addAccountCmd = &cobra.Command{
Use: "add username",
Short: "Create new account.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
username := args[0]
fmt.Println("Username: " + username)
fmt.Print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
cError.Println(err)
return
}
fmt.Println()
err = addAccount(username, string(bytePassword))
if err != nil {
cError.Println(err)
return
}
},
}
printAccountCmd = &cobra.Command{
Use: "print",
Short: "Print the saved accounts.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
keyword, _ := cmd.Flags().GetString("search")
err := printAccounts(keyword)
if err != nil {
cError.Println(err)
return
}
},
}
deleteAccountCmd = &cobra.Command{
Use: "delete [usernames]",
Short: "Delete the saved accounts.",
Long: "Delete accounts. " +
"Accepts space-separated list of usernames. " +
"If no arguments, all records will be deleted.",
Run: func(cmd *cobra.Command, args []string) {
// Read flags
skipConfirmation, _ := cmd.Flags().GetBool("yes")
// If no arguments, confirm to user
if len(args) == 0 && !skipConfirmation {
confirmDelete := ""
fmt.Print("Remove ALL accounts? (y/n): ")
fmt.Scanln(&confirmDelete)
if confirmDelete != "y" {
fmt.Println("No accounts deleted")
return
}
}
err := DB.DeleteAccounts(args...)
if err != nil {
cError.Println(err)
return
}
if len(args) == 1 {
fmt.Println("Account has been deleted")
return
}
fmt.Println("Accounts have been deleted")
},
}
)
func init() {
// Create flags
printAccountCmd.Flags().StringP("search", "s", "", "Search accounts by username")
deleteAccountCmd.Flags().BoolP("yes", "y", false, "Skip confirmation prompt and delete ALL accounts")
accountCmd.AddCommand(addAccountCmd)
accountCmd.AddCommand(printAccountCmd)
accountCmd.AddCommand(deleteAccountCmd)
rootCmd.AddCommand(accountCmd)
}
func addAccount(username, password string) error {
if username == "" {
return fmt.Errorf("Username must not empty")
}
if len(password) < 8 {
return fmt.Errorf("Password must be at least 8 characters")
}
err := DB.CreateAccount(username, password)
if err != nil {
return err
}
return nil
}
func printAccounts(keyword string) error {
accounts, err := DB.GetAccounts(keyword, false)
if err != nil {
return err
}
for _, account := range accounts {
cIndex.Print("- ")
fmt.Println(account.Username)
}
return nil
}