Skip to content

Commit

Permalink
Move flags parsing to PersistentPreRun (Fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferrari88 committed Jan 19, 2024
1 parent 4b8b202 commit 914257b
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,45 @@ var (
Use: "sbstck-dl",
Short: "Substack Downloader",
Long: `sbstck-dl is a command line tool for downloading Substack newsletters for archival purposes, offline reading, or data analysis.`,
}
)
PersistentPreRun: func(cmd *cobra.Command, args []string) {

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
var cookie *http.Cookie
var cookie *http.Cookie

if proxyURL != "" {
var err error
parsedProxyURL, err = parseURL(proxyURL)
if err != nil {
log.Fatal(err)
}
}

if ratePerSecond == 0 {
log.Fatal("rate must be greater than 0")
}
if proxyURL != "" {
var err error
parsedProxyURL, err = parseURL(proxyURL)
if err != nil {
log.Fatal(err)
}
}

if idCookieVal != "" && idCookieName != "" {
if idCookieName == substackSid {
cookie = &http.Cookie{
Name: "substack.sid",
Value: idCookieVal,
if ratePerSecond == 0 {
log.Fatal("rate must be greater than 0")
}
} else if idCookieName == connectSid {
cookie = &http.Cookie{
Name: "connect.sid",
Value: idCookieVal,

if idCookieVal != "" && idCookieName != "" {
if idCookieName == substackSid {
cookie = &http.Cookie{
Name: "substack.sid",
Value: idCookieVal,
}
} else if idCookieName == connectSid {
cookie = &http.Cookie{
Name: "connect.sid",
Value: idCookieVal,
}
}
}
}
}

fetcher = lib.NewFetcher(lib.WithRatePerSecond(ratePerSecond), lib.WithProxyURL(parsedProxyURL), lib.WithCookie(cookie))
extractor = lib.NewExtractor(fetcher)
fetcher = lib.NewFetcher(lib.WithRatePerSecond(ratePerSecond), lib.WithProxyURL(parsedProxyURL), lib.WithCookie(cookie))
extractor = lib.NewExtractor(fetcher)
},
}
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down

0 comments on commit 914257b

Please sign in to comment.