Skip to content

Commit

Permalink
more refactor wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 10, 2021
1 parent aa46273 commit acbbc56
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ func (ac *AttachCmd) Name() string {
return ac.fs.Name()
}

func (ac *AttachCmd) Aliases() []string {
func (ac *AttachCmd) Alias() []string {
return ac.alias
}
2 changes: 1 addition & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func (ec *EnvCmd) Name() string {
return ec.fs.Name()
}

func (ec *EnvCmd) Aliases() []string {
func (ec *EnvCmd) Alias() []string {
return ec.alias
}
70 changes: 70 additions & 0 deletions cmd/kill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"flag"

"github.com/kkga/kks/kak"
)

func NewKillCmd() *KillCmd {
c := &KillCmd{
fs: flag.NewFlagSet("kill", flag.ExitOnError),
alias: []string{""},
}
c.fs.StringVar(&c.session, "s", "", "session")
c.fs.BoolVar(&c.all, "a", false, "all sessions")

return c
}

type KillCmd struct {
fs *flag.FlagSet
session string
all bool
alias []string
cc CmdContext
}

func (c *KillCmd) Run() error {
kakCmd := "kill"

sess := c.cc.Session
if c.session != "" {
sess = c.session
}

switch c.all {
case false:
if err := kak.Send(kakCmd, "", sess, ""); err != nil {
return err
}
case true:
sessions, err := kak.List()
if err != nil {
return err
}
for _, sess := range sessions {
if err := kak.Send(kakCmd, "", sess.Name, ""); err != nil {
return err
}
}
}

return nil
}

func (c *KillCmd) Init(args []string, cc CmdContext) error {
c.cc = cc
if err := c.fs.Parse(args); err != nil {
return err
}
return nil
}

func (c *KillCmd) Name() string {
return c.fs.Name()
}

func (c *KillCmd) Alias() []string {
return c.alias
}
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Runner interface {
Init([]string, CmdContext) error
Run() error
Name() string
Aliases() []string
Alias() []string
}

func Root(args []string) error {
Expand All @@ -19,9 +19,10 @@ func Root(args []string) error {
}

cmds := []Runner{
NewEnvCmd(),
NewAttachCmd(),
NewSendCmd(),
NewAttachCmd(),
NewKillCmd(),
NewEnvCmd(),
}

subcommand := os.Args[1]
Expand All @@ -31,7 +32,7 @@ func Root(args []string) error {
}

for _, cmd := range cmds {
if cmd.Name() == subcommand || containsString(cmd.Aliases(), subcommand) {
if cmd.Name() == subcommand || containsString(cmd.Alias(), subcommand) {
cmd.Init(os.Args[2:], *cmdCtx)
return cmd.Run()
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ func (c *SendCmd) Run() error {
kakCmd := strings.Join(c.fs.Args(), " ")

buf := ""
sess := c.cc.Session
cl := c.cc.Client

if c.buffer != "" {
buf = c.buffer
}
sess := c.cc.Session
if c.session != "" {
sess = c.session
}
cl := c.cc.Client
if c.client != "" {
cl = c.client
}
Expand Down Expand Up @@ -81,6 +80,6 @@ func (c *SendCmd) Name() string {
return c.fs.Name()
}

func (c *SendCmd) Aliases() []string {
func (c *SendCmd) Alias() []string {
return c.alias
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module github.com/kkga/kks

go 1.17

require github.com/fsnotify/fsnotify v1.5.1

require (
github.com/google/go-cmp v0.5.6 // indirect
golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 // indirect
github.com/fsnotify/fsnotify v1.5.1
github.com/google/go-cmp v0.5.6
)

require golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 // indirect
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWp
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 h1:3Ad41xy2WCESpufXwgs7NpDSu+vjxqLt2UFqUV+20bI=
golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit acbbc56

Please sign in to comment.