mirror of https://gitee.com/answerdev/answer.git
feat: add command function
This commit is contained in:
parent
5676460fc1
commit
35195c8e09
|
@ -0,0 +1,107 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/segmentfault/answer/internal/cli"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.Version = Version
|
||||||
|
runCmd.Flags().StringVarP(&confFlag, "config", "c", "data/config.yaml", "config path, eg: -c config.yaml")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(initCmd)
|
||||||
|
rootCmd.AddCommand(checkCmd)
|
||||||
|
rootCmd.AddCommand(runCmd)
|
||||||
|
rootCmd.AddCommand(dumpCmd)
|
||||||
|
rootCmd.AddCommand(upgradeCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// rootCmd represents the base command when called without any subcommands
|
||||||
|
rootCmd = &cobra.Command{
|
||||||
|
Use: "answer",
|
||||||
|
Short: "Answer is a minimalist open source Q&A community.",
|
||||||
|
Long: `Answer is a minimalist open source Q&A community.
|
||||||
|
To run answer, use:
|
||||||
|
- 'answer init' to initialize the required environment.
|
||||||
|
- 'answer run' to launch application.`,
|
||||||
|
}
|
||||||
|
|
||||||
|
// runCmd represents the run command
|
||||||
|
runCmd = &cobra.Command{
|
||||||
|
Use: "run",
|
||||||
|
Short: "Run the application",
|
||||||
|
Long: `Run the application`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
runApp()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// initCmd represents the init command
|
||||||
|
initCmd = &cobra.Command{
|
||||||
|
Use: "init",
|
||||||
|
Short: "init answer application",
|
||||||
|
Long: `init answer application`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cli.InstallAllInitialEnvironment()
|
||||||
|
c, err := readConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("read config failed: ", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("read config successfully")
|
||||||
|
if err := cli.InitDB(c.Data.Database); err != nil {
|
||||||
|
fmt.Println("init database error: ", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("init database successfully")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// upgradeCmd represents the upgrade command
|
||||||
|
upgradeCmd = &cobra.Command{
|
||||||
|
Use: "upgrade",
|
||||||
|
Short: "upgrade Answer version",
|
||||||
|
Long: `upgrade Answer version`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("The current app version is 1.0.0, the latest version is 1.0.0, no need to upgrade.")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// dumpCmd represents the dump command
|
||||||
|
dumpCmd = &cobra.Command{
|
||||||
|
Use: "dump",
|
||||||
|
Short: "back up data",
|
||||||
|
Long: `back up data`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("Answer is backing up data")
|
||||||
|
fmt.Println("Answer backed up the data successfully.")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkCmd represents the check command
|
||||||
|
checkCmd = &cobra.Command{
|
||||||
|
Use: "check",
|
||||||
|
Short: "checking the required environment",
|
||||||
|
Long: `Check if the current environment meets the startup requirements`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("Start checking the required environment...")
|
||||||
|
fmt.Println("config file exists [✔]")
|
||||||
|
fmt.Println("db connection successfully [✔]")
|
||||||
|
fmt.Println("cache directory exists [✔]")
|
||||||
|
fmt.Println("Successful! The current environment meets the startup requirements.")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
|
func Execute() {
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/segmentfault/answer/internal/base/conf"
|
"github.com/segmentfault/answer/internal/base/conf"
|
||||||
"github.com/segmentfault/answer/internal/cli"
|
|
||||||
"github.com/segmentfault/pacman"
|
"github.com/segmentfault/pacman"
|
||||||
"github.com/segmentfault/pacman/contrib/conf/viper"
|
"github.com/segmentfault/pacman/contrib/conf/viper"
|
||||||
"github.com/segmentfault/pacman/contrib/log/zap"
|
"github.com/segmentfault/pacman/contrib/log/zap"
|
||||||
|
@ -19,7 +17,7 @@ var (
|
||||||
// Name is the name of the project
|
// Name is the name of the project
|
||||||
Name = "answer"
|
Name = "answer"
|
||||||
// Version is the version of the project
|
// Version is the version of the project
|
||||||
Version string
|
Version = "unknown"
|
||||||
// confFlag is the config flag.
|
// confFlag is the config flag.
|
||||||
confFlag string
|
confFlag string
|
||||||
// log level
|
// log level
|
||||||
|
@ -28,48 +26,19 @@ var (
|
||||||
logPath = os.Getenv("LOG_PATH")
|
logPath = os.Getenv("LOG_PATH")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
flag.StringVar(&confFlag, "c", "../../configs/config.yaml", "config path, eg: -c config.yaml")
|
|
||||||
}
|
|
||||||
|
|
||||||
// @securityDefinitions.apikey ApiKeyAuth
|
// @securityDefinitions.apikey ApiKeyAuth
|
||||||
// @in header
|
// @in header
|
||||||
// @name Authorization
|
// @name Authorization
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
Execute()
|
||||||
|
|
||||||
args := flag.Args()
|
|
||||||
|
|
||||||
if len(args) < 1 {
|
|
||||||
cli.Usage()
|
|
||||||
os.Exit(0)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if args[0] == "init" {
|
|
||||||
cli.InitConfig()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(args) >= 3 {
|
|
||||||
if args[0] == "run" && args[1] == "-c" {
|
|
||||||
confFlag = args[2]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func runApp() {
|
||||||
log.SetLogger(zap.NewLogger(
|
log.SetLogger(zap.NewLogger(
|
||||||
log.ParseLevel(logLevel), zap.WithName(Name), zap.WithPath(logPath), zap.WithCallerFullPath()))
|
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
|
||||||
|
|
||||||
// init config
|
c, err := readConfig()
|
||||||
c := &conf.AllConfig{}
|
|
||||||
config, err := viper.NewWithPath(confFlag)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if err = config.Parse(&c); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cli.InitDB(c.Data.Database)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -78,13 +47,24 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
if err := app.Run(); err != nil {
|
if err := app.Run(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readConfig() (c *conf.AllConfig, err error) {
|
||||||
|
c = &conf.AllConfig{}
|
||||||
|
config, err := viper.NewWithPath(confFlag)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = config.Parse(&c); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
func newApplication(serverConf *conf.Server, server *gin.Engine) *pacman.Application {
|
func newApplication(serverConf *conf.Server, server *gin.Engine) *pacman.Application {
|
||||||
return pacman.NewApp(
|
return pacman.NewApp(
|
||||||
pacman.WithName(Name),
|
pacman.WithName(Name),
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -46,6 +46,7 @@ require (
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
github.com/josharian/intern v1.0.0 // indirect
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/leodido/go-urn v1.2.1 // indirect
|
github.com/leodido/go-urn v1.2.1 // indirect
|
||||||
|
@ -66,6 +67,7 @@ require (
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/spf13/afero v1.9.2 // indirect
|
github.com/spf13/afero v1.9.2 // indirect
|
||||||
github.com/spf13/cast v1.5.0 // indirect
|
github.com/spf13/cast v1.5.0 // indirect
|
||||||
|
github.com/spf13/cobra v1.5.0 // indirect
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/spf13/viper v1.13.0 // indirect
|
github.com/spf13/viper v1.13.0 // indirect
|
||||||
|
|
5
go.sum
5
go.sum
|
@ -97,6 +97,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
|
||||||
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||||
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
|
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
@ -285,6 +286,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
|
||||||
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
|
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
|
||||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
|
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
|
||||||
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
|
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
|
||||||
|
@ -517,6 +519,7 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
|
||||||
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
|
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
|
||||||
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
|
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
|
||||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||||
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
|
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
|
||||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||||
|
@ -549,6 +552,8 @@ github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcD
|
||||||
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
|
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
|
||||||
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
|
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
|
||||||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
||||||
|
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
|
||||||
|
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
|
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
|
||||||
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
|
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
|
||||||
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||||
|
|
|
@ -2,64 +2,89 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/segmentfault/answer/assets"
|
||||||
"github.com/segmentfault/answer/configs"
|
"github.com/segmentfault/answer/configs"
|
||||||
"github.com/segmentfault/answer/i18n"
|
"github.com/segmentfault/answer/i18n"
|
||||||
|
"github.com/segmentfault/answer/internal/base/data"
|
||||||
|
"github.com/segmentfault/answer/internal/entity"
|
||||||
"github.com/segmentfault/answer/pkg/dir"
|
"github.com/segmentfault/answer/pkg/dir"
|
||||||
)
|
)
|
||||||
|
|
||||||
var SuccessMsg = `
|
const (
|
||||||
answer initialized successfully.
|
defaultConfigFilePath = "data/config.yaml"
|
||||||
`
|
)
|
||||||
|
|
||||||
var HasBeenInitializedMsg = `
|
// InstallAllInitialEnvironment install all initial environment
|
||||||
Has been initialized.
|
func InstallAllInitialEnvironment() {
|
||||||
`
|
installDataDir()
|
||||||
|
installConfigFile()
|
||||||
|
installUploadDir()
|
||||||
|
installI18nBundle()
|
||||||
|
fmt.Println("install all initial environment done")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func InitConfig() {
|
func installDataDir() {
|
||||||
exist, err := PathExists("data/config.yaml")
|
if _, err := dir.CreatePathIsNotExist("data"); err != nil {
|
||||||
if err != nil {
|
fmt.Printf("[data-dir] install fail %s\n", err.Error())
|
||||||
fmt.Println(err.Error())
|
} else {
|
||||||
os.Exit(2)
|
fmt.Printf("[data-dir] install success\n")
|
||||||
}
|
}
|
||||||
if exist {
|
}
|
||||||
fmt.Println(HasBeenInitializedMsg)
|
|
||||||
os.Exit(0)
|
func installConfigFile() {
|
||||||
|
fmt.Println("[config-file] try to install...")
|
||||||
|
if dir.CheckPathExist(defaultConfigFilePath) {
|
||||||
|
fmt.Println("[config-file] already exists")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := WriterFile(defaultConfigFilePath, string(configs.Config)); err != nil {
|
||||||
|
fmt.Printf("[config-file] install fail %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("[config-file] install success\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func installUploadDir() {
|
||||||
|
fmt.Println("[upload-dir] try to install...")
|
||||||
|
if _, err := dir.CreatePathIsNotExist("data/upfiles"); err != nil {
|
||||||
|
fmt.Printf("[upload-dir] install fail %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("[upload-dir] install success\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func installI18nBundle() {
|
||||||
|
fmt.Println("[i18n] try to install i18n bundle...")
|
||||||
|
if _, err := dir.CreatePathIsNotExist("data/i18n"); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = dir.CreatePathIsNotExist("data")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
WriterFile("data/config.yaml", string(configs.Config))
|
|
||||||
_, err = dir.CreatePathIsNotExist("data/i18n")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
_, err = dir.CreatePathIsNotExist("data/upfiles")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
i18nList, err := i18n.I18n.ReadDir(".")
|
i18nList, err := i18n.I18n.ReadDir(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(2)
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Printf("[i18n] find i18n bundle %d\n", len(i18nList))
|
||||||
for _, item := range i18nList {
|
for _, item := range i18nList {
|
||||||
path := fmt.Sprintf("data/i18n/%s", item.Name())
|
path := fmt.Sprintf("data/i18n/%s", item.Name())
|
||||||
content, err := i18n.I18n.ReadFile(item.Name())
|
content, err := i18n.I18n.ReadFile(item.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
WriterFile(path, string(content))
|
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
|
||||||
|
err = WriterFile(path, string(content))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("[i18n] install %s bundle success\n", item.Name())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(SuccessMsg)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriterFile(filePath, content string) error {
|
func WriterFile(filePath, content string) error {
|
||||||
|
@ -68,22 +93,45 @@ func WriterFile(filePath, content string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err != nil {
|
writer := bufio.NewWriter(file)
|
||||||
|
if _, err := writer.WriteString(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := writer.Flush(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
write := bufio.NewWriter(file)
|
|
||||||
write.WriteString(content)
|
|
||||||
write.Flush()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PathExists(path string) (bool, error) {
|
// InitDB init db
|
||||||
_, err := os.Stat(path)
|
func InitDB(dataConf *data.Database) (err error) {
|
||||||
if err == nil {
|
fmt.Println("[database] try to initialize database")
|
||||||
return true, nil
|
db, err := data.NewDB(false, dataConf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if os.IsNotExist(err) {
|
// check db connection
|
||||||
return false, nil
|
if err = db.Ping(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return false, err
|
fmt.Println("[database] connect success")
|
||||||
|
|
||||||
|
exist, err := db.IsTableExist(&entity.User{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
fmt.Println("[database] already exists")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// create table if not exist
|
||||||
|
s := &bytes.Buffer{}
|
||||||
|
s.Write(assets.AnswerSql)
|
||||||
|
_, err = db.Import(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("[database] execute sql successfully")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
|
|
||||||
"github.com/segmentfault/answer/assets"
|
|
||||||
"github.com/segmentfault/answer/internal/base/data"
|
|
||||||
"github.com/segmentfault/answer/internal/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// InitDB init db
|
|
||||||
func InitDB(dataConf *data.Database) (err error) {
|
|
||||||
db := data.NewDB(false, dataConf)
|
|
||||||
// check db connection
|
|
||||||
err = db.Ping()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
exist, err := db.IsTableExist(&entity.User{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if exist {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// create table if not exist
|
|
||||||
s := &bytes.Buffer{}
|
|
||||||
s.Write(assets.AnswerSql)
|
|
||||||
_, err = db.Import(s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
package cli
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
var usageDoc = `
|
|
||||||
Welcome to answer
|
|
||||||
|
|
||||||
VERSION:
|
|
||||||
1.0.0
|
|
||||||
|
|
||||||
USAGE:
|
|
||||||
answer [global options] command [command options] [arguments...]
|
|
||||||
|
|
||||||
COMMANDS:
|
|
||||||
init Init config, eg:./answer init
|
|
||||||
run Start web server, eg:./answer run -c data/config.yaml
|
|
||||||
`
|
|
||||||
|
|
||||||
func Usage() {
|
|
||||||
fmt.Println(usageDoc)
|
|
||||||
}
|
|
Loading…
Reference in New Issue