Author: Honza Pokorny <honza@redhat.com>
Add config flags
Makefile | 3 +++ cmd/build.go | 11 +++++++---- cmd/cache.go | 8 +++++++- cmd/root.go | 13 ++++++++++--- pkg/bookends/bookends.go | 24 ++++++++++++++----------
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..f8597ac3d11ebdca2416932167d3db2287da4d8e --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +all: + go run main.go build + go run main.go cache diff --git a/cmd/build.go b/cmd/build.go index 1551cc9953d3400e8fac0f18190707f6b9d72ff5..1da218eecb9964a4ecdce42b3ae3ea778e1d2e1e 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -23,14 +23,17 @@ "git.pokorny.ca/bookends/pkg/bookends" "github.com/spf13/cobra" ) -var cfgFile string -var debug bool - var buildCmd = &cobra.Command{ Use: "build", Short: "", Run: func(cmd *cobra.Command, args []string) { - err := bookends.Build() + config := bookends.Config{ + BookLogFilename: bookLogFilename, + CacheDir: cacheDir, + CoversDir: coversDir, + OutputFilename: outputFilename, + } + err := bookends.Build(config) if err != nil { fmt.Println(err) } diff --git a/cmd/cache.go b/cmd/cache.go index 437fed7bbfd135f256dd10ebf7112ee2477e1bc5..289725bd7d8a7ea9f08538e72a661c66cd0185e4 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -27,7 +27,13 @@ var cacheCovers = &cobra.Command{ Use: "cache", Short: "", Run: func(cmd *cobra.Command, args []string) { - err := bookends.CacheCovers() + config := bookends.Config{ + BookLogFilename: bookLogFilename, + CacheDir: cacheDir, + CoversDir: coversDir, + OutputFilename: outputFilename, + } + err := bookends.CacheCovers(config) if err != nil { fmt.Println("ERROR", err) } diff --git a/cmd/root.go b/cmd/root.go index 0c75a15deaea89e83a2d0b36fbb1b68a7aabad70..91a16ece31fe71f022ea15cfcdf4a4787f7fa80e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,6 +23,11 @@ "github.com/spf13/cobra" ) +var bookLogFilename string +var cacheDir string +var coversDir string +var outputFilename string + var rootCmd = &cobra.Command{ Use: "bookends", Short: "", @@ -40,9 +45,11 @@ } } func init() { - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path (default is config.yaml)") - // rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "") - // rootCmd.AddCommand(generateDefaultConfigurationCmd) + rootCmd.PersistentFlags().StringVar(&bookLogFilename, "book-log-filename", "book-log.org", "") + rootCmd.PersistentFlags().StringVar(&cacheDir, "cache-dir", "cache", "") + rootCmd.PersistentFlags().StringVar(&coversDir, "covers-dir", "covers", "") + rootCmd.PersistentFlags().StringVar(&outputFilename, "output-filename", "output.html", "") + rootCmd.AddCommand(buildCmd) rootCmd.AddCommand(yaml2OrgCmd) rootCmd.AddCommand(cacheCovers) diff --git a/pkg/bookends/bookends.go b/pkg/bookends/bookends.go index 51c798a904c905f103a132707bcdb5a8b38cdc01..7fef50b2a9ec713b46fe8ca240867655f6a463ec 100644 --- a/pkg/bookends/bookends.go +++ b/pkg/bookends/bookends.go @@ -20,6 +20,13 @@ "github.com/niklasfasching/go-org/org" "gopkg.in/yaml.v3" ) +type Config struct { + BookLogFilename string + CacheDir string + CoversDir string + OutputFilename string +} + type DateRead struct { time.Time } @@ -128,8 +135,8 @@ return nil } -func Build() error { - f, err := os.Open("book-log.org") +func Build(config Config) error { + f, err := os.Open(config.BookLogFilename) if err != nil { return err @@ -151,7 +158,7 @@ } // fmt.Println(htmlOutput) - err = WriteFile("output.html", htmlOutput) + err = WriteFile(config.OutputFilename, htmlOutput) if err != nil { return err @@ -339,8 +346,8 @@ _, err = io.Copy(destination, source) return err } -func CacheCovers() error { - f, err := os.Open("book-log.org") +func CacheCovers(config Config) error { + f, err := os.Open(config.BookLogFilename) if err != nil { return err @@ -352,21 +359,18 @@ if err != nil { return err } - cacheDir := "cache" - coversDir := "covers" - fmt.Println(len(books)) for _, book := range books { fmt.Println("Processing", book.Title) - cachePath := path.Join(cacheDir, fmt.Sprintf("%s.jpg", book.ISBN)) + cachePath := path.Join(config.CacheDir, fmt.Sprintf("%s.jpg", book.ISBN)) if PathExists(cachePath) { fmt.Println(" Cache exists", cachePath) continue } - coversPath := path.Join(coversDir, fmt.Sprintf("%s.jpg", book.ISBN)) + coversPath := path.Join(config.CoversDir, fmt.Sprintf("%s.jpg", book.ISBN)) if PathExists(coversPath) { fmt.Println(" Cover exists", coversPath)