File: account.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (78 lines) | stat: -rw-r--r-- 2,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cli

import (
	"context"
	"fmt"
	"log"
	"os"
	"strings"
	"time"

	"github.com/la5nta/pat/app"
	"github.com/la5nta/pat/internal/cmsapi"
)

const (
	AccountUsage = `property [value]

properties:
  password.recovery.email
`
	AccountExample = `
  account password.recovery.email                 Get your current password recovery email for winlink.org.
  account password.recovery.email me@example.com  Set your password recovery email to for winlink.org to "me@example.com".
`
)

func AccountHandle(ctx context.Context, app *app.App, args []string) {
	switch cmd, args := shiftArgs(args); cmd {
	case "password.recovery.email":
		if err := passwordRecoveryEmailHandle(ctx, app, args); err != nil {
			fmt.Println("ERROR:", err)
			os.Exit(1)
		}
	default:
		fmt.Println("Missing argument, try 'account help'.")
	}
}

// getPasswordForCallsign gets the password for the specified callsign
// It tries the configured SecureLoginPassword first, then prompts if not available
func getPasswordForCallsign(ctx context.Context, a *app.App, callsign string) string {
	password := a.Config().SecureLoginPassword
	if password != "" {
		return password
	}

	select {
	case <-ctx.Done():
		return ""
	case resp := <-a.PromptHub().Prompt(ctx, time.Minute, app.PromptKindPassword, "Enter account password for "+callsign):
		if resp.Err != nil {
			log.Printf("Password prompt error: %v", resp.Err)
			return ""
		}
		return resp.Value
	}
}

func passwordRecoveryEmailHandle(ctx context.Context, a *app.App, args []string) error {
	mycall := a.Options().MyCall
	password := getPasswordForCallsign(ctx, a, mycall)

	arg, _ := shiftArgs(args)
	if arg != "" {
		if err := cmsapi.PasswordRecoveryEmailSet(ctx, mycall, password, arg); err != nil {
			return fmt.Errorf("failed to set value: %w", err)
		}
	}
	email, err := cmsapi.PasswordRecoveryEmailGet(ctx, mycall, password)
	switch {
	case err != nil:
		return fmt.Errorf("failed to get value: %w", err)
	case strings.TrimSpace(email) == "":
		email = "[not set]"
	}
	fmt.Println(email)
	return nil
}