ente/cli/cmd/account.go

122 lines
3.1 KiB
Go
Raw Normal View History

2023-09-14 04:20:32 +00:00
package cmd
import (
"context"
"fmt"
2023-10-21 09:26:13 +00:00
"github.com/ente-io/cli/internal/api"
"github.com/ente-io/cli/pkg/model"
2023-09-14 04:20:32 +00:00
"github.com/spf13/cobra"
)
// Define the 'account' command and its subcommands
var accountCmd = &cobra.Command{
Use: "account",
Short: "Manage account settings",
}
// Subcommand for 'account list'
var listAccCmd = &cobra.Command{
Use: "list",
Short: "list configured accounts",
RunE: func(cmd *cobra.Command, args []string) error {
2023-09-21 09:53:37 +00:00
recoverWithLog()
2023-09-14 04:20:32 +00:00
return ctrl.ListAccounts(context.Background())
},
}
// Subcommand for 'account add'
var addAccCmd = &cobra.Command{
Use: "add",
Short: "Add a new account",
Run: func(cmd *cobra.Command, args []string) {
2023-09-21 09:53:37 +00:00
recoverWithLog()
2023-09-14 04:20:32 +00:00
ctrl.AddAccount(context.Background())
},
}
// Subcommand for 'account update'
var updateAccCmd = &cobra.Command{
Use: "update",
Short: "Update an existing account's export directory",
Run: func(cmd *cobra.Command, args []string) {
recoverWithLog()
exportDir, _ := cmd.Flags().GetString("dir")
app, _ := cmd.Flags().GetString("app")
email, _ := cmd.Flags().GetString("email")
if email == "" {
2023-10-18 07:11:20 +00:00
fmt.Println("email must be specified")
return
}
if exportDir == "" {
fmt.Println("dir param must be specified")
return
}
validApps := map[string]bool{
"photos": true,
"locker": true,
"auth": true,
}
if !validApps[app] {
fmt.Printf("invalid app. Accepted values are 'photos', 'locker', 'auth'")
}
2024-03-08 12:05:16 +00:00
err := ctrl.UpdateAccount(context.Background(), model.AccountCommandParams{
Email: email,
App: api.StringToApp(app),
ExportDir: &exportDir,
})
if err != nil {
fmt.Printf("Error updating account: %v\n", err)
}
},
}
2024-03-08 12:05:16 +00:00
// Subcommand for 'account update'
var getTokenCmd = &cobra.Command{
Use: "get-token",
Short: "Get token for an account for a specific app",
Run: func(cmd *cobra.Command, args []string) {
recoverWithLog()
app, _ := cmd.Flags().GetString("app")
email, _ := cmd.Flags().GetString("email")
if email == "" {
fmt.Println("email must be specified, use --help for more information")
// print help
return
}
validApps := map[string]bool{
"photos": true,
"locker": true,
"auth": true,
}
if !validApps[app] {
fmt.Printf("invalid app. Accepted values are 'photos', 'locker', 'auth'")
}
err := ctrl.GetToken(context.Background(), model.AccountCommandParams{
Email: email,
App: api.StringToApp(app),
})
if err != nil {
fmt.Printf("Error updating account: %v\n", err)
}
},
}
2023-09-14 04:20:32 +00:00
func init() {
// Add 'config' subcommands to the root command
rootCmd.AddCommand(accountCmd)
// Add 'config' subcommands to the 'config' command
2023-10-18 07:11:20 +00:00
updateAccCmd.Flags().String("dir", "", "update export directory")
2024-03-08 12:05:16 +00:00
updateAccCmd.Flags().String("email", "", "email address of the account")
updateAccCmd.Flags().String("app", "photos", "Specify the app, default is 'photos'")
2024-03-08 12:05:16 +00:00
getTokenCmd.Flags().String("email", "", "email address of the account")
getTokenCmd.Flags().String("app", "photos", "Specify the app, default is 'photos'")
accountCmd.AddCommand(listAccCmd, addAccCmd, updateAccCmd, getTokenCmd)
2023-09-14 04:20:32 +00:00
}