Removed color flag and changed error output format

This commit is contained in:
bytedream 2022-02-28 16:38:56 +01:00
parent 9fbb3266aa
commit ef4b24f068

View file

@ -5,20 +5,17 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http" "net/http"
"os" "os"
"runtime"
"runtime/debug" "runtime/debug"
) )
var ( var (
client *http.Client client *http.Client
locale crunchyroll.LOCALE
crunchy *crunchyroll.Crunchyroll crunchy *crunchyroll.Crunchyroll
out = newLogger(false, true, true, colorFlag) out = newLogger(false, true, true)
quietFlag bool quietFlag bool
verboseFlag bool verboseFlag bool
proxyFlag string proxyFlag string
colorFlag bool
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -26,9 +23,9 @@ var rootCmd = &cobra.Command{
Short: "Download crunchyroll videos with ease", Short: "Download crunchyroll videos with ease",
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if verboseFlag { if verboseFlag {
out = newLogger(true, true, true, colorFlag) out = newLogger(true, true, true)
} else if quietFlag { } else if quietFlag {
out = newLogger(false, false, false, false) out = newLogger(false, false, false)
} }
out.DebugLog.Printf("Executing `%s` command with %d arg(s)\n", cmd.Name(), len(args)) out.DebugLog.Printf("Executing `%s` command with %d arg(s)\n", cmd.Name(), len(args))
@ -42,23 +39,21 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output") rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output")
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output") rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output")
rootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use") rootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
rootCmd.PersistentFlags().BoolVar(&colorFlag, "color", false, "Colored output. Only available on not windows systems")
} }
func Execute() { func Execute() {
rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.CompletionOptions.DisableDefaultCmd = true
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
out.Errln(r) if out.IsDev() {
// change color to red out.Err(string(debug.Stack()))
if colorFlag && runtime.GOOS != "windows" { } else {
out.ErrLog.SetOutput(&loggerWriter{original: out.ErrLog.Writer(), color: "\033[31m"}) out.Err("Unexpected error: %v", r)
} }
out.Debugln(string(debug.Stack())) os.Exit(1)
os.Exit(2)
} }
}() }()
if err := rootCmd.Execute(); err != nil { if rootCmd.Execute() != nil {
out.Fatalln(err) os.Exit(1)
} }
} }