Files
gosqldump/cmd/dump.go
Aaron Guise 0cbfb81096
All checks were successful
CI / build (push) Successful in 3m12s
fix: Spelling update/fixed zip creation. 🐛
closes #3
closes #1
2024-09-03 11:08:27 +12:00

102 lines
2.8 KiB
Go

package cmd
import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"hub.cybercinch.nz/guisea/gosqldump/internal/color"
"hub.cybercinch.nz/guisea/gosqldump/internal/dump"
"hub.cybercinch.nz/guisea/gosqldump/internal/icon"
"hub.cybercinch.nz/guisea/gosqldump/internal/style"
"time"
)
// dumpCmd represents the dump command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Use this command to initiate dumping of MySQL/MariaDB Database",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
log.Info("dump called")
start := time.Now()
hostname, _ := cmd.Flags().GetString("host")
databaseName, _ := cmd.Flags().GetString("database")
//port, _ := cmd.Flags().GetInt("port")
// Retrieve user from config
user := viper.GetViper().GetString("mysql.user")
// Retrieve password from config
pass := viper.GetViper().GetString("mysql.password")
myDump := dump.NewClient(
dump.WithHostName(hostname),
dump.WithDatabaseName(databaseName),
dump.WithPort(3306),
dump.WithUserName(user),
dump.WithPassword(pass),
)
err := myDump.Dump()
if err != nil {
fmt.Printf("Error: %s", err)
return
}
fmt.Printf(
"%s Combining files\n\r",
style.Success(icon.Info),
)
filename, err := myDump.Combine()
if err != nil {
if err != nil {
fmt.Printf(
"%s could not combine backup file: %s\n\r",
style.Failure(icon.Cross),
lipgloss.NewStyle().Foreground(color.Red).Render(err.Error()),
)
return
}
return
}
fmt.Printf(
"%s Completed combining files\n\r",
style.Success(icon.Check),
)
fmt.Printf(
"%s Zipping file\n\r",
style.Success(icon.Info),
)
filename, _ = dump.ZipFile(filename)
timeElapsed := time.Since(start)
fmt.Printf(
"%s Dump file available at %s\r\nTook %s\n\r",
style.Success(icon.Check),
filename,
timeElapsed,
)
},
}
func init() {
rootCmd.AddCommand(dumpCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// dumpCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// dumpCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
dumpCmd.Flags().StringP("host", "H", "localhost", "database host")
dumpCmd.Flags().StringP("port", "p", "3306", "database port")
dumpCmd.Flags().StringP("database", "d", "~", "database name")
}