From 4a5ec69664ba6991f06a5e57a1a80d76e2365567 Mon Sep 17 00:00:00 2001 From: Yxnt Date: Tue, 4 Jun 2024 19:27:34 +0800 Subject: [PATCH] =?UTF-8?q?new=20url=20=E5=8A=9F=E8=83=BD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20json=20=E6=97=A5=E5=BF=97=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yxnt --- cmd/new/url/url.go | 55 +++++++++++++-------- cmd/root.go | 11 +++-- internal/pikpak/url.go | 14 +++--- internal/utils/log.go | 110 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 internal/utils/log.go diff --git a/cmd/new/url/url.go b/cmd/new/url/url.go index 698b161..29be757 100644 --- a/cmd/new/url/url.go +++ b/cmd/new/url/url.go @@ -9,18 +9,34 @@ import ( "github.com/52funny/pikpakcli/conf" "github.com/52funny/pikpakcli/internal/pikpak" - "github.com/sirupsen/logrus" + "github.com/52funny/pikpakcli/internal/utils" "github.com/spf13/cobra" ) +var ( + path, parentId, input, outputFormatVal string + cli bool + err error + logClient utils.Log +) + var NewUrlCommand = &cobra.Command{ Use: "url", Short: `Create a file according to url`, Run: func(cmd *cobra.Command, args []string) { + + flagset := cmd.InheritedFlags() + outputFormatVal, err = flagset.GetString("output") + if err != nil { + panic(err) + } + + logClient = utils.NewLog(outputFormatVal) + p := pikpak.NewPikPak(conf.Config.Username, conf.Config.Password) - err := p.Login() + err = p.Login() if err != nil { - logrus.Errorln("Login Failed:", err) + logClient.Errorf("Login Failed: %v", err) } if cli { handleCli(&p) @@ -30,7 +46,7 @@ var NewUrlCommand = &cobra.Command{ if strings.TrimSpace(input) != "" { f, err := os.OpenFile(input, os.O_RDONLY, 0666) if err != nil { - logrus.Errorln("Open file %s failed:", input, err) + logClient.Errorf("Open file %s failed: %v", input, err) return } reader := bufio.NewReader(f) @@ -50,19 +66,11 @@ var NewUrlCommand = &cobra.Command{ if len(args) > 0 { handleNewUrl(&p, args) } else { - logrus.Errorln("Please input the folder name") + logClient.Error("Please input the folder name") } }, } -var path string - -var parentId string - -var input string - -var cli bool - func init() { NewUrlCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder") NewUrlCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id") @@ -76,18 +84,21 @@ func handleNewUrl(p *pikpak.PikPak, shas []string) { if parentId == "" { parentId, err = p.GetPathFolderId(path) if err != nil { - logrus.Errorf("Get parent id failed: %s\n", err) + logClient.Errorf("Get parent id failed: %v\n", err) return } } for _, url := range shas { - err := p.CreateUrlFile(parentId, url) + taskInfo, err := p.CreateUrlFile(parentId, url) if err != nil { - logrus.Errorln("Create url file failed: ", err) + logClient.Errorf("Create url file failed: %v\n", err) continue } - logrus.Infoln("Create url file success: ", url) + + logClient.Infof("Create url file success: %s\n", url) + logClient.Info(taskInfo) } + } func handleCli(p *pikpak.PikPak) { @@ -95,7 +106,7 @@ func handleCli(p *pikpak.PikPak) { if parentId == "" { parentId, err = p.GetPathFolderId(path) if err != nil { - logrus.Errorf("Get parent id failed: %s\n", err) + logClient.Errorf("Get parent id failed: %v\n", err) return } } @@ -108,11 +119,13 @@ func handleCli(p *pikpak.PikPak) { break } url := string(lineBytes) - err = p.CreateUrlFile(parentId, url) + taskInfo, err := p.CreateUrlFile(parentId, url) if err != nil { - logrus.Errorln("Create url file failed: ", err) + logClient.Errorf("Create url file failed: %v\n", err) continue } - logrus.Infoln("Create url file success: ", url) + logClient.Infof("Create url file success: ", url) + + logClient.Info(taskInfo) } } diff --git a/cmd/root.go b/cmd/root.go index fca8214..e1cf837 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,16 +34,17 @@ var rootCmd = &cobra.Command{ }, } -// Config path -var configPath string - -// Debug mode -var debug bool +var ( + configPath, output string // Config Path + debug bool // Debug Mode +) // Initialize the command line interface func init() { // debug + rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug mode") + rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "text", "Output Format:[json,text]") // config rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yml", "config file path") rootCmd.AddCommand(upload.UploadCmd) diff --git a/internal/pikpak/url.go b/internal/pikpak/url.go index e7dbeed..b57a592 100644 --- a/internal/pikpak/url.go +++ b/internal/pikpak/url.go @@ -9,7 +9,7 @@ import ( "github.com/sirupsen/logrus" ) -func (p *PikPak) CreateUrlFile(parentId, url string) error { +func (p *PikPak) CreateUrlFile(parentId, url string) (string, error) { m := map[string]interface{}{ "kind": "drive#file", "upload_type": "UPLOAD_TYPE_URL", @@ -22,12 +22,12 @@ func (p *PikPak) CreateUrlFile(parentId, url string) error { } bs, err := jsoniter.Marshal(&m) if err != nil { - return err + return "", err } START: req, err := http.NewRequest("POST", "https://api-drive.mypikpak.com/drive/v1/files", bytes.NewBuffer(bs)) if err != nil { - return err + return "", err } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Product_flavor_name", "cha") @@ -39,18 +39,18 @@ START: req.Header.Set("Country", "CN") bs, err = p.sendRequest(req) if err != nil { - return err + return "", err } error_code := jsoniter.Get(bs, "error_code").ToInt() if error_code != 0 { if error_code == 9 { err := p.AuthCaptchaToken("POST:/drive/v1/files") if err != nil { - return err + return "", err } goto START } - return fmt.Errorf("upload file error: %s", jsoniter.Get(bs, "error").ToString()) + return "", fmt.Errorf("upload file error: %s", jsoniter.Get(bs, "error").ToString()) } task := jsoniter.Get(bs, "task") @@ -61,5 +61,5 @@ START: // } else { // return fmt.Errorf("create file error: %s", phase) // } - return nil + return task.ToString(), nil } diff --git a/internal/utils/log.go b/internal/utils/log.go new file mode 100644 index 0000000..c4e241c --- /dev/null +++ b/internal/utils/log.go @@ -0,0 +1,110 @@ +package utils + +import ( + "os" + + "github.com/sirupsen/logrus" +) + +const ( + WarnLevel = logrus.WarnLevel + InfoLevel = logrus.InfoLevel + DebugLevel = logrus.DebugLevel + ErrorLevel = logrus.ErrorLevel +) + +var ( + timeFormat = "2006-01-02 15:04:05.999999 -0700" +) + +type LogStruct struct { + log *logrus.Logger +} + +type Log interface { + SetLevel(level logrus.Level) + SetLogFormat(format logrus.Formatter) + Info(message string) + Warn(message string) + Debug(message string) + Error(message string) + + Infof(message string, args ...interface{}) + Warnf(message string, args ...interface{}) + Debugf(message string, args ...interface{}) + Errorf(message string, args ...interface{}) +} + +func NewLog(format string) Log { + + var (logFormat logrus.Formatter) + log := logrus.New() + log.SetOutput(os.Stdout) + log.SetLevel(InfoLevel) + logFieldMap := logrus.FieldMap{ + logrus.FieldKeyTime: "timestamp", + logrus.FieldKeyLevel: "level", + logrus.FieldKeyMsg: "message", + logrus.FieldKeyFunc: "caller", + } + + + switch format { + case "json": + logFormat = &logrus.JSONFormatter{ + FieldMap: logFieldMap, + TimestampFormat: timeFormat, + } + case "text": + logFormat = &logrus.TextFormatter{ + FieldMap: logFieldMap, + TimestampFormat: timeFormat, + } + } + + log.SetFormatter(logFormat) + + return &LogStruct{ + log: log, + } +} + +func (l *LogStruct) SetLevel(level logrus.Level) { + l.log.SetLevel(level) +} + +func (l *LogStruct) SetLogFormat(format logrus.Formatter) { + l.log.SetFormatter(format) +} + +func (l *LogStruct) Info(message string) { + l.log.Info(message) +} + +func (l *LogStruct) Infof(message string, args ...interface{}) { + l.log.Infof(message, args...) +} + +func (l *LogStruct) Warn(message string) { + l.log.Warn(message) +} + +func (l *LogStruct) Warnf(message string, args ...interface{}) { + l.log.Warnf(message, args...) +} + +func (l *LogStruct) Debug(message string) { + l.log.Debug(message) +} + +func (l *LogStruct) Debugf(message string, args ...interface{}) { + l.log.Debugf(message, args...) +} + +func (l *LogStruct) Error(message string) { + l.log.Error(message) +} + +func (l *LogStruct) Errorf(message string, args ...interface{}) { + l.log.Errorf(message, args...) +}