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

Implement subcommands: domainadd, domaindel

This commit is contained in:
teru
2016-08-11 17:36:28 +09:00
parent d3b125132b
commit 2ec3e444a3
3 changed files with 167 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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