summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/file.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-01-08 14:00:36 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-01-09 14:09:53 +1100
commitc9a0cc6b30dca6ff6c520268c10afff4e99a68e9 (patch)
tree74a03be28aafb5fba5c8391ca611aeb25ca89445 /pkg/commands/git_commands/file.go
parent3621854dc79baf2064b00b561ebea0ecc8fcb5df (diff)
refactor
Diffstat (limited to 'pkg/commands/git_commands/file.go')
-rw-r--r--pkg/commands/git_commands/file.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go
new file mode 100644
index 000000000..070a50790
--- /dev/null
+++ b/pkg/commands/git_commands/file.go
@@ -0,0 +1,80 @@
+package git_commands
+
+import (
+ "io/ioutil"
+ "strconv"
+
+ "github.com/go-errors/errors"
+ "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
+ "github.com/jesseduffield/lazygit/pkg/common"
+ "github.com/jesseduffield/lazygit/pkg/utils"
+)
+
+type FileCommands struct {
+ *common.Common
+
+ cmd oscommands.ICmdObjBuilder
+ config *ConfigCommands
+ os FileOSCommand
+}
+
+type FileOSCommand interface {
+ Getenv(string) string
+}
+
+func NewFileCommands(
+ common *common.Common,
+ cmd oscommands.ICmdObjBuilder,
+ config *ConfigCommands,
+ osCommand FileOSCommand,
+) *FileCommands {
+ return &FileCommands{
+ Common: common,
+ cmd: cmd,
+ config: config,
+ os: osCommand,
+ }
+}
+
+// Cat obtains the content of a file
+func (self *FileCommands) Cat(fileName string) (string, error) {
+ buf, err := ioutil.ReadFile(fileName)
+ if err != nil {
+ return "", nil
+ }
+ return string(buf), nil
+}
+
+func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) {
+ editor := self.UserConfig.OS.EditCommand
+
+ if editor == "" {
+ editor = self.config.GetCoreEditor()
+ }
+ if editor == "" {
+ editor = self.os.Getenv("GIT_EDITOR")
+ }
+ if editor == "" {
+ editor = self.os.Getenv("VISUAL")
+ }
+ if editor == "" {
+ editor = self.os.Getenv("EDITOR")
+ }
+ if editor == "" {
+ if err := self.cmd.New("which vi").DontLog().Run(); err == nil {
+ editor = "vi"
+ }
+ }
+ if editor == "" {
+ return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
+ }
+
+ templateValues := map[string]string{
+ "editor": editor,
+ "filename": self.cmd.Quote(filename),
+ "line": strconv.Itoa(lineNumber),
+ }
+
+ editCmdTemplate := self.UserConfig.OS.EditCommandTemplate
+ return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil
+}