From 2ec3e444a3c7cfe2e5ec9a07362fd6b7e6ac3066 Mon Sep 17 00:00:00 2001 From: teru Date: Thu, 11 Aug 2016 17:36:28 +0900 Subject: [PATCH] Implement subcommands: `domainadd`, `domaindel` --- cmd/mailfull/command/domainadd.go | 88 +++++++++++++++++++++++++++++++ cmd/mailfull/command/domaindel.go | 71 +++++++++++++++++++++++++ cmd/mailfull/main.go | 8 +++ 3 files changed, 167 insertions(+) create mode 100644 cmd/mailfull/command/domainadd.go create mode 100644 cmd/mailfull/command/domaindel.go diff --git a/cmd/mailfull/command/domainadd.go b/cmd/mailfull/command/domainadd.go new file mode 100644 index 0000000..8af48d8 --- /dev/null +++ b/cmd/mailfull/command/domainadd.go @@ -0,0 +1,88 @@ +package command + +import ( + "fmt" + + "github.com/directorz/mailfull-go" +) + +// DomainAddCommand represents a DomainAddCommand. +type DomainAddCommand struct { + Meta +} + +// Synopsis returns a one-line synopsis. +func (c *DomainAddCommand) Synopsis() string { + return "Create a new domain and postmaster." +} + +// Help returns long-form help text. +func (c *DomainAddCommand) Help() string { + txt := fmt.Sprintf(` +Usage: + %s %s domain + +Description: + %s + +Required Args: + domain + The domain name 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 *DomainAddCommand) Run(args []string) int { + if len(args) != 1 { + fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help()) + return 1 + } + + domainName := args[0] + + repo, err := mailfull.OpenRepository(".") + if err != nil { + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err) + return 1 + } + + domain, err := mailfull.NewDomain(domainName) + if err != nil { + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err) + return 1 + } + + if err := repo.DomainCreate(domain); err != nil { + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err) + return 1 + } + + user, err := mailfull.NewUser("postmaster", "", 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 +} diff --git a/cmd/mailfull/command/domaindel.go b/cmd/mailfull/command/domaindel.go new file mode 100644 index 0000000..e7dcee7 --- /dev/null +++ b/cmd/mailfull/command/domaindel.go @@ -0,0 +1,71 @@ +package command + +import ( + "fmt" + + "github.com/directorz/mailfull-go" +) + +// DomainDelCommand represents a DomainDelCommand. +type DomainDelCommand struct { + Meta +} + +// Synopsis returns a one-line synopsis. +func (c *DomainDelCommand) Synopsis() string { + return "Delete and backup a domain." +} + +// Help returns long-form help text. +func (c *DomainDelCommand) Help() string { + txt := fmt.Sprintf(` +Usage: + %s %s domain + +Description: + %s + +Required Args: + domain + The domain name 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 *DomainDelCommand) Run(args []string) int { + if len(args) != 1 { + fmt.Fprintf(c.UI.ErrorWriter, "%v\n", c.Help()) + return 1 + } + + domainName := args[0] + + repo, err := mailfull.OpenRepository(".") + if err != nil { + fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err) + return 1 + } + + if err := repo.DomainRemove(domainName); 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 +} diff --git a/cmd/mailfull/main.go b/cmd/mailfull/main.go index 711a197..ce03c5c 100644 --- a/cmd/mailfull/main.go +++ b/cmd/mailfull/main.go @@ -47,6 +47,14 @@ func main() { meta.SubCmdName = c.Subcommand() return &command.DomainsCommand{Meta: meta}, nil }, + "domainadd": func() (cli.Command, error) { + meta.SubCmdName = c.Subcommand() + return &command.DomainAddCommand{Meta: meta}, nil + }, + "domaindel": func() (cli.Command, error) { + meta.SubCmdName = c.Subcommand() + return &command.DomainDelCommand{Meta: meta}, nil + }, "commit": func() (cli.Command, error) { meta.SubCmdName = c.Subcommand() return &command.CommitCommand{Meta: meta}, nil