1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-23 21:51:07 +02:00

81 lines
1.7 KiB
Go
Raw Normal View History

2022-01-08 14:00:36 +11:00
package git_commands
2020-09-29 20:03:39 +10:00
import (
2021-08-23 22:35:19 +09:00
"io/ioutil"
"strconv"
2020-09-29 20:03:39 +10:00
"github.com/go-errors/errors"
2021-12-07 21:59:36 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2022-01-02 10:34:33 +11:00
"github.com/jesseduffield/lazygit/pkg/common"
2020-09-29 20:03:39 +10:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
2022-01-02 10:34:33 +11:00
type FileCommands struct {
*common.Common
2020-09-29 20:03:39 +10:00
2022-01-02 10:34:33 +11:00
cmd oscommands.ICmdObjBuilder
config *ConfigCommands
os FileOSCommand
2020-09-29 20:03:39 +10:00
}
2022-01-02 10:34:33 +11:00
type FileOSCommand interface {
Getenv(string) string
2020-09-29 20:03:39 +10:00
}
2022-01-02 10:34:33 +11:00
func NewFileCommands(
common *common.Common,
cmd oscommands.ICmdObjBuilder,
config *ConfigCommands,
osCommand FileOSCommand,
) *FileCommands {
return &FileCommands{
Common: common,
cmd: cmd,
config: config,
os: osCommand,
2020-09-29 20:03:39 +10:00
}
}
2022-01-02 10:34:33 +11:00
// Cat obtains the content of a file
func (self *FileCommands) Cat(fileName string) (string, error) {
buf, err := ioutil.ReadFile(fileName)
2020-09-29 20:03:39 +10:00
if err != nil {
2022-01-02 10:34:33 +11:00
return "", nil
2020-09-29 20:03:39 +10:00
}
2022-01-02 10:34:33 +11:00
return string(buf), nil
2020-09-29 20:03:39 +10:00
}
2022-01-08 14:00:36 +11:00
func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) {
editor := self.UserConfig.OS.EditCommand
2021-05-19 23:44:58 -07:00
if editor == "" {
2022-01-08 14:00:36 +11:00
editor = self.config.GetCoreEditor()
2021-05-19 23:44:58 -07:00
}
2021-04-01 20:27:06 +11:00
if editor == "" {
2022-01-08 14:00:36 +11:00
editor = self.os.Getenv("GIT_EDITOR")
2021-04-01 20:27:06 +11:00
}
if editor == "" {
2022-01-08 14:00:36 +11:00
editor = self.os.Getenv("VISUAL")
}
if editor == "" {
2022-01-08 14:00:36 +11:00
editor = self.os.Getenv("EDITOR")
}
if editor == "" {
2022-01-08 14:00:36 +11:00
if err := self.cmd.New("which vi").DontLog().Run(); err == nil {
editor = "vi"
}
}
if editor == "" {
2021-05-31 23:23:33 -07:00
return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
2021-04-10 11:40:42 +10:00
}
templateValues := map[string]string{
"editor": editor,
2022-01-08 14:00:36 +11:00
"filename": self.cmd.Quote(filename),
"line": strconv.Itoa(lineNumber),
}
2022-01-08 14:00:36 +11:00
editCmdTemplate := self.UserConfig.OS.EditCommandTemplate
2021-08-04 18:43:34 +09:00
return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil
2021-04-10 11:40:42 +10:00
}