account.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ente-io/cli/internal/api"
  6. "github.com/ente-io/cli/pkg/model"
  7. "github.com/spf13/cobra"
  8. )
  9. // Define the 'account' command and its subcommands
  10. var accountCmd = &cobra.Command{
  11. Use: "account",
  12. Short: "Manage account settings",
  13. }
  14. // Subcommand for 'account list'
  15. var listAccCmd = &cobra.Command{
  16. Use: "list",
  17. Short: "list configured accounts",
  18. RunE: func(cmd *cobra.Command, args []string) error {
  19. recoverWithLog()
  20. return ctrl.ListAccounts(context.Background())
  21. },
  22. }
  23. // Subcommand for 'account add'
  24. var addAccCmd = &cobra.Command{
  25. Use: "add",
  26. Short: "Add a new account",
  27. Run: func(cmd *cobra.Command, args []string) {
  28. recoverWithLog()
  29. ctrl.AddAccount(context.Background())
  30. },
  31. }
  32. // Subcommand for 'account update'
  33. var updateAccCmd = &cobra.Command{
  34. Use: "update",
  35. Short: "Update an existing account's export directory",
  36. Run: func(cmd *cobra.Command, args []string) {
  37. recoverWithLog()
  38. exportDir, _ := cmd.Flags().GetString("dir")
  39. app, _ := cmd.Flags().GetString("app")
  40. email, _ := cmd.Flags().GetString("email")
  41. if email == "" {
  42. fmt.Println("email must be specified")
  43. return
  44. }
  45. if exportDir == "" {
  46. fmt.Println("dir param must be specified")
  47. return
  48. }
  49. validApps := map[string]bool{
  50. "photos": true,
  51. "locker": true,
  52. "auth": true,
  53. }
  54. if !validApps[app] {
  55. fmt.Printf("invalid app. Accepted values are 'photos', 'locker', 'auth'")
  56. }
  57. err := ctrl.UpdateAccount(context.Background(), model.UpdateAccountParams{
  58. Email: email,
  59. App: api.StringToApp(app),
  60. ExportDir: &exportDir,
  61. })
  62. if err != nil {
  63. fmt.Printf("Error updating account: %v\n", err)
  64. }
  65. },
  66. }
  67. func init() {
  68. // Add 'config' subcommands to the root command
  69. rootCmd.AddCommand(accountCmd)
  70. // Add 'config' subcommands to the 'config' command
  71. updateAccCmd.Flags().String("dir", "", "update export directory")
  72. updateAccCmd.Flags().String("email", "", "email address of the account to update")
  73. updateAccCmd.Flags().String("app", "photos", "Specify the app, default is 'photos'")
  74. accountCmd.AddCommand(listAccCmd, addAccCmd, updateAccCmd)
  75. }