2021-10-30 23:25:44 +00:00
package cmd
import (
"fmt"
2021-11-17 23:32:37 +00:00
"os"
"accorder/pkg/calibre"
2021-10-30 23:25:44 +00:00
2021-11-18 22:46:07 +00:00
"github.com/satori/go.uuid"
2021-10-30 23:25:44 +00:00
"github.com/spf13/cobra"
2021-11-17 00:19:46 +00:00
"github.com/spf13/viper"
2021-10-30 23:25:44 +00:00
)
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
2021-11-21 01:19:45 +00:00
the configuration file for the future use ( therefore : ' accorder build SESSION '
2021-10-30 23:25:44 +00:00
should be enough for the next successful build ) . ` ,
2021-11-21 01:19:45 +00:00
Args : cobra . ExactArgs ( 1 ) ,
2021-10-30 23:25:44 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2021-11-21 01:19:45 +00:00
session := args [ 0 ]
libraryUUID := viper . GetString ( fmt . Sprintf ( "%s.library_uuid" , session ) )
librarySecret := viper . GetString ( fmt . Sprintf ( "%s.library_secret" , session ) )
2021-11-17 23:32:37 +00:00
calibrePathFromCli , _ := cmd . PersistentFlags ( ) . GetString ( "directory" )
librarianNameFromCli , _ := cmd . PersistentFlags ( ) . GetString ( "librarian" )
2021-11-21 01:19:45 +00:00
// didn't exist before
if libraryUUID == "" {
// new session provided path + librarian name
if calibrePathFromCli != "" && librarianNameFromCli != "" {
2021-11-17 23:32:37 +00:00
libraryUUID = uuid . NewV4 ( ) . String ( )
viper . Set ( fmt . Sprintf ( "%s.library_uuid" , args [ 0 ] ) , libraryUUID )
2021-11-21 01:19:45 +00:00
2021-11-17 23:32:37 +00:00
librarySecret = uuid . NewV4 ( ) . String ( )
viper . Set ( fmt . Sprintf ( "%s.library_secret" , args [ 0 ] ) , librarySecret )
2021-11-21 01:19:45 +00:00
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 )
2021-11-17 23:32:37 +00:00
}
2021-11-21 01:19:45 +00:00
} else if calibrePathFromCli != "" || librarianNameFromCli != "" {
2021-11-17 23:32:37 +00:00
viper . WriteConfig ( )
2021-11-17 00:19:46 +00:00
}
2021-11-21 01:19:45 +00:00
calibrePath := viper . GetString ( fmt . Sprintf ( "%s.local_upload" , args [ 0 ] ) )
librarianName := viper . GetString ( fmt . Sprintf ( "%s.librarian_name" , args [ 0 ] ) )
2021-11-18 22:46:07 +00:00
jsonPath , _ := cmd . PersistentFlags ( ) . GetString ( "jsonpath" )
calibre . RenderStandaloneApp ( calibrePath , librarianName , libraryUUID , librarySecret , jsonPath )
2021-10-30 23:25:44 +00:00
} ,
}
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 )
2021-11-21 01:19:45 +00:00
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" ) )
2021-11-17 00:19:46 +00:00
rootCmd . AddCommand ( buildCmd )
2021-10-30 23:25:44 +00:00
}