2020package answercmd
2121
2222import (
23+ "context"
2324 "fmt"
2425 "os"
2526 "strings"
2627
2728 "github.com/apache/answer/internal/base/conf"
29+ "github.com/apache/answer/internal/base/path"
2830 "github.com/apache/answer/internal/cli"
2931 "github.com/apache/answer/internal/install"
3032 "github.com/apache/answer/internal/migrations"
5355 i18nSourcePath string
5456 // i18nTargetPath i18n to path
5557 i18nTargetPath string
58+ // resetPasswordEmail user email for password reset
59+ resetPasswordEmail string
60+ // resetPasswordPassword new password for password reset
61+ resetPasswordPassword string
5662)
5763
5864func init () {
@@ -76,7 +82,10 @@ func init() {
7682
7783 i18nCmd .Flags ().StringVarP (& i18nTargetPath , "target" , "t" , "" , "i18n target path, eg: -t ./i18n/target" )
7884
79- for _ , cmd := range []* cobra.Command {initCmd , checkCmd , runCmd , dumpCmd , upgradeCmd , buildCmd , pluginCmd , configCmd , i18nCmd } {
85+ resetPasswordCmd .Flags ().StringVarP (& resetPasswordEmail , "email" , "e" , "" , "user email address" )
86+ resetPasswordCmd .Flags ().StringVarP (& resetPasswordPassword , "password" , "p" , "" , "new password (not recommended, will be recorded in shell history)" )
87+
88+ for _ , cmd := range []* cobra.Command {initCmd , checkCmd , runCmd , dumpCmd , upgradeCmd , buildCmd , pluginCmd , configCmd , i18nCmd , resetPasswordCmd } {
8089 rootCmd .AddCommand (cmd )
8190 }
8291}
@@ -96,8 +105,8 @@ To run answer, use:
96105 Short : "Run Answer" ,
97106 Long : `Start running Answer` ,
98107 Run : func (_ * cobra.Command , _ []string ) {
99- cli .FormatAllPath (dataDirPath )
100- fmt .Println ("config file path: " , cli .GetConfigFilePath ())
108+ path .FormatAllPath (dataDirPath )
109+ fmt .Println ("config file path: " , path .GetConfigFilePath ())
101110 fmt .Println ("Answer is starting.........................." )
102111 runApp ()
103112 },
@@ -111,10 +120,10 @@ To run answer, use:
111120 // check config file and database. if config file exists and database is already created, init done
112121 cli .InstallAllInitialEnvironment (dataDirPath )
113122
114- configFileExist := cli .CheckConfigFile (cli .GetConfigFilePath ())
123+ configFileExist := cli .CheckConfigFile (path .GetConfigFilePath ())
115124 if configFileExist {
116125 fmt .Println ("config file exists, try to read the config..." )
117- c , err := conf .ReadConfig (cli .GetConfigFilePath ())
126+ c , err := conf .ReadConfig (path .GetConfigFilePath ())
118127 if err != nil {
119128 fmt .Println ("read config failed: " , err .Error ())
120129 return
@@ -128,7 +137,7 @@ To run answer, use:
128137 }
129138
130139 // start installation server to install
131- install .Run (cli .GetConfigFilePath ())
140+ install .Run (path .GetConfigFilePath ())
132141 },
133142 }
134143
@@ -138,9 +147,9 @@ To run answer, use:
138147 Long : `Upgrade Answer to the latest version` ,
139148 Run : func (_ * cobra.Command , _ []string ) {
140149 log .SetLogger (log .NewStdLogger (os .Stdout ))
141- cli .FormatAllPath (dataDirPath )
150+ path .FormatAllPath (dataDirPath )
142151 cli .InstallI18nBundle (true )
143- c , err := conf .ReadConfig (cli .GetConfigFilePath ())
152+ c , err := conf .ReadConfig (path .GetConfigFilePath ())
144153 if err != nil {
145154 fmt .Println ("read config failed: " , err .Error ())
146155 return
@@ -159,8 +168,8 @@ To run answer, use:
159168 Long : `Back up database into an SQL file` ,
160169 Run : func (_ * cobra.Command , _ []string ) {
161170 fmt .Println ("Answer is backing up data" )
162- cli .FormatAllPath (dataDirPath )
163- c , err := conf .ReadConfig (cli .GetConfigFilePath ())
171+ path .FormatAllPath (dataDirPath )
172+ c , err := conf .ReadConfig (path .GetConfigFilePath ())
164173 if err != nil {
165174 fmt .Println ("read config failed: " , err .Error ())
166175 return
@@ -179,9 +188,9 @@ To run answer, use:
179188 Short : "Check the required environment" ,
180189 Long : `Check if the current environment meets the startup requirements` ,
181190 Run : func (_ * cobra.Command , _ []string ) {
182- cli .FormatAllPath (dataDirPath )
191+ path .FormatAllPath (dataDirPath )
183192 fmt .Println ("Start checking the required environment..." )
184- if cli .CheckConfigFile (cli .GetConfigFilePath ()) {
193+ if cli .CheckConfigFile (path .GetConfigFilePath ()) {
185194 fmt .Println ("config file exists [✔]" )
186195 } else {
187196 fmt .Println ("config file not exists [x]" )
@@ -193,7 +202,7 @@ To run answer, use:
193202 fmt .Println ("upload directory not exists [x]" )
194203 }
195204
196- c , err := conf .ReadConfig (cli .GetConfigFilePath ())
205+ c , err := conf .ReadConfig (path .GetConfigFilePath ())
197206 if err != nil {
198207 fmt .Println ("read config failed: " , err .Error ())
199208 return
@@ -246,9 +255,9 @@ To run answer, use:
246255 Short : "Set some config to default value" ,
247256 Long : `Set some config to default value` ,
248257 Run : func (_ * cobra.Command , _ []string ) {
249- cli .FormatAllPath (dataDirPath )
258+ path .FormatAllPath (dataDirPath )
250259
251- c , err := conf .ReadConfig (cli .GetConfigFilePath ())
260+ c , err := conf .ReadConfig (path .GetConfigFilePath ())
252261 if err != nil {
253262 fmt .Println ("read config failed: " , err .Error ())
254263 return
@@ -297,6 +306,32 @@ To run answer, use:
297306 }
298307 },
299308 }
309+
310+ resetPasswordCmd = & cobra.Command {
311+ Use : "passwd" ,
312+ Aliases : []string {"password" , "reset-password" },
313+ Short : "Reset user password" ,
314+ Long : "Reset user password by email address." ,
315+ Example : ` # Interactive mode (recommended, safest)
316+ answer passwd -C ./answer-data
317+
318+ # Specify email only (will prompt for password securely)
319+ answer passwd -C ./answer-data --email user@example.com
320+ answer passwd -C ./answer-data -e user@example.com
321+
322+ # Specify email and password (NOT recommended, will be recorded in shell history)
323+ answer passwd -C ./answer-data -e user@example.com -p newpassword123` ,
324+ Run : func (cmd * cobra.Command , args []string ) {
325+ opts := & cli.ResetPasswordOptions {
326+ Email : resetPasswordEmail ,
327+ Password : resetPasswordPassword ,
328+ }
329+ if err := cli .ResetPassword (context .Background (), dataDirPath , opts ); err != nil {
330+ fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
331+ os .Exit (1 )
332+ }
333+ },
334+ }
300335)
301336
302337// Execute adds all child commands to the root command and sets flags appropriately.
0 commit comments