60 lines
2.4 KiB
Go
60 lines
2.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"accorder/pkg/calibre"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/satori/go.uuid"
|
|
)
|
|
|
|
var buildCmd = &cobra.Command{
|
|
Use: "build",
|
|
Short: "Build standalone, portable webapp from Calibre library.",
|
|
Long: `Build searchable, standalone, portable webapp against the local Calibre
|
|
library including all the metadata needed. It creates BROWSE_LIBRARY.html in
|
|
root directory of the local Calibre library. For search (authors, titles,
|
|
tags...) it uses rendered metadata from static/data{1-8}.js files.
|
|
|
|
Every time the directory path and/or librarian is provided it is saved in
|
|
the configuration file for the future use (therefore: 'accorder build PROFILE'
|
|
should be enough for the next successful build).`,
|
|
Args: OnlyProfileArgument,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
calibrePath := viper.GetString(fmt.Sprintf("%s.calibre_path", args[0]))
|
|
librarianName := viper.GetString(fmt.Sprintf("%s.librarian_name", args[0]))
|
|
libraryUUID := viper.GetString(fmt.Sprintf("%s.library_uuid", args[0]))
|
|
librarySecret := viper.GetString(fmt.Sprintf("%s.library_secret", args[0]))
|
|
|
|
calibrePathFromCli, _ := cmd.PersistentFlags().GetString("directory")
|
|
librarianNameFromCli, _ := cmd.PersistentFlags().GetString("librarian")
|
|
if calibrePathFromCli != "" || librarianNameFromCli != "" || libraryUUID == "" || librarySecret == "" {
|
|
if libraryUUID == "" {
|
|
libraryUUID = uuid.NewV4().String()
|
|
viper.Set(fmt.Sprintf("%s.library_uuid", args[0]), libraryUUID)
|
|
} else if librarySecret == "" {
|
|
librarySecret = uuid.NewV4().String()
|
|
viper.Set(fmt.Sprintf("%s.library_secret", args[0]), librarySecret)
|
|
}
|
|
viper.WriteConfig()
|
|
}
|
|
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
buildCmd.PersistentFlags().StringP("directory", "d", "", "A local Calibre directory path.")
|
|
buildCmd.PersistentFlags().StringP("librarian", "l", "", "Librarian's name.")
|
|
buildCmd.PersistentFlags().StringP("jsonpath", "j", "", "Path where to render all metadata into JSON.")
|
|
buildCmd.PersistentFlags().StringP("bibtex", "b", "", "Import books from BibTex file into Calibre.")
|
|
CustomHelpOutput(buildCmd)
|
|
profile := os.Args[len(os.Args)-1]
|
|
viper.BindPFlag(fmt.Sprintf("%s.calibre_path", profile), buildCmd.PersistentFlags().Lookup("directory"))
|
|
viper.BindPFlag(fmt.Sprintf("%s.librarian_name", profile), buildCmd.PersistentFlags().Lookup("librarian"))
|
|
rootCmd.AddCommand(buildCmd)
|
|
}
|