mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Remove library & refactor cli
This commit is contained in:
parent
0fed0f8d3b
commit
8a3e42e4d1
45 changed files with 117 additions and 3687 deletions
78
commands/root.go
Normal file
78
commands/root.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/ByteDream/crunchyroll-go/v3"
|
||||
"github.com/spf13/cobra"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Version = "development"
|
||||
|
||||
var (
|
||||
client *http.Client
|
||||
crunchy *crunchyroll.Crunchyroll
|
||||
out = newLogger(false, true, true)
|
||||
|
||||
quietFlag bool
|
||||
verboseFlag bool
|
||||
|
||||
proxyFlag string
|
||||
|
||||
useragentFlag string
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "crunchy-cli",
|
||||
Version: Version,
|
||||
Short: "Download crunchyroll videos with ease. See the wiki for details about the cli and library: https://github.com/ByteDream/crunchy-cli/wiki",
|
||||
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
if verboseFlag {
|
||||
out = newLogger(true, true, true)
|
||||
} else if quietFlag {
|
||||
out = newLogger(false, false, false)
|
||||
}
|
||||
|
||||
out.Debug("Executing `%s` command with %d arg(s)", cmd.Name(), len(args))
|
||||
|
||||
client, err = createOrDefaultClient(proxyFlag, useragentFlag)
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
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().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchy-cli/%s", Version), "Useragent to do all request with")
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if out.IsDev() {
|
||||
out.Err("%v: %s", r, debug.Stack())
|
||||
} else {
|
||||
out.Err("Unexpected error: %v", r)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
if !strings.HasSuffix(err.Error(), context.Canceled.Error()) {
|
||||
out.Exit("An error occurred: %v", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue