55 "fmt"
66 "io"
77 "io/ioutil"
8- "log"
98 "net/http"
109 "net/url"
1110 "os"
@@ -14,10 +13,9 @@ import (
1413 "runtime"
1514 "strings"
1615
17- "github.com/pkg/errors"
18-
1916 "github.com/mitchellh/go-homedir"
2017 "github.com/natefinch/lumberjack"
18+ "github.com/pkg/errors"
2119 "github.com/vartanbeno/go-reddit/reddit"
2220 "go.uber.org/zap"
2321 kingpin "gopkg.in/alecthomas/kingpin.v2"
@@ -76,7 +74,7 @@ func downloadFile(URL string, fileName string) error {
7674 return nil
7775}
7876
79- func genFilePath (destinationDir string , title string , fullURL string ) (string , error ) {
77+ func genFilePath (destinationDir string , subreddit string , title string , fullURL string ) (string , error ) {
8078 // https://www.golangprograms.com/golang-download-image-from-given-url.html
8179 fileURL , err := url .Parse (fullURL )
8280 if err != nil {
@@ -90,19 +88,20 @@ func genFilePath(destinationDir string, title string, fullURL string) (string, e
9088
9189 for _ , s := range []string {" " , "/" , "\\ " , "\n " , "\r " , "\x00 " } {
9290 title = strings .ReplaceAll (title , s , "_" )
91+ subreddit = strings .ReplaceAll (subreddit , s , "_" )
9392 }
94- fileName = title + "_" + fileName
93+ fileName = subreddit + "_" + title + "_" + fileName
9594 fileName = filepath .Join (destinationDir , fileName )
9695 return fileName , nil
9796}
9897
99- func readConfig (configBytes []byte ) (* lumberjack.Logger , []subreddit ) {
98+ func parseConfig (configBytes []byte ) (* lumberjack.Logger , []subreddit , error ) {
10099
101100 cfg := config {}
102101 err := yaml .UnmarshalStrict (configBytes , & cfg )
103102 if err != nil {
104103 // not ok to get invalid YAML
105- log . Panicf ( "readConfig: yaml decode: %+v \n " , errors .WithStack (err ) )
104+ return nil , [] subreddit {} , errors .WithStack (err )
106105 }
107106
108107 var lumberjackLogger * lumberjack.Logger = nil
@@ -111,7 +110,7 @@ func readConfig(configBytes []byte) (*lumberjack.Logger, []subreddit) {
111110 if cfg .LumberjackLogger != nil {
112111 f , err := homedir .Expand (cfg .LumberjackLogger .Filename )
113112 if err != nil {
114- log . Panicf ( "readConfig: expand lumberjack: %+v \n " , errors .WithStack (err ) )
113+ return nil , [] subreddit {} , errors .WithStack (err )
115114 }
116115 cfg .LumberjackLogger .Filename = f
117116 lumberjackLogger = cfg .LumberjackLogger
@@ -121,13 +120,22 @@ func readConfig(configBytes []byte) (*lumberjack.Logger, []subreddit) {
121120 for _ , sr := range cfg .Subreddits {
122121 fullDest , err := homedir .Expand (sr .Destination )
123122 if err != nil {
124- log . Panicf ( "readConfig: Cannot expand subreddit destination %v: %v: %v" , sr . Name , sr . Destination , err )
123+ return nil , [] subreddit {}, errors . WithStack ( err )
125124 }
126125 sr .Destination = fullDest
126+ info , err := os .Stat (sr .Destination )
127+ if err != nil {
128+ return nil , []subreddit {}, errors .WithStack (err )
129+
130+ }
131+ if ! info .IsDir () {
132+ return nil , []subreddit {}, errors .Errorf ("not a directory: %#v\n " , sr .Destination )
133+ }
134+
127135 subreddits = append (subreddits , sr )
128136 }
129137
130- return lumberjackLogger , subreddits
138+ return lumberjackLogger , subreddits , nil
131139}
132140
133141func isImage (URL string ) error {
@@ -154,26 +162,6 @@ func grab(sugar *zap.SugaredLogger, subreddits []subreddit) error {
154162
155163 for _ , subreddit := range subreddits {
156164
157- info , err := os .Stat (subreddit .Destination )
158- // NOTE: should I move these checks to config parsing time?
159- if err != nil {
160- logAndPrint (sugar , os .Stderr ,
161- "destination error - skipping all posts" ,
162- "subreddit" , subreddit .Name ,
163- "directory" , subreddit .Destination ,
164- "err" , errors .WithStack (err ),
165- )
166- continue
167- }
168- if ! info .IsDir () {
169- logAndPrint (sugar , os .Stderr ,
170- "Is not directory - skipping all posts" ,
171- "subreddit" , subreddit .Name ,
172- "directory" , subreddit .Destination ,
173- )
174- continue
175- }
176-
177165 posts , _ , err := client .Subreddit .TopPosts (
178166 ctx ,
179167 subreddit .Name , & reddit.ListPostOptions {
@@ -205,7 +193,7 @@ func grab(sugar *zap.SugaredLogger, subreddits []subreddit) error {
205193 continue
206194 }
207195
208- filePath , err := genFilePath (subreddit .Destination , post .Title , post .URL )
196+ filePath , err := genFilePath (subreddit .Destination , subreddit . Name , post .Title , post .URL )
209197 if err != nil {
210198 logAndPrint (sugar , os .Stderr ,
211199 "genFilePath err" ,
@@ -268,11 +256,6 @@ subreddits:
268256 _ , err := os .Stat (configPath )
269257 if os .IsNotExist (err ) {
270258 err = ioutil .WriteFile (configPath , emptyConfigContent , 0644 )
271- logAndPrint (
272- sugar , os .Stdout ,
273- "wrote default config" ,
274- "configPath" , configPath ,
275- )
276259 if err != nil {
277260 logAndPrint (
278261 sugar , os .Stderr ,
@@ -281,6 +264,11 @@ subreddits:
281264 )
282265 return err
283266 }
267+ logAndPrint (
268+ sugar , os .Stdout ,
269+ "wrote default config" ,
270+ "configPath" , configPath ,
271+ )
284272 } else if err != nil {
285273 logAndPrint (
286274 sugar , os .Stderr ,
@@ -316,7 +304,7 @@ subreddits:
316304
317305 logAndPrint (
318306 sugar , os .Stdout ,
319- "Opening editor " ,
307+ "Opening config " ,
320308 "editor" , executable ,
321309 "configPath" , configPath ,
322310 )
@@ -362,34 +350,42 @@ func run() error {
362350 panic (err )
363351 }
364352
365- configBytes , cfgErr := ioutil .ReadFile (configPath )
353+ // This is expected to fail on first run
354+ configBytes , cfgLoadErr := ioutil .ReadFile (configPath )
366355
367- // if configBytes == []byte{}, then this will be
368- // defaulted to nothing and the logging won't work
369- // Stuff will still be printed out with logAndPrint though
370- lumberjackLogger , subreddits := readConfig (configBytes )
356+ lumberjackLogger , subreddits , cfgParseErr := parseConfig (configBytes )
371357
372358 logger := newLogger (
373359 lumberjackLogger ,
374360 nil ,
375361 zap .DebugLevel ,
362+ version ,
376363 )
377364
378365 defer logger .Sync ()
379366 sugar := logger .Sugar ()
380367
368+ if cfgParseErr != nil {
369+ logAndPrint (
370+ sugar , os .Stderr ,
371+ "Can't parse config" ,
372+ "err" , cfgParseErr ,
373+ )
374+ return cfgParseErr
375+ }
376+
381377 switch cmd {
382378 case editConfigCmd .FullCommand ():
383379 return editConfig (sugar , configPath , * editConfigCmdEditorFlag )
384380 case grabCmd .FullCommand ():
385- if cfgErr != nil {
381+ if cfgLoadErr != nil {
386382 logAndPrint (
387383 sugar , os .Stderr ,
388384 "Config error - try `edit-config`" ,
389- "cfgErr " , cfgErr ,
390- "cfgErrMsg " , cfgErr .Error (),
385+ "cfgLoadErr " , cfgLoadErr ,
386+ "cfgLoadErrMsg " , cfgLoadErr .Error (),
391387 )
392- return cfgErr
388+ return cfgLoadErr
393389 }
394390 return grab (sugar , subreddits )
395391 case versionCmd .FullCommand ():
0 commit comments