Files
gosqldump/cmd/root.go

76 lines
1.7 KiB
Go
Raw Normal View History

2024-08-30 11:25:28 +12:00
package cmd
import (
"fmt"
"github.com/charmbracelet/log"
"hub.cybercinch.nz/guisea/go-template/internal/style"
"os"
"strings"
cc "github.com/ivanpirog/coloredcobra"
"github.com/samber/lo"
"github.com/spf13/cobra"
"hub.cybercinch.nz/guisea/go-template/internal/app"
"hub.cybercinch.nz/guisea/go-template/internal/filesystem"
"hub.cybercinch.nz/guisea/go-template/internal/icon"
"hub.cybercinch.nz/guisea/go-template/internal/where"
)
func init() {
rootCmd.Flags().BoolP("version", "v", false, app.Name+" version")
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: strings.ToLower(app.Name),
Short: app.DescriptionShort,
Long: app.DescriptionLong,
Run: func(cmd *cobra.Command, args []string) {
if lo.Must(cmd.Flags().GetBool("version")) {
versionCmd.Run(versionCmd, args)
} else {
_ = cmd.Help()
}
},
}
func Execute() {
// Setup colored cobra
cc.Init(&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiCyan + cc.Bold + cc.Underline,
Commands: cc.HiYellow + cc.Bold,
Example: cc.Italic,
ExecName: cc.Bold,
Flags: cc.Bold,
FlagsDataType: cc.Italic + cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
// Clears temp files on each run.
// It should not affect startup time since it's being run as goroutine.
go func() {
_ = filesystem.Api().RemoveAll(where.Temp())
}()
_ = rootCmd.Execute()
}
// handleErr will stop program execution and logger error to the stderr
// if err is not nil
func handleErr(err error) {
if err == nil {
return
}
log.Error(err)
_, _ = fmt.Fprintf(
os.Stderr,
"%s %s\n",
style.Failure(icon.Cross),
strings.Trim(err.Error(), " \n"),
)
os.Exit(1)
}