summaryrefslogtreecommitdiffstats
path: root/pkg/gui/credentials_panel.go
blob: aa2caecfa0bf93925519000b56b328f9066bd623 (plain)
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
package gui

import (
	"strings"

	"github.com/jesseduffield/gocui"
)

type credentials chan string

// waitForPassUname wait for a username or password input from the credentials popup
func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string {
	gui.credentials = make(chan string)
	g.Update(func(g *gocui.Gui) error {
		credentialsView, _ := g.View("credentials")
		if passOrUname == "username" {
			credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername")
			credentialsView.Mask = 0
		} else {
			credentialsView.Title = gui.Tr.SLocalize("CredentialsPassword")
			credentialsView.Mask = '*'
		}
		err := gui.switchFocus(g, currentView, credentialsView)
		if err != nil {
			return err
		}
		gui.RenderCommitLength()
		return nil
	})

	// wait for username/passwords input
	userInput := <-gui.credentials
	return userInput + "\n"
}

func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
	message := gui.trimmedContent(v)
	gui.credentials <- message
	err := gui.refreshFiles()
	if err != nil {
		return err
	}
	v.Clear()
	err = v.SetCursor(0, 0)
	if err != nil {
		return err
	}
	_, err = g.SetViewOnBottom("credentials")
	if err != nil {
		return err
	}
	nextView, err := gui.g.View("confirmation")
	if err != nil {
		nextView = gui.getFilesView()
	}
	err = gui.switchFocus(g, nil, nextView)
	if err != nil {
		return err
	}
	return gui.refreshCommits(g)
}

func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
	_, err := g.SetViewOnBottom("credentials")
	if err != nil {
		return err
	}

	gui.credentials <- ""
	return gui.switchFocus(g, nil, gui.getFilesView())
}

func (gui *Gui) handleCredentialsViewFocused(g *gocui.Gui, v *gocui.View) error {
	if _, err := g.SetViewOnTop("credentials"); err != nil {
		return err
	}

	message := gui.Tr.TemplateLocalize(
		"CloseConfirm",
		Teml{
			"keyBindClose":   "esc",
			"keyBindConfirm": "enter",
		},
	)
	gui.renderString(g, "options", message)
	return nil
}

// HandleCredentialsPopup handles the views after executing a command that might ask for credentials
func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr error) {
	if popupOpened {
		_, _ = gui.g.SetViewOnBottom("credentials")
	}
	if cmdErr != nil {
		errMessage := cmdErr.Error()
		if strings.Contains(errMessage, "Invalid username or password") {
			errMessage = gui.Tr.SLocalize("PassUnameWrong")
		}
		// we are not logging this error because it may contain a password
		_ = gui.createSpecificErrorPanel(errMessage, gui.getFilesView(), false)
	} else {
		_ = gui.closeConfirmationPrompt(g, true)
		_ = gui.refreshSidePanels(g)
	}
}