Skip to content

Commit 914257b

Browse files
committed
Move flags parsing to PersistentPreRun (Fixes #4)
1 parent 4b8b202 commit 914257b

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

cmd/root.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,45 @@ var (
5656
Use: "sbstck-dl",
5757
Short: "Substack Downloader",
5858
Long: `sbstck-dl is a command line tool for downloading Substack newsletters for archival purposes, offline reading, or data analysis.`,
59-
}
60-
)
59+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
6160

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

67-
if proxyURL != "" {
68-
var err error
69-
parsedProxyURL, err = parseURL(proxyURL)
70-
if err != nil {
71-
log.Fatal(err)
72-
}
73-
}
74-
75-
if ratePerSecond == 0 {
76-
log.Fatal("rate must be greater than 0")
77-
}
63+
if proxyURL != "" {
64+
var err error
65+
parsedProxyURL, err = parseURL(proxyURL)
66+
if err != nil {
67+
log.Fatal(err)
68+
}
69+
}
7870

79-
if idCookieVal != "" && idCookieName != "" {
80-
if idCookieName == substackSid {
81-
cookie = &http.Cookie{
82-
Name: "substack.sid",
83-
Value: idCookieVal,
71+
if ratePerSecond == 0 {
72+
log.Fatal("rate must be greater than 0")
8473
}
85-
} else if idCookieName == connectSid {
86-
cookie = &http.Cookie{
87-
Name: "connect.sid",
88-
Value: idCookieVal,
74+
75+
if idCookieVal != "" && idCookieName != "" {
76+
if idCookieName == substackSid {
77+
cookie = &http.Cookie{
78+
Name: "substack.sid",
79+
Value: idCookieVal,
80+
}
81+
} else if idCookieName == connectSid {
82+
cookie = &http.Cookie{
83+
Name: "connect.sid",
84+
Value: idCookieVal,
85+
}
86+
}
8987
}
90-
}
91-
}
9288

93-
fetcher = lib.NewFetcher(lib.WithRatePerSecond(ratePerSecond), lib.WithProxyURL(parsedProxyURL), lib.WithCookie(cookie))
94-
extractor = lib.NewExtractor(fetcher)
89+
fetcher = lib.NewFetcher(lib.WithRatePerSecond(ratePerSecond), lib.WithProxyURL(parsedProxyURL), lib.WithCookie(cookie))
90+
extractor = lib.NewExtractor(fetcher)
91+
},
92+
}
93+
)
9594

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

0 commit comments

Comments
 (0)