Files
gmbase/cmd/mbasedump/util.go
T
2026-06-06 18:48:40 +02:00

64 lines
1.4 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
)
type Util struct {
rootCmd *cobra.Command
dumpDatabaseParams dumpDatabaseParams
restoreDatabaseParams restoreDatabaseParams
}
func NewUtil() *Util {
return &Util{}
}
func (util *Util) GetRooCmd() *cobra.Command {
return util.rootCmd
}
func (util *Util) Build() error {
var err error
execName := filepath.Base(os.Args[0])
rootCmd := &cobra.Command{
Use: execName,
Short: "\nDump application database",
SilenceUsage: true,
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
var dumpDatabaseCmd = &cobra.Command{
Use: "dump [filename|-]",
Short: "Dump application database",
Args: cobra.ExactArgs(1),
Run: util.DumpDatabase,
}
rootCmd.AddCommand(dumpDatabaseCmd)
var restoreDatabaseCmd = &cobra.Command{
Use: "restore [] [filename|-]",
Short: "Restore application database",
Args: cobra.ExactArgs(1),
Run: util.RestoreDatabase,
}
restoreDatabaseCmd.Flags().BoolVarP(&util.restoreDatabaseParams.DeleteAllRecords, "clean", "C", false, "Clean all record")
rootCmd.AddCommand(restoreDatabaseCmd)
util.rootCmd = rootCmd
return err
}
func (util *Util) Exec(args []string) error {
var err error
util.rootCmd.SetArgs(args)
err = util.rootCmd.Execute()
return err
}