mirror of
https://github.com/directorz/mailfull-go.git
synced 2025-12-17 09:37:02 +00:00
Implement subcommands: users, useradd, userdel
This commit is contained in:
86
cmd/mailfull/command/useradd.go
Normal file
86
cmd/mailfull/command/useradd.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/directorz/mailfull-go"
|
||||
)
|
||||
|
||||
// UserAddCommand represents a UserAddCommand.
|
||||
type UserAddCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
// Synopsis returns a one-line synopsis.
|
||||
func (c *UserAddCommand) Synopsis() string {
|
||||
return "Create a new user."
|
||||
}
|
||||
|
||||
// Help returns long-form help text.
|
||||
func (c *UserAddCommand) Help() string {
|
||||
txt := fmt.Sprintf(`
|
||||
Usage:
|
||||
%s %s address
|
||||
|
||||
Description:
|
||||
%s
|
||||
|
||||
Required Args:
|
||||
address
|
||||
The email address that you want to create.
|
||||
`,
|
||||
c.CmdName, c.SubCmdName,
|
||||
c.Synopsis())
|
||||
|
||||
return txt[1:]
|
||||
}
|
||||
|
||||
// Run runs the command and returns the exit status.
|
||||
func (c *UserAddCommand) Run(args []string) int {
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
address := args[0]
|
||||
words := strings.Split(address, "@")
|
||||
if len(words) != 2 {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
userName := words[0]
|
||||
domainName := words[1]
|
||||
|
||||
repo, err := mailfull.OpenRepository(".")
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
user, err := mailfull.NewUser(userName, mailfull.NeverMatchHashedPassword, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := repo.UserCreate(domainName, user); err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
mailData, err := repo.MailData()
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
err = repo.GenerateDatabases(mailData)
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
80
cmd/mailfull/command/userdel.go
Normal file
80
cmd/mailfull/command/userdel.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/directorz/mailfull-go"
|
||||
)
|
||||
|
||||
// UserDelCommand represents a UserDelCommand.
|
||||
type UserDelCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
// Synopsis returns a one-line synopsis.
|
||||
func (c *UserDelCommand) Synopsis() string {
|
||||
return "Delete and backup a user."
|
||||
}
|
||||
|
||||
// Help returns long-form help text.
|
||||
func (c *UserDelCommand) Help() string {
|
||||
txt := fmt.Sprintf(`
|
||||
Usage:
|
||||
%s %s address
|
||||
|
||||
Description:
|
||||
%s
|
||||
|
||||
Required Args:
|
||||
address
|
||||
The email address that you want to delete.
|
||||
`,
|
||||
c.CmdName, c.SubCmdName,
|
||||
c.Synopsis())
|
||||
|
||||
return txt[1:]
|
||||
}
|
||||
|
||||
// Run runs the command and returns the exit status.
|
||||
func (c *UserDelCommand) Run(args []string) int {
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
address := args[0]
|
||||
words := strings.Split(address, "@")
|
||||
if len(words) != 2 {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
userName := words[0]
|
||||
domainName := words[1]
|
||||
|
||||
repo, err := mailfull.OpenRepository(".")
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := repo.UserRemove(domainName, userName); err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
mailData, err := repo.MailData()
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
err = repo.GenerateDatabases(mailData)
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
66
cmd/mailfull/command/users.go
Normal file
66
cmd/mailfull/command/users.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/directorz/mailfull-go"
|
||||
)
|
||||
|
||||
// UsersCommand represents a UsersCommand.
|
||||
type UsersCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
// Synopsis returns a one-line synopsis.
|
||||
func (c *UsersCommand) Synopsis() string {
|
||||
return "Show users."
|
||||
}
|
||||
|
||||
// Help returns long-form help text.
|
||||
func (c *UsersCommand) Help() string {
|
||||
txt := fmt.Sprintf(`
|
||||
Usage:
|
||||
%s %s domain
|
||||
|
||||
Description:
|
||||
%s
|
||||
|
||||
Required Args:
|
||||
domain
|
||||
The domain name.
|
||||
`,
|
||||
c.CmdName, c.SubCmdName,
|
||||
c.Synopsis())
|
||||
|
||||
return txt[1:]
|
||||
}
|
||||
|
||||
// Run runs the command and returns the exit status.
|
||||
func (c *UsersCommand) Run(args []string) int {
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
targetDomainName := args[0]
|
||||
|
||||
repo, err := mailfull.OpenRepository(".")
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
users, err := repo.Users(targetDomainName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
|
||||
return 1
|
||||
}
|
||||
sort.Sort(mailfull.UserSlice(users))
|
||||
|
||||
for _, user := range users {
|
||||
fmt.Fprintf(c.UI.Writer, "%s\n", user.Name())
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -67,6 +67,18 @@ func main() {
|
||||
meta.SubCmdName = c.Subcommand()
|
||||
return &command.AliasDomainDelCommand{Meta: meta}, nil
|
||||
},
|
||||
"users": func() (cli.Command, error) {
|
||||
meta.SubCmdName = c.Subcommand()
|
||||
return &command.UsersCommand{Meta: meta}, nil
|
||||
},
|
||||
"useradd": func() (cli.Command, error) {
|
||||
meta.SubCmdName = c.Subcommand()
|
||||
return &command.UserAddCommand{Meta: meta}, nil
|
||||
},
|
||||
"userdel": func() (cli.Command, error) {
|
||||
meta.SubCmdName = c.Subcommand()
|
||||
return &command.UserDelCommand{Meta: meta}, nil
|
||||
},
|
||||
"commit": func() (cli.Command, error) {
|
||||
meta.SubCmdName = c.Subcommand()
|
||||
return &command.CommitCommand{Meta: meta}, nil
|
||||
|
||||
Reference in New Issue
Block a user