1
0
mirror of https://github.com/directorz/mailfull-go.git synced 2025-12-17 09:37:02 +00:00

Implement subcommands: aliasusers, aliasuseradd, aliasusermod, aliasuserdel

This commit is contained in:
teru
2016-08-14 12:31:29 +09:00
parent fdd476d3b7
commit cd1101d096
5 changed files with 348 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
package command
import (
"fmt"
"strings"
mailfull "github.com/directorz/mailfull-go"
)
// AliasUserAddCommand represents a AliasUserAddCommand.
type AliasUserAddCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *AliasUserAddCommand) Synopsis() string {
return "Create a new aliasuser."
}
// Help returns long-form help text.
func (c *AliasUserAddCommand) Help() string {
txt := fmt.Sprintf(`
Usage:
%s %s address target [target...]
Description:
%s
Required Args:
address
The email address that you want to create.
target
Target email addresses.
`,
c.CmdName, c.SubCmdName,
c.Synopsis())
return txt[1:]
}
// Run runs the command and returns the exit status.
func (c *AliasUserAddCommand) Run(args []string) int {
if len(args) < 2 {
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
return 1
}
address := args[0]
targets := args[1:]
words := strings.Split(address, "@")
if len(words) != 2 {
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
return 1
}
aliasUserName := words[0]
domainName := words[1]
repo, err := mailfull.OpenRepository(".")
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
aliasUser, err := mailfull.NewAliasUser(aliasUserName, targets)
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
if err := repo.AliasUserCreate(domainName, aliasUser); 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
}

View File

@@ -0,0 +1,79 @@
package command
import (
"fmt"
"strings"
mailfull "github.com/directorz/mailfull-go"
)
// AliasUserDelCommand represents a AliasUserDelCommand.
type AliasUserDelCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *AliasUserDelCommand) Synopsis() string {
return "Delete a aliasuser."
}
// Help returns long-form help text.
func (c *AliasUserDelCommand) 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 *AliasUserDelCommand) 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
}
aliasUserName := 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.AliasUserRemove(domainName, aliasUserName); 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
}

View File

@@ -0,0 +1,98 @@
package command
import (
"fmt"
"strings"
mailfull "github.com/directorz/mailfull-go"
)
// AliasUserModCommand represents a AliasUserModCommand.
type AliasUserModCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *AliasUserModCommand) Synopsis() string {
return "Modify a aliasuser."
}
// Help returns long-form help text.
func (c *AliasUserModCommand) Help() string {
txt := fmt.Sprintf(`
Usage:
%s %s address target [target...]
Description:
%s
Required Args:
address
The email address that you want to modify.
target
Target email addresses.
`,
c.CmdName, c.SubCmdName,
c.Synopsis())
return txt[1:]
}
// Run runs the command and returns the exit status.
func (c *AliasUserModCommand) Run(args []string) int {
if len(args) < 2 {
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
return 1
}
address := args[0]
targets := args[1:]
words := strings.Split(address, "@")
if len(words) != 2 {
fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help())
return 1
}
aliasUserName := words[0]
domainName := words[1]
repo, err := mailfull.OpenRepository(".")
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
aliasUser, err := repo.AliasUser(domainName, aliasUserName)
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
if aliasUser == nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", mailfull.ErrAliasUserNotExist)
return 1
}
if err := aliasUser.SetTargets(targets); err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
if err := repo.AliasUserUpdate(domainName, aliasUser); 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
}

View File

@@ -0,0 +1,66 @@
package command
import (
"fmt"
"sort"
"github.com/directorz/mailfull-go"
)
// AliasUsersCommand represents a AliasUsersCommand.
type AliasUsersCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *AliasUsersCommand) Synopsis() string {
return "Show aliasusers."
}
// Help returns long-form help text.
func (c *AliasUsersCommand) 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 *AliasUsersCommand) 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
}
aliasUsers, err := repo.AliasUsers(targetDomainName)
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
sort.Sort(mailfull.AliasUserSlice(aliasUsers))
for _, aliasUser := range aliasUsers {
fmt.Fprintf(c.UI.Writer, "%s\n", aliasUser.Name())
}
return 0
}

View File

@@ -87,6 +87,22 @@ func main() {
meta.SubCmdName = c.Subcommand()
return &command.UserCheckPwCommand{Meta: meta}, nil
},
"aliasusers": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.AliasUsersCommand{Meta: meta}, nil
},
"aliasuseradd": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.AliasUserAddCommand{Meta: meta}, nil
},
"aliasusermod": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.AliasUserModCommand{Meta: meta}, nil
},
"aliasuserdel": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.AliasUserDelCommand{Meta: meta}, nil
},
"catchall": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.CatchAllCommand{Meta: meta}, nil