forked from rusq/slackdump
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rusq#92 from rusq/auth
Cherry pick auth changes from login
- Loading branch information
Showing
19 changed files
with
447 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package auth_ui | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
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) | ||
fmt.Fprint(w, "Enter Slack Workspace Name: ") | ||
workspace, err := readln(os.Stdin) | ||
if err != nil { | ||
return "", err | ||
} | ||
return workspace, nil | ||
} | ||
|
||
func (*CLI) Stop() { | ||
return | ||
} | ||
|
||
func readln(r io.Reader) (string, error) { | ||
line, err := bufio.NewReader(r).ReadString('\n') | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(line), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package auth_ui | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
"github.com/rusq/dlog" | ||
) | ||
|
||
type TView struct { | ||
app *tview.Application | ||
|
||
mustStop chan struct{} | ||
inputReceived chan struct{} | ||
done chan struct{} | ||
} | ||
|
||
func (tv *TView) RequestWorkspace(w io.Writer) (string, error) { | ||
tv.inputReceived = make(chan struct{}, 1) | ||
tv.mustStop = make(chan struct{}, 1) | ||
tv.done = make(chan struct{}, 1) | ||
tv.app = tview.NewApplication() | ||
|
||
var workspace string | ||
var exit bool | ||
input := tview.NewInputField().SetLabel("Slack Workspace").SetFieldWidth(40) | ||
form := tview.NewForm(). | ||
AddFormItem(input). | ||
AddButton("OK", func() { | ||
workspace = input.GetText() | ||
tv.wait() | ||
}). | ||
AddButton("Cancel", func() { | ||
exit = true | ||
tv.wait() | ||
}) | ||
|
||
form.SetBorder(true). | ||
SetTitle(" Slackdump EZ-Login 3000 "). | ||
SetBackgroundColor(tcell.ColorDarkCyan). | ||
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if !input.HasFocus() { | ||
return event | ||
} | ||
switch event.Key() { | ||
default: | ||
return event | ||
case tcell.KeyCR: | ||
workspace = input.GetText() | ||
case tcell.KeyESC: | ||
exit = true | ||
|
||
} | ||
tv.wait() | ||
return nil | ||
}) | ||
|
||
go func() { | ||
if err := tv.app.SetRoot(modal(form, 60, 7), true).EnableMouse(true).Run(); err != nil { | ||
dlog.Println(err) | ||
} | ||
}() | ||
|
||
// waiting for the user to finish interaction | ||
<-tv.inputReceived | ||
if exit { | ||
tv.app.Stop() | ||
return "", errors.New("operation cancelled") | ||
} | ||
return workspace, nil | ||
} | ||
|
||
func (tv *TView) wait() { | ||
close(tv.inputReceived) | ||
<-tv.mustStop | ||
tv.app.Stop() | ||
close(tv.done) | ||
} | ||
|
||
func (tv *TView) Stop() { | ||
close(tv.mustStop) | ||
<-tv.done | ||
} | ||
|
||
func modal(p tview.Primitive, width int, height int) tview.Primitive { | ||
return tview.NewGrid(). | ||
SetColumns(0, width, 0). | ||
SetRows(0, height, 0). | ||
AddItem(p, 1, 1, 1, 1, 0, 0, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.