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

Implement subcommands: domaindisable, domainenable #8

This commit is contained in:
teru
2016-08-23 17:45:17 +09:00
parent 281d5a4a9d
commit 009ee84e63
3 changed files with 174 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
package command
import (
"fmt"
"github.com/directorz/mailfull-go"
)
// DomainDisableCommand represents a DomainDisableCommand.
type DomainDisableCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *DomainDisableCommand) Synopsis() string {
return "Disable a domain temporarily."
}
// Help returns long-form help text.
func (c *DomainDisableCommand) Help() string {
txt := fmt.Sprintf(`
Usage:
%s %s domain
Description:
%s
Required Args:
domain
The domain name that you want to disable.
`,
c.CmdName, c.SubCmdName,
c.Synopsis())
return txt[1:]
}
// Run runs the command and returns the exit status.
func (c *DomainDisableCommand) 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 := repo.Domain(domainName)
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
if domain == nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", mailfull.ErrDomainNotExist)
return 1
}
domain.SetDisabled(true)
if err := repo.DomainUpdate(domain); 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,83 @@
package command
import (
"fmt"
"github.com/directorz/mailfull-go"
)
// DomainEnableCommand represents a DomainEnableCommand.
type DomainEnableCommand struct {
Meta
}
// Synopsis returns a one-line synopsis.
func (c *DomainEnableCommand) Synopsis() string {
return "Enable a domain."
}
// Help returns long-form help text.
func (c *DomainEnableCommand) Help() string {
txt := fmt.Sprintf(`
Usage:
%s %s domain
Description:
%s
Required Args:
domain
The domain name that you want to enable.
`,
c.CmdName, c.SubCmdName,
c.Synopsis())
return txt[1:]
}
// Run runs the command and returns the exit status.
func (c *DomainEnableCommand) 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 := repo.Domain(domainName)
if err != nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", err)
return 1
}
if domain == nil {
fmt.Fprintf(c.UI.ErrorWriter, "[ERR] %v\n", mailfull.ErrDomainNotExist)
return 1
}
domain.SetDisabled(false)
if err := repo.DomainUpdate(domain); 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

@@ -62,6 +62,14 @@ func main() {
meta.SubCmdName = c.Subcommand() meta.SubCmdName = c.Subcommand()
return &command.DomainDelCommand{Meta: meta}, nil return &command.DomainDelCommand{Meta: meta}, nil
}, },
"domaindisable": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.DomainDisableCommand{Meta: meta}, nil
},
"domainenable": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand()
return &command.DomainEnableCommand{Meta: meta}, nil
},
"aliasdomains": func() (cli.Command, error) { "aliasdomains": func() (cli.Command, error) {
meta.SubCmdName = c.Subcommand() meta.SubCmdName = c.Subcommand()
return &command.AliasDomainsCommand{Meta: meta}, nil return &command.AliasDomainsCommand{Meta: meta}, nil