Skip to content

Commit

Permalink
chore: move flags to cmd directory, remove useless shorthands.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 15, 2022
1 parent 022be91 commit 4cdb197
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 223 deletions.
2 changes: 1 addition & 1 deletion internal/argument/arguments.go → cmd/flags/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package argument
package flags

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package argument
package flags

import "testing"

Expand Down
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/cmd/flags"
"github.com/bookstairs/bookhunter/internal/log"
)

Expand Down Expand Up @@ -38,8 +38,8 @@ func init() {
persistentFlags := rootCmd.PersistentFlags()

// Common flags.
persistentFlags.StringVarP(&argument.ConfigRoot, "config", "c", argument.ConfigRoot, "The config path for bookhunter.")
persistentFlags.StringVarP(&argument.Proxy, "proxy", "", argument.Proxy, "The request proxy.")
persistentFlags.StringVarP(&argument.UserAgent, "user-agent", "a", argument.UserAgent, "The request user-agent.")
persistentFlags.BoolVarP(&log.EnableDebug, "verbose", "", false, "Print all the logs for debugging.")
persistentFlags.StringVarP(&flags.ConfigRoot, "config", "c", flags.ConfigRoot, "The config path for bookhunter.")
persistentFlags.StringVar(&flags.Proxy, "proxy", flags.Proxy, "The request proxy.")
persistentFlags.StringVarP(&flags.UserAgent, "user-agent", "a", flags.UserAgent, "The request user-agent.")
persistentFlags.BoolVar(&log.EnableDebug, "verbose", false, "Print all the logs for debugging.")
}
66 changes: 33 additions & 33 deletions cmd/sanqiu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/cmd/flags"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)
Expand All @@ -19,37 +19,37 @@ var sanqiuCmd = &cobra.Command{
Short: "A tool for downloading books from sanqiu.mobi",
Run: func(cmd *cobra.Command, args []string) {
// Set the default start index.
if argument.InitialBookID < lowestBookID {
argument.InitialBookID = lowestBookID
if flags.InitialBookID < lowestBookID {
flags.InitialBookID = lowestBookID
}

// Print download configuration.
log.NewPrinter().
Title("Sanqiu Download Information").
Head(log.DefaultHead...).
Row("Config Path", argument.ConfigRoot).
Row("Proxy", argument.Proxy).
Row("UserAgent", argument.UserAgent).
Row("Formats", argument.Formats).
Row("Extract Archive", argument.Extract).
Row("Download Path", argument.DownloadPath).
Row("Initial ID", argument.InitialBookID).
Row("Rename File", argument.Rename).
Row("Thread", argument.Thread).
Row("Request Per Minute", argument.RateLimit).
Row("Aliyun RefreshToken", argument.HideSensitive(argument.RefreshToken)).
Row("Telecom Username", argument.HideSensitive(argument.TelecomUsername)).
Row("Telecom Password", argument.HideSensitive(argument.TelecomPassword)).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Formats", flags.Formats).
Row("Extract Archive", flags.Extract).
Row("Download Path", flags.DownloadPath).
Row("Initial ID", flags.InitialBookID).
Row("Rename File", flags.Rename).
Row("Thread", flags.Thread).
Row("Request Per Minute", flags.RateLimit).
Row("Aliyun RefreshToken", flags.HideSensitive(flags.RefreshToken)).
Row("Telecom Username", flags.HideSensitive(flags.TelecomUsername)).
Row("Telecom Password", flags.HideSensitive(flags.TelecomPassword)).
Print()

// Set the domain for using in client.Client.
argument.Website = sanqiuWebsite
// Set the domain for using in the client.Client.
flags.Website = sanqiuWebsite

// Create the fetcher.
f, err := argument.NewFetcher(fetcher.SanQiu, map[string]string{
"refreshToken": argument.RefreshToken,
"telecomUsername": argument.TelecomUsername,
"telecomPassword": argument.TelecomPassword,
f, err := flags.NewFetcher(fetcher.SanQiu, map[string]string{
"refreshToken": flags.RefreshToken,
"telecomUsername": flags.TelecomUsername,
"telecomPassword": flags.TelecomPassword,
})
log.Fatal(err)

Expand All @@ -63,24 +63,24 @@ var sanqiuCmd = &cobra.Command{
}

func init() {
flags := sanqiuCmd.Flags()
f := sanqiuCmd.Flags()

// Common download flags.
flags.StringSliceVarP(&argument.Formats, "format", "f", argument.Formats, "The file formats you want to download.")
flags.BoolVarP(&argument.Extract, "extract", "e", argument.Extract, "Extract the archive file for filtering.")
flags.StringVarP(&argument.DownloadPath, "download", "d", argument.DownloadPath,
f.StringSliceVarP(&flags.Formats, "format", "f", flags.Formats, "The file formats you want to download.")
f.BoolVarP(&flags.Extract, "extract", "e", flags.Extract, "Extract the archive file for filtering.")
f.StringVarP(&flags.DownloadPath, "download", "d", flags.DownloadPath,
"The book directory you want to use, default would be current working directory.")
flags.Int64VarP(&argument.InitialBookID, "initial", "i", argument.InitialBookID,
f.Int64VarP(&flags.InitialBookID, "initial", "i", flags.InitialBookID,
"The book id you want to start download. It should exceed 0.")
flags.BoolVarP(&argument.Rename, "rename", "r", argument.Rename, "Rename the book file by book ID.")
flags.IntVarP(&argument.Thread, "thread", "t", argument.Thread, "The number of concurrent download thead.")
flags.IntVarP(&argument.RateLimit, "ratelimit", "", argument.RateLimit, "The request per minutes.")
f.BoolVarP(&flags.Rename, "rename", "r", flags.Rename, "Rename the book file by book ID.")
f.IntVarP(&flags.Thread, "thread", "t", flags.Thread, "The number of concurrent download thead.")
f.IntVar(&flags.RateLimit, "ratelimit", flags.RateLimit, "The request per minutes.")

// Drive ISP flags.
flags.StringVarP(&argument.RefreshToken, "refreshToken", "", argument.RefreshToken,
f.StringVar(&flags.RefreshToken, "refreshToken", flags.RefreshToken,
"We would try to download from the aliyun drive if you provide this token.")
flags.StringVarP(&argument.TelecomUsername, "telecomUsername", "", argument.TelecomUsername,
f.StringVar(&flags.TelecomUsername, "telecomUsername", flags.TelecomUsername,
"Used to download file from telecom drive")
flags.StringVarP(&argument.TelecomPassword, "telecomPassword", "", argument.TelecomPassword,
f.StringVar(&flags.TelecomPassword, "telecomPassword", flags.TelecomPassword,
"Used to download file from telecom drive")
}
139 changes: 136 additions & 3 deletions cmd/talebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/cmd/talebook"
"github.com/bookstairs/bookhunter/cmd/flags"
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
"github.com/bookstairs/bookhunter/internal/talebook"
)

// talebookCmd used to download books from talebook
Expand All @@ -15,7 +19,136 @@ The url for talebook should be provided, the formats is also
optional.`,
}

// talebookDownloadCmd represents the download command
var talebookDownloadCmd = &cobra.Command{
Use: "download",
Short: "Download the book from talebook.",
Run: func(cmd *cobra.Command, args []string) {
// Print download configuration.
log.NewPrinter().
Title("Talebook Download Information").
Head(log.DefaultHead...).
Row("Website", flags.Website).
Row("Username", flags.HideSensitive(flags.Username)).
Row("Password", flags.HideSensitive(flags.Password)).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Formats", flags.Formats).
Row("Download Path", flags.DownloadPath).
Row("Initial ID", flags.InitialBookID).
Row("Rename File", flags.Rename).
Row("Thread", flags.Thread).
Row("Request Per Minute", flags.RateLimit).
Print()

// Create the fetcher.
f, err := flags.NewFetcher(fetcher.Talebook, map[string]string{
"username": flags.Username,
"password": flags.Password,
})
log.Fatal(err)

// Start download the books.
err = f.Download()
log.Fatal(err)

// Finished all the tasks.
log.Info("Successfully download all the books.")
},
}

// talebookRegisterCmd represents the register command.
var talebookRegisterCmd = &cobra.Command{
Use: "register",
Short: "Register account on talebook.",
Long: `Some talebook website need a user account for downloading books.
You can use this register command for creating account.`,
Run: func(cmd *cobra.Command, args []string) {
// Print register configuration.
log.NewPrinter().
Title("Talebook Register Information").
Head(log.DefaultHead...).
Row("Website", flags.Website).
Row("Username", flags.Username).
Row("Password", flags.Password).
Row("Email", flags.Email).
Row("Config Path", flags.ConfigRoot).
Row("UserAgent", flags.UserAgent).
Row("Proxy", flags.Proxy).
Print()

// Create client config.
config, err := client.NewConfig(flags.Website, flags.UserAgent, flags.Proxy, flags.ConfigRoot)
log.Fatal(err)

// Create http client.
c, err := client.New(config)
log.Fatal(err)

// Execute the register request.
resp, err := c.R().
SetFormData(map[string]string{
"username": flags.Username,
"password": flags.Password,
"nickname": flags.Username,
"email": flags.Email,
}).
SetResult(&talebook.CommonResp{}).
ForceContentType("application/json").
Post("/api/user/sign_up")
log.Fatal(err)

result := resp.Result().(*talebook.CommonResp)
if result.Err == talebook.SuccessStatus {
log.Info("Register success.")
} else {
log.Fatalf("Register failed, reason: %s", result.Err)
}
},
}

func init() {
talebookCmd.AddCommand(talebook.DownloadCmd)
talebookCmd.AddCommand(talebook.RegisterCmd)
/// Add the download command.

// Add flags for use info.
f := talebookDownloadCmd.Flags()

// Talebook related flags.
f.StringVarP(&flags.Username, "username", "u", flags.Username, "The account login name.")
f.StringVarP(&flags.Password, "password", "p", flags.Password, "The account password.")
f.StringVarP(&flags.Website, "website", "w", flags.Website, "The talebook website.")

// Common download flags.
f.StringSliceVarP(&flags.Formats, "format", "f", flags.Formats, "The file formats you want to download.")
f.StringVarP(&flags.DownloadPath, "download", "d", flags.DownloadPath,
"The book directory you want to use, default would be current working directory.")
f.Int64VarP(&flags.InitialBookID, "initial", "i", flags.InitialBookID,
"The book id you want to start download. It should exceed 0.")
f.BoolVarP(&flags.Rename, "rename", "r", flags.Rename, "Rename the book file by book ID.")
f.IntVarP(&flags.Thread, "thread", "t", flags.Thread, "The number of concurrent download thead.")
f.IntVar(&flags.RateLimit, "ratelimit", flags.RateLimit, "The request per minutes.")

// Mark some flags as required.
_ = talebookDownloadCmd.MarkFlagRequired("website")

talebookCmd.AddCommand(talebookDownloadCmd)

/// Add the register command.

f = talebookRegisterCmd.Flags()

// Add flags for registering.
f.StringVarP(&flags.Username, "username", "u", flags.Username, "The account login name.")
f.StringVarP(&flags.Password, "password", "p", flags.Password, "The account password.")
f.StringVarP(&flags.Email, "email", "e", flags.Email, "The account email.")
f.StringVarP(&flags.Website, "website", "w", flags.Website, "The talebook website.")

// Mark some flags as required.
_ = talebookRegisterCmd.MarkFlagRequired("website")
_ = talebookRegisterCmd.MarkFlagRequired("username")
_ = talebookRegisterCmd.MarkFlagRequired("password")
_ = talebookRegisterCmd.MarkFlagRequired("email")

talebookCmd.AddCommand(talebookRegisterCmd)
}
71 changes: 0 additions & 71 deletions cmd/talebook/download.go

This file was deleted.

Loading

0 comments on commit 4cdb197

Please sign in to comment.