diff --git a/cmd/config/config.go b/cmd/config/config.go index 3ded835..918fd53 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -2,16 +2,12 @@ package config import "github.com/urfave/cli/v2" -var Command *cli.Command - -func init() { - Command = &cli.Command{ - Name: "config", - Usage: "config some settings", - Subcommands: []*cli.Command{ - UserCommand, - DatabaseCommand, - DNSCommand, - }, - } +var Command = &cli.Command{ + Name: "config", + Usage: "config some settings", + Subcommands: []*cli.Command{ + UserCommand, + DatabaseCommand, + DNSCommand, + }, } diff --git a/cmd/config/database.go b/cmd/config/database.go index 5115a6e..5f354b3 100644 --- a/cmd/config/database.go +++ b/cmd/config/database.go @@ -7,23 +7,19 @@ import ( "github.com/urfave/cli/v2" ) -var DatabaseCommand *cli.Command +var migrationCommand = &cli.Command{ + Name: "migrate", + Usage: "migrate database", + Action: migrateDatabase, +} -func init() { - migrationCommand := &cli.Command{ - Name: "migrate", - Usage: "migrate database", - Action: migrateDatabase, - } - - DatabaseCommand = &cli.Command{ - Name: "database", - Usage: "database administration", - Aliases: []string{"db"}, - Subcommands: []*cli.Command{ - migrationCommand, - }, - } +var DatabaseCommand = &cli.Command{ + Name: "database", + Usage: "database administration", + Aliases: []string{"db"}, + Subcommands: []*cli.Command{ + migrationCommand, + }, } func migrateDatabase(c *cli.Context) error { diff --git a/cmd/config/dns.go b/cmd/config/dns.go index cf2c3c5..727a6ba 100644 --- a/cmd/config/dns.go +++ b/cmd/config/dns.go @@ -5,24 +5,21 @@ import ( "reCoreD-UI/database" "github.com/urfave/cli/v2" + "github.com/urfave/cli/v2/altsrc" ) -var DNSCommand *cli.Command - -func init() { - DNSCommand = &cli.Command{ - Name: "dns", - Usage: "Config DNS Settings", - Flags: []cli.Flag{ - &cli.StringSliceFlag{ - Name: "servers", - Usage: "dns servers", - Aliases: []string{"s"}, - Required: true, - }, - }, - Action: setDNS, - } +var DNSCommand = &cli.Command{ + Name: "dns", + Usage: "Config DNS Settings", + Flags: []cli.Flag{ + altsrc.NewStringSliceFlag(&cli.StringSliceFlag{ + Name: "servers", + Usage: "dns servers", + Aliases: []string{"s"}, + Required: true, + }), + }, + Action: setDNS, } func setDNS(c *cli.Context) error { diff --git a/cmd/config/user.go b/cmd/config/user.go index dafc096..0b49c96 100644 --- a/cmd/config/user.go +++ b/cmd/config/user.go @@ -5,32 +5,29 @@ import ( "reCoreD-UI/database" "github.com/urfave/cli/v2" + "github.com/urfave/cli/v2/altsrc" ) -var UserCommand *cli.Command - -func init() { - UserCommand = &cli.Command{ - Name: "user", - Usage: "set admin username and password", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "username", - Aliases: []string{"u"}, - Value: "amdin", - Usage: "admin username", - EnvVars: []string{"RECORED_ADMIN_USERNAME"}, - }, - &cli.StringFlag{ - Name: "passowrd", - Aliases: []string{"p"}, - Required: true, - Usage: "admin password", - EnvVars: []string{"RECORED_ADMIN_PASSWORD"}, - }, - }, - Action: setUser, - } +var UserCommand = &cli.Command{ + Name: "user", + Usage: "set admin username and password", + Flags: []cli.Flag{ + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "username", + Aliases: []string{"u"}, + Value: "amdin", + Usage: "admin username", + EnvVars: []string{"RECORED_ADMIN_USERNAME"}, + }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "passowrd", + Aliases: []string{"p"}, + Required: true, + Usage: "admin password", + EnvVars: []string{"RECORED_ADMIN_PASSWORD"}, + }), + }, + Action: setUser, } func setUser(c *cli.Context) error { diff --git a/cmd/server/server.go b/cmd/server/server.go index eb9776b..ba64826 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -4,33 +4,30 @@ import ( webserver "reCoreD-UI/server" "github.com/urfave/cli/v2" + "github.com/urfave/cli/v2/altsrc" ) -var Command *cli.Command - -func init() { - Command = &cli.Command{ - Name: "server", - Usage: "run server", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "prefix", - Value: "/", - Usage: "web prefix", - }, - &cli.StringFlag{ - Name: "listen", - Value: "[::]", - Usage: "IP for listen at", - }, - &cli.IntFlag{ - Name: "port", - Value: 8080, - Usage: "Port for listen at", - }, - }, - Action: runServer, - } +var Command = &cli.Command{ + Name: "server", + Usage: "run server", + Flags: []cli.Flag{ + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "prefix", + Value: "/", + Usage: "web prefix", + }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "listen", + Value: "[::]", + Usage: "IP for listen at", + }), + altsrc.NewIntFlag(&cli.IntFlag{ + Name: "port", + Value: 8080, + Usage: "Port for listen at", + }), + }, + Action: runServer, } func runServer(c *cli.Context) error { diff --git a/main.go b/main.go index f1d4463..8d726c1 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( "os" + "reCoreD-UI/cmd/config" + "reCoreD-UI/cmd/server" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -18,17 +20,14 @@ func main() { Name: "config", Usage: "config yaml file", Aliases: []string{"c"}, - Value: "config.yaml", EnvVars: []string{"RECORED_CONFIG_FILE"}, }, - altsrc.NewStringSliceFlag(&cli.StringSliceFlag{}), - &cli.StringFlag{ + altsrc.NewStringFlag(&cli.StringFlag{ Name: "mysql-dsn", Usage: "mysql dsn", - Required: true, EnvVars: []string{"RECORED_MYSQL_DSN"}, - }, - &cli.BoolFlag{ + }), + altsrc.NewBoolFlag(&cli.BoolFlag{ Name: "debug", Usage: "enable debug mode", Value: false, @@ -38,16 +37,21 @@ func main() { } return nil }, - }, + }), } app := &cli.App{ Name: "reCoreD-UI", Usage: "Web UI for CoreDNS", + UseShortOptionHandling: true, Before: altsrc.InitInputSourceWithContext( flags, altsrc.NewYamlSourceFromFlagFunc("config"), ), Flags: flags, + Commands: []*cli.Command{ + server.Command, + config.Command, + }, } if err := app.Run(os.Args); err != nil {