1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-16 11:37:01 +02:00
lazygit/pkg/gui/controllers/submodules_controller.go

240 lines
7.6 KiB
Go
Raw Normal View History

2022-01-28 11:44:36 +02:00
package controllers
import (
"fmt"
"path/filepath"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
2022-02-05 08:04:10 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubmodulesController struct {
2022-02-05 05:42:56 +02:00
baseController
2022-02-06 06:54:26 +02:00
*controllerCommon
2022-02-05 08:04:10 +02:00
enterSubmodule func(submodule *models.SubmoduleConfig) error
2022-01-28 11:44:36 +02:00
}
var _ types.IController = &SubmodulesController{}
2022-01-28 11:44:36 +02:00
func NewSubmodulesController(
2022-02-06 06:54:26 +02:00
controllerCommon *controllerCommon,
enterSubmodule func(submodule *models.SubmoduleConfig) error,
2022-01-28 11:44:36 +02:00
) *SubmodulesController {
return &SubmodulesController{
2022-02-06 06:54:26 +02:00
baseController: baseController{},
controllerCommon: controllerCommon,
enterSubmodule: enterSubmodule,
2022-01-28 11:44:36 +02:00
}
}
2022-02-05 01:31:07 +02:00
func (self *SubmodulesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
return []*types.Binding{
2022-01-28 11:44:36 +02:00
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcEnterSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcRemoveSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.Update),
Handler: self.checkSelected(self.update),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcSubmoduleUpdate,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.New),
2022-01-28 11:44:36 +02:00
Handler: self.add,
Description: self.c.Tr.LcAddSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.editURL),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcEditSubmoduleUrl,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.Init),
Handler: self.checkSelected(self.init),
2022-01-28 11:44:36 +02:00
Description: self.c.Tr.LcInitSubmodule,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Submodules.BulkMenu),
2022-01-28 11:44:36 +02:00
Handler: self.openBulkActionsMenu,
Description: self.c.Tr.LcViewBulkSubmoduleOptions,
OpensMenu: true,
},
}
}
2022-02-27 02:42:22 +02:00
func (self *SubmodulesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
2022-01-28 11:44:36 +02:00
func (self *SubmodulesController) enter(submodule *models.SubmoduleConfig) error {
return self.enterSubmodule(submodule)
2022-01-28 11:44:36 +02:00
}
func (self *SubmodulesController) add() error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmoduleUrl,
HandleConfirm: func(submoduleUrl string) error {
nameSuggestion := filepath.Base(strings.TrimSuffix(submoduleUrl, filepath.Ext(submoduleUrl)))
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmoduleName,
InitialContent: nameSuggestion,
HandleConfirm: func(submoduleName string) error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcNewSubmodulePath,
InitialContent: submoduleName,
HandleConfirm: func(submodulePath string) error {
return self.c.WithWaitingStatus(self.c.Tr.LcAddingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.AddSubmodule)
err := self.git.Submodule.Add(submoduleName, submodulePath, submoduleUrl)
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
})
},
})
},
})
}
func (self *SubmodulesController) editURL(submodule *models.SubmoduleConfig) error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
2022-01-28 11:44:36 +02:00
Title: fmt.Sprintf(self.c.Tr.LcUpdateSubmoduleUrl, submodule.Name),
InitialContent: submodule.Url,
HandleConfirm: func(newUrl string) error {
return self.c.WithWaitingStatus(self.c.Tr.LcUpdatingSubmoduleUrlStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.UpdateSubmoduleUrl)
err := self.git.Submodule.UpdateUrl(submodule.Name, submodule.Path, newUrl)
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
})
}
func (self *SubmodulesController) init(submodule *models.SubmoduleConfig) error {
return self.c.WithWaitingStatus(self.c.Tr.LcInitializingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.InitialiseSubmodule)
err := self.git.Submodule.Init(submodule.Path)
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
}
func (self *SubmodulesController) openBulkActionsMenu() error {
2022-01-29 10:09:20 +02:00
return self.c.Menu(types.CreateMenuOptions{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.LcBulkSubmoduleOptions,
2022-01-29 10:09:20 +02:00
Items: []*types.MenuItem{
2022-01-28 11:44:36 +02:00
{
DisplayStrings: []string{self.c.Tr.LcBulkInitSubmodules, style.FgGreen.Sprint(self.git.Submodule.BulkInitCmdObj().ToString())},
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkInitialiseSubmodules)
err := self.git.Submodule.BulkInitCmdObj().Run()
if err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'i',
2022-01-28 11:44:36 +02:00
},
{
DisplayStrings: []string{self.c.Tr.LcBulkUpdateSubmodules, style.FgYellow.Sprint(self.git.Submodule.BulkUpdateCmdObj().ToString())},
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkUpdateSubmodules)
if err := self.git.Submodule.BulkUpdateCmdObj().Run(); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'u',
2022-01-28 11:44:36 +02:00
},
{
DisplayStrings: []string{self.c.Tr.LcBulkDeinitSubmodules, style.FgRed.Sprint(self.git.Submodule.BulkDeinitCmdObj().ToString())},
OnPress: func() error {
return self.c.WithWaitingStatus(self.c.Tr.LcRunningCommand, func() error {
self.c.LogAction(self.c.Tr.Actions.BulkDeinitialiseSubmodules)
if err := self.git.Submodule.BulkDeinitCmdObj().Run(); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
},
Key: 'd',
2022-01-28 11:44:36 +02:00
},
},
})
}
func (self *SubmodulesController) update(submodule *models.SubmoduleConfig) error {
return self.c.WithWaitingStatus(self.c.Tr.LcUpdatingSubmoduleStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.UpdateSubmodule)
err := self.git.Submodule.Update(submodule.Path)
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES}})
})
}
func (self *SubmodulesController) remove(submodule *models.SubmoduleConfig) error {
2022-01-29 10:09:20 +02:00
return self.c.Ask(types.AskOpts{
2022-01-28 11:44:36 +02:00
Title: self.c.Tr.RemoveSubmodule,
Prompt: fmt.Sprintf(self.c.Tr.RemoveSubmodulePrompt, submodule.Name),
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.RemoveSubmodule)
if err := self.git.Submodule.Delete(submodule); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUBMODULES, types.FILES}})
},
})
}
func (self *SubmodulesController) checkSelected(callback func(*models.SubmoduleConfig) error) func() error {
2022-01-28 11:44:36 +02:00
return func() error {
2022-02-06 06:54:26 +02:00
submodule := self.context().GetSelected()
2022-01-28 11:44:36 +02:00
if submodule == nil {
return nil
}
return callback(submodule)
}
}
func (self *SubmodulesController) Context() types.Context {
2022-02-06 06:54:26 +02:00
return self.context()
}
func (self *SubmodulesController) context() *context.SubmodulesContext {
return self.contexts.Submodules
}