1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/gpg.go

67 lines
2.0 KiB
Go
Raw Normal View History

2021-04-10 03:40:42 +02:00
package gui
import (
"fmt"
2021-12-07 12:59:36 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2021-04-10 03:40:42 +02:00
// Currently there is a bug where if we switch to a subprocess from within
// WithWaitingStatus we get stuck there and can't return to lazygit. We could
// fix this bug, or just stop running subprocesses from within there, given that
// we don't need to see a loading status if we're in a subprocess.
2021-12-07 12:59:36 +02:00
// TODO: work out if we actually need to use a shell command here
func (gui *Gui) withGpgHandling(cmdObj oscommands.ICmdObj, waitingStatus string, onSuccess func() error) error {
gui.LogCommand(cmdObj.ToString(), true)
2022-01-05 03:01:59 +02:00
useSubprocess := gui.git.Config.UsingGpg()
2021-04-10 03:40:42 +02:00
if useSubprocess {
2021-12-29 05:33:38 +02:00
success, err := gui.runSubprocessWithSuspense(gui.OSCommand.Cmd.NewShell(cmdObj.ToString()))
2021-04-10 03:40:42 +02:00
if success && onSuccess != nil {
if err := onSuccess(); err != nil {
return err
}
}
if err := gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}); err != nil {
2021-04-10 03:40:42 +02:00
return err
}
return err
2021-04-10 03:40:42 +02:00
} else {
2021-12-07 12:59:36 +02:00
return gui.RunAndStream(cmdObj, waitingStatus, onSuccess)
}
}
2021-12-07 12:59:36 +02:00
func (gui *Gui) RunAndStream(cmdObj oscommands.ICmdObj, waitingStatus string, onSuccess func() error) error {
return gui.c.WithWaitingStatus(waitingStatus, func() error {
2021-12-29 05:33:38 +02:00
cmdObj := gui.OSCommand.Cmd.NewShell(cmdObj.ToString())
2021-12-07 12:59:36 +02:00
cmdObj.AddEnvVars("TERM=dumb")
cmdWriter := gui.getCmdWriter()
2021-12-07 12:59:36 +02:00
cmd := cmdObj.GetCmd()
cmd.Stdout = cmdWriter
cmd.Stderr = cmdWriter
if err := cmd.Run(); err != nil {
if _, err := cmd.Stdout.Write([]byte(fmt.Sprintf("%s\n", style.FgRed.Sprint(err.Error())))); err != nil {
gui.c.Log.Error(err)
2021-04-10 03:40:42 +02:00
}
_ = gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
return gui.c.Error(
fmt.Errorf(
gui.c.Tr.GitCommandFailed, gui.c.UserConfig.Keybinding.Universal.ExtrasMenu,
),
)
}
2021-04-10 03:40:42 +02:00
if onSuccess != nil {
if err := onSuccess(); err != nil {
return err
}
}
2021-04-10 03:40:42 +02:00
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
})
2021-04-10 03:40:42 +02:00
}