From ab0c78ac79acd91d1f7a772fa7b40d800e284b63 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Thu, 16 Sep 2021 15:41:55 +0300 Subject: [PATCH] clean up Edit cmd --- cmd/edit.go | 66 ++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/cmd/edit.go b/cmd/edit.go index d2456e9..d336709 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -3,7 +3,6 @@ package cmd import ( "flag" "fmt" - "os" "strings" "github.com/kkga/kks/kak" @@ -35,55 +34,40 @@ func (c *EditCmd) Run() error { switch c.kakContext.Session.Name { case "": - var gitDirName string - _, useGitDirSessions := os.LookupEnv("KKS_USE_GITDIR_SESSIONS") - - if useGitDirSessions { - gitDirName = fp.ParseGitDir() - } - - if gitDirName != "" { - // try gitdir-session - gitDirSession := kak.Session{Name: gitDirName} - exists, err := gitDirSession.Exists() - if err != nil { - return err - } - - if !exists { - sessionName, err := kak.Start(gitDirSession.Name) - if err != nil { - return err + kctx := &kak.Context{} + + if c.useGitDirSessions { + kctx.Session = kak.Session{Name: fp.ParseGitDir()} + + if kctx.Session.Name != "" { + if exists, _ := kctx.Session.Exists(); !exists { + sessionName, err := kak.Start(kctx.Session.Name) + if err != nil { + return err + } + fmt.Println("git-dir session started:", sessionName) } - fmt.Println("git-dir session started:", sessionName) } + } + + if kctx.Session.Name == "" { + kctx.Session = kak.Session{Name: c.defaultSession} + } - kctx := &kak.Context{Session: gitDirSession} + sessionExists, err := kctx.Session.Exists() + if err != nil { + return err + } + switch sessionExists { + case true: if err := kak.Connect(kctx, fp); err != nil { return err } - - } else { - defaultSession := kak.Session{Name: os.Getenv("KKS_DEFAULT_SESSION")} - exists, err := defaultSession.Exists() - if err != nil { + case false: + if err := kak.Run(&kak.Context{}, []string{}, fp); err != nil { return err } - - if exists { - // try default session - kctx := &kak.Context{Session: defaultSession} - if err := kak.Connect(kctx, fp); err != nil { - return err - } - - } else { - // if nothing: run one-off session - if err := kak.Run(&kak.Context{}, []string{}, fp); err != nil { - return err - } - } } default: