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