Skip to content

Commit

Permalink
clean up Edit cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 16, 2021
1 parent 266d861 commit ab0c78a
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"flag"
"fmt"
"os"
"strings"

"github.com/kkga/kks/kak"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ab0c78a

Please sign in to comment.