2023-01-18 15:37:22 +08:00
|
|
|
package answercmd
|
2022-10-12 11:12:04 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-01-29 16:06:17 +08:00
|
|
|
"strings"
|
2022-10-12 11:12:04 +08:00
|
|
|
|
2022-11-03 20:09:04 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/conf"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/cli"
|
2022-11-03 20:09:04 +08:00
|
|
|
"github.com/answerdev/answer/internal/install"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/migrations"
|
2023-01-29 16:06:17 +08:00
|
|
|
"github.com/answerdev/answer/plugin"
|
2023-05-17 18:13:03 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2022-10-12 11:12:04 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-10-12 11:49:28 +08:00
|
|
|
var (
|
2022-10-13 17:36:50 +08:00
|
|
|
// dataDirPath save all answer application data in this directory. like config file, upload file...
|
|
|
|
dataDirPath string
|
2022-10-12 11:49:28 +08:00
|
|
|
// dumpDataPath dump data path
|
|
|
|
dumpDataPath string
|
2023-01-29 16:06:17 +08:00
|
|
|
// plugins needed to build in answer application
|
|
|
|
buildWithPlugins []string
|
|
|
|
// build output path
|
|
|
|
buildOutput string
|
2023-05-17 18:13:03 +08:00
|
|
|
// This config is used to upgrade the database from a specific version manually.
|
|
|
|
// If you want to upgrade the database to version 1.1.0, you can use `answer upgrade -f v1.1.0`.
|
|
|
|
upgradeVersion string
|
2022-10-12 11:49:28 +08:00
|
|
|
)
|
|
|
|
|
2022-10-12 11:12:04 +08:00
|
|
|
func init() {
|
2022-10-18 12:19:07 +08:00
|
|
|
rootCmd.Version = fmt.Sprintf("%s\nrevision: %s\nbuild time: %s", Version, Revision, Time)
|
2022-10-13 17:36:50 +08:00
|
|
|
|
2022-11-04 18:36:33 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./data/")
|
2022-10-12 11:12:04 +08:00
|
|
|
|
2022-10-12 11:49:28 +08:00
|
|
|
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
|
|
|
|
2023-01-29 16:06:17 +08:00
|
|
|
buildCmd.Flags().StringSliceVarP(&buildWithPlugins, "with", "w", []string{}, "plugins needed to build")
|
|
|
|
|
|
|
|
buildCmd.Flags().StringVarP(&buildOutput, "output", "o", "", "build output path")
|
|
|
|
|
2023-05-17 18:13:03 +08:00
|
|
|
upgradeCmd.Flags().StringVarP(&upgradeVersion, "from", "f", "", "upgrade from specific version, eg: -f v1.1.0")
|
2023-05-11 11:55:39 +08:00
|
|
|
|
2023-01-29 16:06:17 +08:00
|
|
|
for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, dumpCmd, upgradeCmd, buildCmd, pluginCmd} {
|
2022-10-26 18:09:39 +08:00
|
|
|
rootCmd.AddCommand(cmd)
|
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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`,
|
2022-11-01 15:25:44 +08:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2022-11-04 18:36:33 +08:00
|
|
|
cli.FormatAllPath(dataDirPath)
|
|
|
|
fmt.Println("config file path: ", cli.GetConfigFilePath())
|
2022-11-04 17:26:16 +08:00
|
|
|
fmt.Println("Answer is string..........................")
|
2022-10-12 11:12:04 +08:00
|
|
|
runApp()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// initCmd represents the init command
|
|
|
|
initCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "init answer application",
|
|
|
|
Long: `init answer application`,
|
2022-11-01 15:25:44 +08:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2022-11-03 20:09:04 +08:00
|
|
|
// check config file and database. if config file exists and database is already created, init done
|
2022-10-13 17:36:50 +08:00
|
|
|
cli.InstallAllInitialEnvironment(dataDirPath)
|
2022-11-03 20:09:04 +08:00
|
|
|
|
2022-11-04 18:36:33 +08:00
|
|
|
configFileExist := cli.CheckConfigFile(cli.GetConfigFilePath())
|
2022-11-03 20:09:04 +08:00
|
|
|
if configFileExist {
|
|
|
|
fmt.Println("config file exists, try to read the config...")
|
2022-11-04 18:36:33 +08:00
|
|
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
2022-11-03 20:09:04 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("read config failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("config file read successfully, try to connect database...")
|
2022-11-10 14:13:29 +08:00
|
|
|
if cli.CheckDBTableExist(c.Data.Database) {
|
2022-11-03 20:09:04 +08:00
|
|
|
fmt.Println("connect to database successfully and table already exists, do nothing.")
|
|
|
|
return
|
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
}
|
2022-11-03 20:09:04 +08:00
|
|
|
|
|
|
|
// start installation server to install
|
2022-11-04 18:36:33 +08:00
|
|
|
install.Run(cli.GetConfigFilePath())
|
2022-10-12 11:12:04 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgradeCmd represents the upgrade command
|
|
|
|
upgradeCmd = &cobra.Command{
|
|
|
|
Use: "upgrade",
|
|
|
|
Short: "upgrade Answer version",
|
|
|
|
Long: `upgrade Answer version`,
|
2022-11-01 15:25:44 +08:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2023-05-17 18:13:03 +08:00
|
|
|
log.SetLogger(log.NewStdLogger(os.Stdout))
|
2022-11-04 18:36:33 +08:00
|
|
|
cli.FormatAllPath(dataDirPath)
|
2023-05-11 11:36:23 +08:00
|
|
|
cli.InstallI18nBundle(true)
|
2022-11-04 18:36:33 +08:00
|
|
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
2022-10-18 15:26:48 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("read config failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-07-31 15:04:32 +08:00
|
|
|
if err = migrations.Migrate(c.Debug, c.Data.Database, c.Data.Cache, upgradeVersion); err != nil {
|
2022-10-18 15:26:48 +08:00
|
|
|
fmt.Println("migrate failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("upgrade done")
|
2022-10-12 11:12:04 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// dumpCmd represents the dump command
|
|
|
|
dumpCmd = &cobra.Command{
|
|
|
|
Use: "dump",
|
|
|
|
Short: "back up data",
|
|
|
|
Long: `back up data`,
|
2022-11-01 15:25:44 +08:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Println("Answer is backing up data")
|
2022-11-04 18:36:33 +08:00
|
|
|
cli.FormatAllPath(dataDirPath)
|
|
|
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
2022-10-12 11:49:28 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("read config failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = cli.DumpAllData(c.Data.Database, dumpDataPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("dump failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
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`,
|
2022-11-01 15:25:44 +08:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2022-11-04 18:36:33 +08:00
|
|
|
cli.FormatAllPath(dataDirPath)
|
2022-10-12 11:12:04 +08:00
|
|
|
fmt.Println("Start checking the required environment...")
|
2022-11-04 18:36:33 +08:00
|
|
|
if cli.CheckConfigFile(cli.GetConfigFilePath()) {
|
2022-10-12 11:49:28 +08:00
|
|
|
fmt.Println("config file exists [✔]")
|
|
|
|
} else {
|
|
|
|
fmt.Println("config file not exists [x]")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.CheckUploadDir() {
|
|
|
|
fmt.Println("upload directory exists [✔]")
|
|
|
|
} else {
|
|
|
|
fmt.Println("upload directory not exists [x]")
|
|
|
|
}
|
|
|
|
|
2022-11-04 18:36:33 +08:00
|
|
|
c, err := conf.ReadConfig(cli.GetConfigFilePath())
|
2022-10-12 11:49:28 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("read config failed: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:13:29 +08:00
|
|
|
if cli.CheckDBConnection(c.Data.Database) {
|
2022-10-12 11:49:28 +08:00
|
|
|
fmt.Println("db connection successfully [✔]")
|
|
|
|
} else {
|
|
|
|
fmt.Println("db connection failed [x]")
|
|
|
|
}
|
|
|
|
fmt.Println("check environment all done")
|
2022-10-12 11:12:04 +08:00
|
|
|
},
|
|
|
|
}
|
2023-01-29 16:06:17 +08:00
|
|
|
|
|
|
|
// buildCmd used to build another answer with plugins
|
|
|
|
buildCmd = &cobra.Command{
|
|
|
|
Use: "build",
|
|
|
|
Short: "used to build answer with plugins",
|
|
|
|
Long: `Build a new Answer with plugins that you need`,
|
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
|
|
fmt.Printf("try to build a new answer with plugins:\n%s\n", strings.Join(buildWithPlugins, "\n"))
|
|
|
|
err := cli.BuildNewAnswer(buildOutput, buildWithPlugins, cli.OriginalAnswerInfo{
|
|
|
|
Version: Version,
|
|
|
|
Revision: Revision,
|
|
|
|
Time: Time,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("build failed %v", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("build new answer successfully %s\n", buildOutput)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// pluginCmd prints all plugins packed in the binary
|
|
|
|
pluginCmd = &cobra.Command{
|
|
|
|
Use: "plugin",
|
|
|
|
Short: "prints all plugins packed in the binary",
|
|
|
|
Long: `prints all plugins packed in the binary`,
|
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
|
|
_ = plugin.CallBase(func(base plugin.Base) error {
|
|
|
|
info := base.Info()
|
2023-02-21 17:28:05 +08:00
|
|
|
fmt.Printf("%s[%s] made by %s\n", info.SlugName, info.Version, info.Author)
|
2023-01-29 16:06:17 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
2022-10-12 11:12:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
2023-01-18 15:37:22 +08:00
|
|
|
// This is called by main(). It only needs to happen once to the rootCmd.
|
2022-10-12 11:12:04 +08:00
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|