github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/cmd/hkserver/commands/reset.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/masterhung0112/hk_server/v5/audit"
    13  )
    14  
    15  var ResetCmd = &cobra.Command{
    16  	Use:   "reset",
    17  	Short: "Reset the database to initial state",
    18  	Long:  "Completely erases the database causing the loss of all data. This will reset Mattermost to its initial state.",
    19  	RunE:  resetCmdF,
    20  }
    21  
    22  func init() {
    23  	ResetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
    24  
    25  	RootCmd.AddCommand(ResetCmd)
    26  }
    27  
    28  func resetCmdF(command *cobra.Command, args []string) error {
    29  	a, err := InitDBCommandContextCobra(command)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer a.Srv().Shutdown()
    34  
    35  	confirmFlag, _ := command.Flags().GetBool("confirm")
    36  	if !confirmFlag {
    37  		var confirm string
    38  		CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ")
    39  		fmt.Scanln(&confirm)
    40  
    41  		if confirm != "YES" {
    42  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    43  		}
    44  		CommandPrettyPrintln("Are you sure you want to delete everything? All data will be permanently deleted? (YES/NO): ")
    45  		fmt.Scanln(&confirm)
    46  		if confirm != "YES" {
    47  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    48  		}
    49  	}
    50  
    51  	a.Srv().Store.DropAllTables()
    52  	CommandPrettyPrintln("Database successfully reset")
    53  
    54  	auditRec := a.MakeAuditRecord("reset", audit.Success)
    55  	a.LogAuditRec(auditRec, nil)
    56  
    57  	return nil
    58  }