-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
cli.go
111 lines (95 loc) · 2.68 KB
/
cli.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package auth_ui
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/fatih/color"
"golang.org/x/term"
)
// CLI is the archaic fallback UI for auth.
type CLI struct{}
func (*CLI) instructions(w io.Writer) {
const welcome = "Welcome to Slackdump EZ-Login 3000"
underline := color.Set(color.Underline)
fmt.Fprintf(w, "%s\n\n", underline.Sprint(welcome))
fmt.Fprintf(w, "Please read these instructions carefully:\n\n")
fmt.Fprintf(w, "1. Enter the slack workspace name or paste the URL of your slack workspace.\n\n HINT: If https://example.slack.com is the Slack URL of your company,\n then 'example' is the Slack Workspace name\n\n")
fmt.Fprintf(w, "2. Browser will open, login as usual.\n\n")
fmt.Fprintf(w, "3. Browser will close and slackdump will be authenticated.\n\n\n")
}
func (cl *CLI) RequestWorkspace(w io.Writer) (string, error) {
cl.instructions(w)
workspace, err := prompt(w, "Enter Slack Workspace Name or URL: ", readln)
if err != nil {
return "", err
}
return Sanitize(workspace)
}
func (cl *CLI) RequestEmail(w io.Writer) (string, error) {
return prompt(w, "Enter Email: ", readln)
}
func (cl *CLI) RequestPassword(w io.Writer, account string) (string, error) {
defer fmt.Fprintln(w)
return prompt(w, fmt.Sprintf("Enter Password for %s (won't be visible): ", account), readpwd)
}
func (cl *CLI) RequestLoginType(w io.Writer) (int, error) {
var types = []struct {
name string
value int
}{
{"Email", LoginEmail},
{"Google", LoginSSO},
{"Apple", LoginSSO},
{"Login with Single-Sign-On (SSO)", LoginSSO},
{"Other/Manual", LoginSSO},
{"Cancel", LoginCancel},
}
var idx int = -1
for idx < 0 || idx >= len(types) {
fmt.Fprintf(w, "Select login type:\n")
for i, t := range types {
fmt.Fprintf(w, "\t%d. %s\n", i+1, t.name)
}
fmt.Fprintf(w, "Enter number, and press Enter: ")
_, err := fmt.Fscanf(os.Stdin, "%d", &idx)
if err != nil {
fmt.Fprintln(w, err)
continue
}
idx -= 1 // adjusting for 0-index
if idx < 0 || idx >= len(types) {
fmt.Fprintln(w, "invalid login type")
}
}
return types[idx].value, nil
}
func (*CLI) Stop() {}
func readln(r *os.File) (string, error) {
line, err := bufio.NewReader(r).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(line), nil
}
func readpwd(f *os.File) (string, error) {
pwd, err := term.ReadPassword(int(f.Fd()))
if err != nil {
return "", err
}
return string(pwd), nil
}
func prompt(w io.Writer, prompt string, readlnFn func(*os.File) (string, error)) (string, error) {
for {
fmt.Fprint(w, prompt)
v, err := readlnFn(os.Stdin)
if err != nil {
return "", err
}
if v != "" {
return v, nil
}
fmt.Fprintln(w, "input cannot be empty")
}
}