Browse Source

[cli] Add command to get-token

Neeraj Gupta 1 year ago
parent
commit
063e980280
3 changed files with 69 additions and 5 deletions
  1. 40 3
      cli/cmd/account.go
  2. 23 1
      cli/pkg/account.go
  3. 6 1
      cli/pkg/model/account.go

+ 40 - 3
cli/cmd/account.go

@@ -62,7 +62,7 @@ var updateAccCmd = &cobra.Command{
 			fmt.Printf("invalid app. Accepted values are 'photos', 'locker', 'auth'")
 
 		}
-		err := ctrl.UpdateAccount(context.Background(), model.UpdateAccountParams{
+		err := ctrl.UpdateAccount(context.Background(), model.AccountCommandParams{
 			Email:     email,
 			App:       api.StringToApp(app),
 			ExportDir: &exportDir,
@@ -73,12 +73,49 @@ var updateAccCmd = &cobra.Command{
 	},
 }
 
+// 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)
+		}
+	},
+}
+
 func init() {
 	// Add 'config' subcommands to the root command
 	rootCmd.AddCommand(accountCmd)
 	// Add 'config' subcommands to the 'config' command
 	updateAccCmd.Flags().String("dir", "", "update export directory")
-	updateAccCmd.Flags().String("email", "", "email address of the account to update")
+	updateAccCmd.Flags().String("email", "", "email address of the account")
 	updateAccCmd.Flags().String("app", "photos", "Specify the app, default is 'photos'")
-	accountCmd.AddCommand(listAccCmd, addAccCmd, updateAccCmd)
+	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)
 }

+ 23 - 1
cli/pkg/account.go

@@ -142,7 +142,7 @@ func (c *ClICtrl) ListAccounts(cxt context.Context) error {
 	return nil
 }
 
-func (c *ClICtrl) UpdateAccount(ctx context.Context, params model.UpdateAccountParams) error {
+func (c *ClICtrl) UpdateAccount(ctx context.Context, params model.AccountCommandParams) error {
 	accounts, err := c.GetAccounts(ctx)
 	if err != nil {
 		return err
@@ -177,5 +177,27 @@ func (c *ClICtrl) UpdateAccount(ctx context.Context, params model.UpdateAccountP
 		return b.Put([]byte(accountKey), accInfoBytes)
 	})
 	return err
+}
 
+func (c *ClICtrl) GetToken(ctx context.Context, params model.AccountCommandParams) error {
+	accounts, err := c.GetAccounts(ctx)
+	if err != nil {
+		return err
+	}
+	var acc *model.Account
+	for _, a := range accounts {
+		if a.Email == params.Email && a.App == params.App {
+			acc = &a
+			break
+		}
+	}
+	if acc == nil {
+		return fmt.Errorf("account not found, use `account list` to list accounts")
+	}
+	secretInfo, err := c.KeyHolder.LoadSecrets(*acc)
+	if err != nil {
+		return err
+	}
+	fmt.Println(secretInfo.TokenStr())
+	return nil
 }

+ 6 - 1
cli/pkg/model/account.go

@@ -1,6 +1,7 @@
 package model
 
 import (
+	"encoding/base64"
 	"fmt"
 	"github.com/ente-io/cli/internal/api"
 )
@@ -17,7 +18,7 @@ type Account struct {
 	ExportDir string    `json:"exportDir"`
 }
 
-type UpdateAccountParams struct {
+type AccountCommandParams struct {
 	Email     string
 	App       api.App
 	ExportDir *string
@@ -37,3 +38,7 @@ type AccSecretInfo struct {
 	Token     []byte
 	PublicKey []byte
 }
+
+func (a *AccSecretInfo) TokenStr() string {
+	return base64.URLEncoding.EncodeToString(a.Token)
+}