bunch of changes.. and in hurry...

This commit is contained in:
Marcell Mars 2021-11-26 15:21:56 +01:00
parent 18c4fc7c4c
commit c058b078c0
4 changed files with 102 additions and 81 deletions

View File

@ -2,11 +2,9 @@ package cmd
import (
"fmt"
"os"
"accorder/pkg/calibre"
"github.com/satori/go.uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -23,48 +21,58 @@ Every time the directory path and/or librarian is provided it is saved in
the configuration file for the future use (therefore: 'accorder build SESSION'
should be enough for the next successful build).`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
session := args[0]
for vipFlag, cliFlag := range map[string]string{
"librarian_name": "librarian",
"local_upload": "directory",
"library_uuid": "library-uuid",
"library_secret": "library-secret",
"server_upload": "server",
"bucket_upload": "bucket",
} {
viper.BindPFlag(fmt.Sprintf("%s.%s", session, vipFlag), cmd.Flags().Lookup(cliFlag))
}
},
Run: func(cmd *cobra.Command, args []string) {
session := args[0]
libraryUUID := viper.GetString(fmt.Sprintf("%s.library_uuid", session))
librarySecret := viper.GetString(fmt.Sprintf("%s.library_secret", session))
calibrePathFromCli, _ := cmd.PersistentFlags().GetString("directory")
librarianNameFromCli, _ := cmd.PersistentFlags().GetString("librarian")
// didn't exist before
if libraryUUID == "" {
// new session provided path + librarian name
if calibrePathFromCli != "" && librarianNameFromCli != "" {
libraryUUID = uuid.NewV4().String()
viper.Set(fmt.Sprintf("%s.library_uuid", args[0]), libraryUUID)
MissingRequiredFlags(
map[string]string{
"librarian_name": "librarian",
"local_upload": "directory",
},
session,
cmd,
)
librarySecret = uuid.NewV4().String()
viper.Set(fmt.Sprintf("%s.library_secret", args[0]), librarySecret)
viper.WriteConfig()
fmt.Printf("Added new session `%s` to the config file.\nNext time it is enough to run:\naccorder build %s\nand it will be run with the same settings.", session, session)
} else {
fmt.Printf("Adding new SESSION via `upload` command should have both: \n librarian name and Calibre path \nto be run and automatically added to the config file. For example:\naccorder build -d \"/users/jessica/Calibre library/\" -l \"Aaron Elbakyan\" %s", session)
os.Exit(1)
}
} else if calibrePathFromCli != "" || librarianNameFromCli != "" {
viper.WriteConfig()
}
libraryUUID := ViperValue(session, "library_uuid")
librarySecret := ViperValue(session, "library_secret")
calibrePath := ViperValue(session, "local_upload")
librarianName := ViperValue(session, "librarian_name")
jsonPath := CliFlagValue(cmd, "jsonpath")
calibrePath := viper.GetString(fmt.Sprintf("%s.local_upload", args[0]))
librarianName := viper.GetString(fmt.Sprintf("%s.librarian_name", args[0]))
jsonPath, _ := cmd.PersistentFlags().GetString("jsonpath")
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
},
}
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)
session := os.Args[len(os.Args)-1]
viper.BindPFlag(fmt.Sprintf("%s.local_upload", session), buildCmd.PersistentFlags().Lookup("directory"))
viper.BindPFlag(fmt.Sprintf("%s.librarian_name", session), buildCmd.PersistentFlags().Lookup("librarian"))
rootCmd.AddCommand(buildCmd)
buildCmd.Flags().StringP("directory", "d", "", "A local Calibre directory.")
buildCmd.Flags().StringP("librarian", "l", "", "Librarian's name.")
buildCmd.Flags().StringP("jsonpath", "j", "", "Path where to render all metadata into JSON.")
buildCmd.Flags().StringP("import-bibtex", "i", "", "Import books from BibTex file into Calibre.")
buildCmd.Flags().StringP("library-uuid", "u", "", "A library's UUID used if part of MotW.")
buildCmd.Flags().StringP("library-secret", "p", "", "A password used if part of MotW.")
buildCmd.Flags().StringP("server", "s", "minio.memoryoftheworld.org", "Server.")
buildCmd.Flags().StringP("bucket", "b", "", "Server.")
CustomHelpOutput(buildCmd)
buildCmd.Flags().MarkHidden("library-uuid")
buildCmd.Flags().MarkHidden("library-secret")
buildCmd.Flags().MarkHidden("server")
buildCmd.Flags().MarkHidden("bucket")
}

View File

@ -10,6 +10,26 @@ import (
"path/filepath"
)
func MissingRequiredFlags(flags map[string]string, session string, cmd *cobra.Command) {
exit := false
for vipFlag, cliFlag := range flags {
if ViperValue(session, vipFlag) == "" {
fmt.Printf("ERROR: A flag --%s is missing for SESSION `%s` to work.\n", cliFlag, session)
exit = true
}
}
if exit {
fmt.Println("~ ~ ~ ~")
cmd.Usage()
os.Exit(1)
}
if cmd.Flags().NFlag() > 0 {
viper.WriteConfig()
}
}
func CustomHelpOutput(cmd *cobra.Command) {
cmd.Flags().SortFlags = false
cmd.SetHelpTemplate(HelpTemplate)
@ -25,7 +45,7 @@ func ViperSettingsPrint() {
}
func CliFlagValue(cmd *cobra.Command, key string) (value string) {
value, _ = cmd.PersistentFlags().GetString(key)
value, _ = cmd.Flags().GetString(key)
return
}

View File

@ -6,7 +6,6 @@ import (
miniocmd "github.com/minio/mc/cmd"
"github.com/spf13/cobra"
// "github.com/spf13/pflag"
"github.com/spf13/viper"
)
@ -22,42 +21,42 @@ Every time the directory path and/or librarian is provided it is saved in
configuration file for the future use (therefore: 'accorder upload SESSION'
should be enough for the next successful upload).`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
session := args[0]
server := ViperValue(session, "server_upload")
// username in upload context comes from library_uuid
username := ViperValue(session, "library_uuid")
// password in upload context comes from library_secret
password := ViperValue(session, "library_secret")
bucket := ViperValue(session, "bucket_upload")
localDirectory := ViperValue(session, "local_upload")
exit := false
for vipFlag, cliFlag := range map[string]string{
"server_upload": "server",
"library_uuid": "username",
"library_secret": "password",
"bucket_upload": "bucket",
"local_upload": "directory",
"librarian_name": "librarian",
} {
if ViperValue(session, vipFlag) == "" {
fmt.Printf("ERROR: A flag --%s is missing for session `%s` to work.\n", cliFlag, session)
exit = true
}
viper.BindPFlag(fmt.Sprintf("%s.%s", session, vipFlag), cmd.Flags().Lookup(cliFlag))
}
},
Run: func(cmd *cobra.Command, args []string) {
session := args[0]
if exit {
fmt.Println("~ ~ ~ ~")
cmd.Usage()
os.Exit(1)
}
if cmd.Flags().NFlag() > 0 {
viper.WriteConfig()
}
MissingRequiredFlags(
map[string]string{
"server_upload": "server",
"library_uuid": "username",
"library_secret": "password",
"bucket_upload": "bucket",
"local_upload": "directory",
},
session,
cmd,
)
server := ViperValue(session, "server_upload")
// username in upload context comes from library_uuid
username := ViperValue(session, "library_uuid")
// password in upload context comes from library_secret
password := ViperValue(session, "library_secret")
bucket := ViperValue(session, "bucket_upload")
localDirectory := ViperValue(session, "local_upload")
deleteResidue, _ := cmd.PersistentFlags().GetBool("delete-residue")
verbose, _ := cmd.PersistentFlags().GetBool("verbose")
@ -77,6 +76,7 @@ should be enough for the next successful upload).`,
if verbose {
rgs = append(rgs, "--json")
}
rgs = append(rgs, localDirectory, fmt.Sprintf("%s/%s", server, bucket))
miniocmd.Main(rgs)
@ -85,24 +85,17 @@ should be enough for the next successful upload).`,
func init() {
rootCmd.AddCommand(uploadCmd)
uploadCmd.PersistentFlags().StringP("directory", "d", "", "A local directory to be uploaded.")
uploadCmd.PersistentFlags().StringP("username", "u", "", "Username.")
uploadCmd.PersistentFlags().StringP("password", "p", "", "Password.")
uploadCmd.PersistentFlags().StringP("bucket", "b", "", "A remote directory/bucket where to upload.")
uploadCmd.PersistentFlags().StringP("server", "s", "", "Server.")
uploadCmd.PersistentFlags().BoolP("delete-residue", "", false, "Delete any remote files not present locally anymore.")
uploadCmd.PersistentFlags().BoolP("verbose", "v", false, "Verbose log.")
uploadCmd.Flags().StringP("directory", "d", "", "A local directory to be uploaded.")
uploadCmd.Flags().StringP("username", "u", "", "Username.")
uploadCmd.Flags().StringP("password", "p", "", "Password.")
uploadCmd.Flags().StringP("bucket", "b", "", "A remote directory/bucket where to upload.")
uploadCmd.Flags().StringP("server", "s", "minio.memoryoftheworld.org", "Server.")
uploadCmd.Flags().BoolP("delete-residue", "", false, "Delete any remote files not present locally anymore.")
uploadCmd.Flags().BoolP("verbose", "v", false, "Verbose log.")
uploadCmd.Flags().StringP("librarian", "l", "", "Librarian's name.")
CustomHelpOutput(uploadCmd)
session := os.Args[len(os.Args)-1]
for vipFlag, cliFlag := range map[string]string{
"server_upload": "server",
"library_uuid": "username",
"library_secret": "password",
"bucket_upload": "bucket",
"local_upload": "directory",
} {
viper.BindPFlag(fmt.Sprintf("%s.%s", session, vipFlag), uploadCmd.PersistentFlags().Lookup(cliFlag))
}
uploadCmd.Flags().MarkHidden("librarian")
}

View File

@ -67,7 +67,7 @@ type BookJSON struct {
Librarian string `json:"librarian"`
LibraryUUID string `json:"library_uuid"`
Title string `json:"title"`
TitleSort string `json:"title_sort,omitempty"`
TitleSort string `json:"title_sort"`
Authors []string `json:"authors"`
Abstract string `json:"abstract"`
Tags []string `json:"tags"`
@ -123,7 +123,7 @@ type BookOpf struct {
// TitleSort returns if Calibre processed Title for sorting order
func (book BookOpf) TitleSort() string {
for _, meta := range book.Metadata.Meta {
if meta.Name == "calibre:title_sort" && meta.Content != book.Metadata.Title {
if meta.Name == "calibre:title_sort" {
return meta.Content
}
}