From 7bacbe9a4af56f8b08a3b1f0f85b9446fc83cb21 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Fri, 17 Sep 2021 22:23:02 +0300 Subject: [PATCH] refactor --- cmd/edit.go | 5 ++--- cmd/env.go | 5 ++--- cmd/kill.go | 13 ++++++------- cmd/list.go | 7 ++----- cmd/send.go | 13 ++++++------- kak/run.go | 2 +- 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cmd/edit.go b/cmd/edit.go index 594f456..8c6faed 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -80,13 +80,12 @@ func findOrRunSession(c *EditCmd, fp *kak.Filepath) error { } func connectOrEditInClient(c *EditCmd, fp *kak.Filepath) error { - switch c.kctx.Client.Name { - case "": + if c.kctx.Client.Name == "" { // if no client, attach to session with new client if err := kak.Connect(c.kctx, fp); err != nil { return err } - default: + } else { // if client set, send 'edit [file]' to client sb := strings.Builder{} sb.WriteString(fmt.Sprintf("edit -existing %s", fp.Name)) diff --git a/cmd/env.go b/cmd/env.go index fc2d162..291dd00 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -24,8 +24,7 @@ type EnvCmd struct { } func (c *EnvCmd) Run() error { - switch c.json { - case true: + if c.json { j, err := json.MarshalIndent( map[string]string{ "session": c.session, @@ -35,7 +34,7 @@ func (c *EnvCmd) Run() error { return err } fmt.Println(string(j)) - case false: + } else { fmt.Printf("session: %s\n", c.session) fmt.Printf("client: %s\n", c.client) } diff --git a/cmd/kill.go b/cmd/kill.go index c7ef7a2..479b563 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -26,13 +26,7 @@ type KillCmd struct { func (c *KillCmd) Run() error { sendCmd := "kill" - switch c.allSessions { - case false: - // TODO need to somehow trigger "no session" err - if err := kak.Send(c.kctx, sendCmd); err != nil { - return err - } - case true: + if c.allSessions { sessions, err := kak.Sessions() if err != nil { return err @@ -47,6 +41,11 @@ func (c *KillCmd) Run() error { return err } } + } else { + // TODO need to somehow trigger "no session" err + if err := kak.Send(c.kctx, sendCmd); err != nil { + return err + } } return nil diff --git a/cmd/list.go b/cmd/list.go index a6e635b..7ac85b1 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -32,9 +32,7 @@ func (c *ListCmd) Run() error { return err } - switch c.json { - - case true: + if c.json { type session struct { Name string `json:"name"` Clients []string `json:"clients"` @@ -65,8 +63,7 @@ func (c *ListCmd) Run() error { } fmt.Println(string(j)) - - case false: + } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) diff --git a/cmd/send.go b/cmd/send.go index 1383f89..768e108 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -30,13 +30,7 @@ func (c *SendCmd) Run() error { // TODO probably need to do some shell escaping here sendCmd := strings.Join(c.fs.Args(), " ") - switch c.allClients { - case false: - // TODO: need to trigger "session not set" error - if err := kak.Send(c.kctx, sendCmd); err != nil { - return err - } - case true: + if c.allClients { sessions, err := kak.Sessions() if err != nil { return err @@ -53,6 +47,11 @@ func (c *SendCmd) Run() error { return err } } + } else { + // TODO: need to trigger "session not set" error + if err := kak.Send(c.kctx, sendCmd); err != nil { + return err + } } return nil diff --git a/kak/run.go b/kak/run.go index fbdb125..012378a 100644 --- a/kak/run.go +++ b/kak/run.go @@ -20,7 +20,7 @@ func Run(kctx *Context, kakArgs []string, fp *Filepath) error { case "-c": kakExecArgs = append(kakExecArgs, "-c", kctx.Session.Name) default: - return errors.New(fmt.Sprintf("unknown argument to Run: %s", a)) + return errors.New(fmt.Sprintf("Unknown argument to Run: %s", a)) } }