Skip to content

Commit

Permalink
add --templates flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hatredholder committed Dec 26, 2024
1 parent 457992b commit 51eee5c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
10 changes: 9 additions & 1 deletion cmd/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/hatredholder/mediabrowse/internal"
"github.com/hatredholder/mediabrowse/internal/api"
"github.com/hatredholder/mediabrowse/internal/utils"
"github.com/spf13/cobra"
)

Expand All @@ -29,9 +31,15 @@ Example:
os.Exit(0)
}

tmplName, err := cmd.Flags().GetString("template")
if err != nil {
log.Fatal(err)
}
tmplPath := utils.GetTmplPath(tmplName)

hits := api.Query(args)
book := internal.Chooser(hits)
result := internal.Format(book, cmd.Flags())
result := internal.Format(book, tmplPath)

fmt.Print(result)
},
Expand Down
15 changes: 12 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"fmt"
"os"

"github.com/hatredholder/mediabrowse/internal/templates"
"github.com/hatredholder/mediabrowse/internal/utils"
"github.com/spf13/cobra"
)

Expand All @@ -13,10 +15,14 @@ var rootCmd = &cobra.Command{
Examples:
$ mediabrowse movie "Memento"
$ mediabrowse book "The Old Man and the Sea"
$ mediabrowse book "The Old Man and the Sea" --template markdown
`,
Version: "v1.0.0",
Run: func(cmd *cobra.Command, args []string) {
if showTemplates, _ := cmd.Flags().GetBool("templates"); showTemplates == true {
fmt.Println("Currently available templates:", utils.FindAvailableTmpls())
os.Exit(0)
}
cmd.Help()
},
}
Expand All @@ -33,12 +39,15 @@ func init() {
rootCmd.PersistentFlags().StringP("template", "t", "default", "template for format output")
rootCmd.PersistentFlags().Lookup("template").DefValue = ""

// disable completion command
rootCmd.Root().CompletionOptions.DisableDefaultCmd = true
// set local flags
rootCmd.Flags().BoolP("templates", "", false, "show available templates")

// set custom usage template
rootCmd.Root().SetUsageTemplate(templates.UsageTmpl)

// disable help command
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

// disable completion command
rootCmd.Root().CompletionOptions.DisableDefaultCmd = true
}
24 changes: 1 addition & 23 deletions internal/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package internal

import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"text/template"

"github.com/hatredholder/mediabrowse/internal/api"
"github.com/hatredholder/mediabrowse/internal/templates"
"github.com/hatredholder/mediabrowse/internal/utils"
"github.com/spf13/pflag"
)

func Process(t *template.Template, vars interface{}) string {
Expand All @@ -36,25 +32,7 @@ func ProcessFile(tmplPath string, funcMap template.FuncMap, vars interface{}) st
return Process(tmpl, vars)
}

func GetTmplFile(tmplName string) string {
tmplFile := filepath.Join(utils.GetConfigDir(), tmplName+".tmpl")
if _, err := os.Stat(tmplFile); err != nil {
fmt.Println("Failed to find template with name:", tmplName)
fmt.Println("Available templates:", utils.FindAvailableTmpls())
os.Exit(0)
}

return tmplFile
}

func Format(book api.Document, flags *pflag.FlagSet) string {
tmplName, err := flags.GetString("template")
if err != nil {
log.Fatal(err)
}

tmplPath := GetTmplFile(tmplName)

func Format(book api.Document, tmplPath string) string {
funcMap := template.FuncMap{
"commify": templates.Commify,
"truncate": templates.Truncate,
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -34,6 +35,16 @@ func FindAvailableTmpls() string {
return strings.Join(result, ", ")
}

func GetTmplPath(tmplName string) string {
tmplPath := filepath.Join(GetConfigDir(), tmplName+".tmpl")
if _, err := os.Stat(tmplPath); err != nil {
fmt.Println("Failed to find template with name:", tmplName)
os.Exit(0)
}

return tmplPath
}

func CreateTmplFiles() error {
if err := os.WriteFile(GetConfigDir()+"/default.tmpl", []byte(templates.DefaultTmpl), 0644); err != nil {
return err
Expand Down

0 comments on commit 51eee5c

Please sign in to comment.