accorder/cmd/upload.go

87 lines
3.2 KiB
Go
Raw Normal View History

2021-10-30 23:25:44 +00:00
package cmd
import (
"fmt"
2021-11-18 22:46:07 +00:00
"os"
2021-10-30 23:25:44 +00:00
2021-11-18 22:46:07 +00:00
miniocmd "github.com/minio/mc/cmd"
"github.com/satori/go.uuid"
2021-10-30 23:25:44 +00:00
"github.com/spf13/cobra"
2021-11-18 22:46:07 +00:00
"github.com/spf13/viper"
2021-10-30 23:25:44 +00:00
)
// uploadCmd represents the upload command
var uploadCmd = &cobra.Command{
Use: "upload",
Short: "Upload local Calibre library to the MotW server.",
Long: `Upload local Calibre library to the Memory of the World server.
It will take care of the differences so files already at the server
will not be uploaded again.
Every time the directory path and/or librarian is provided it is saved in
configuration file for the future use (therefore: 'accorder upload PROFILE'
should be enough for the next successful upload).`,
Args: OnlyProfileArgument,
Run: func(cmd *cobra.Command, args []string) {
2021-11-18 22:46:07 +00:00
// calibrePath := viper.GetString(fmt.Sprintf("%s.calibre_path", args[0]))
// librarianName := viper.GetString(fmt.Sprintf("%s.librarian_name", args[0]))
profile := args[0]
libraryUUID := viper.GetString(fmt.Sprintf("%s.library_uuid", profile))
librarySecret := viper.GetString(fmt.Sprintf("%s.library_secret", profile))
calibrePathFromCli, _ := cmd.PersistentFlags().GetString("directory")
librarianNameFromCli, _ := cmd.PersistentFlags().GetString("librarian")
uploadBucketFromCli, _ := cmd.PersistentFlags().GetString("bucket")
if calibrePathFromCli != "" || librarianNameFromCli != "" || uploadBucketFromCli != "" || libraryUUID == "" || librarySecret == "" {
if libraryUUID == "" {
libraryUUID = uuid.NewV4().String()
viper.Set(fmt.Sprintf("%s.library_uuid", profile), libraryUUID)
} else if librarySecret == "" {
librarySecret = uuid.NewV4().String()
viper.Set(fmt.Sprintf("%s.library_secret", profile), librarySecret)
}
viper.WriteConfig()
}
deleteResidue, _ := cmd.PersistentFlags().GetBool("delete-residue")
rgs := []string{}
if deleteResidue {
rgs = []string{"",
"-C", ConfigMinioDir(),
"mirror",
// "--fake",
"--overwrite",
"--remove",
viper.GetString(fmt.Sprintf("%s.calibre_path", profile)),
fmt.Sprintf("%s.upload/%s/", profile, viper.GetString(fmt.Sprintf("%s.bucket_upload", profile))),
}
} else {
rgs = []string{"",
"-C", ConfigMinioDir(),
"mirror",
// "--fake",
"--overwrite",
viper.GetString(fmt.Sprintf("%s.calibre_path", profile)),
fmt.Sprintf("%s.upload/%s/", profile, viper.GetString(fmt.Sprintf("%s.bucket_upload", profile))),
}
}
miniocmd.Main(rgs)
2021-10-30 23:25:44 +00:00
},
}
func init() {
rootCmd.AddCommand(uploadCmd)
uploadCmd.PersistentFlags().StringP("directory", "d", "", "A local Calibre directory path.")
2021-11-18 22:46:07 +00:00
uploadCmd.PersistentFlags().StringP("bucket", "b", "", "A minio bucket where to upload.")
2021-10-30 23:25:44 +00:00
uploadCmd.PersistentFlags().StringP("librarian", "l", "", "Librarian's name.")
uploadCmd.PersistentFlags().BoolP("delete-residue", "", false, "Delete any remote files not present locally anymore.")
CustomHelpOutput(uploadCmd)
2021-11-18 22:46:07 +00:00
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"))
viper.BindPFlag(fmt.Sprintf("%s.bucket_upload", profile), buildCmd.PersistentFlags().Lookup("bucket"))
rootCmd.AddCommand(buildCmd)
2021-10-30 23:25:44 +00:00
}