github.com/andrewrech/lazygit@v0.8.1/pkg/gui/credentials_panel.go (about)

     1  package gui
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jesseduffield/gocui"
     7  )
     8  
     9  type credentials chan string
    10  
    11  // waitForPassUname wait for a username or password input from the credentials popup
    12  func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string {
    13  	gui.credentials = make(chan string)
    14  	g.Update(func(g *gocui.Gui) error {
    15  		credentialsView, _ := g.View("credentials")
    16  		if passOrUname == "username" {
    17  			credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername")
    18  			credentialsView.Mask = 0
    19  		} else {
    20  			credentialsView.Title = gui.Tr.SLocalize("CredentialsPassword")
    21  			credentialsView.Mask = '*'
    22  		}
    23  		err := gui.switchFocus(g, currentView, credentialsView)
    24  		if err != nil {
    25  			return err
    26  		}
    27  		gui.RenderCommitLength()
    28  		return nil
    29  	})
    30  
    31  	// wait for username/passwords input
    32  	userInput := <-gui.credentials
    33  	return userInput + "\n"
    34  }
    35  
    36  func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
    37  	message := gui.trimmedContent(v)
    38  	gui.credentials <- message
    39  	err := gui.refreshFiles()
    40  	if err != nil {
    41  		return err
    42  	}
    43  	v.Clear()
    44  	err = v.SetCursor(0, 0)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	_, err = g.SetViewOnBottom("credentials")
    49  	if err != nil {
    50  		return err
    51  	}
    52  	nextView, err := gui.g.View("confirmation")
    53  	if err != nil {
    54  		nextView = gui.getFilesView()
    55  	}
    56  	err = gui.switchFocus(g, nil, nextView)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	return gui.refreshCommits(g)
    61  }
    62  
    63  func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
    64  	_, err := g.SetViewOnBottom("credentials")
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	gui.credentials <- ""
    70  	return gui.switchFocus(g, nil, gui.getFilesView())
    71  }
    72  
    73  func (gui *Gui) handleCredentialsViewFocused(g *gocui.Gui, v *gocui.View) error {
    74  	if _, err := g.SetViewOnTop("credentials"); err != nil {
    75  		return err
    76  	}
    77  
    78  	message := gui.Tr.TemplateLocalize(
    79  		"CloseConfirm",
    80  		Teml{
    81  			"keyBindClose":   "esc",
    82  			"keyBindConfirm": "enter",
    83  		},
    84  	)
    85  	return gui.renderString(g, "options", message)
    86  }
    87  
    88  // HandleCredentialsPopup handles the views after executing a command that might ask for credentials
    89  func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr error) {
    90  	if popupOpened {
    91  		_, _ = gui.g.SetViewOnBottom("credentials")
    92  	}
    93  	if cmdErr != nil {
    94  		errMessage := cmdErr.Error()
    95  		if strings.Contains(errMessage, "Invalid username or password") {
    96  			errMessage = gui.Tr.SLocalize("PassUnameWrong")
    97  		}
    98  		// we are not logging this error because it may contain a password
    99  		_ = gui.createSpecificErrorPanel(errMessage, gui.getFilesView(), false)
   100  	} else {
   101  		_ = gui.closeConfirmationPrompt(g)
   102  		_ = gui.refreshSidePanels(g)
   103  	}
   104  }