From 1d43a40db1d1731a2721d92ad38dfdf00b5ff945 Mon Sep 17 00:00:00 2001 From: Gadzhi Kharkharov Date: Tue, 7 Sep 2021 21:08:58 +0300 Subject: [PATCH] parse attach command --- cmd/edit.go | 22 +++++++++++----------- cmd/list.go | 6 +++++- main.go | 10 ++++++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/cmd/edit.go b/cmd/edit.go index 8f668be..28d7e17 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -2,16 +2,14 @@ package cmd import ( "fmt" - "log" "os" "os/exec" "strings" "syscall" ) -func Edit(filename, session, client string) { - fmt.Println(session, client) - if session != "" && client != "" { +func Edit(filename, session, client string) error { + if filename != "" && session != "" && client != "" && client != "-" { kakCommand := fmt.Sprintf("edit %s", filename) Send(kakCommand, session, client) os.Exit(0) @@ -19,16 +17,15 @@ func Edit(filename, session, client string) { kakBinary, err := exec.LookPath("kak") if err != nil { - log.Fatal(err) + return err } - kakExecArgs := []string{"kak"} - sessions, err := exec.Command("kak", "-l").Output() if err != nil { - log.Fatal(err) + return err } + kakExecArgs := []string{"kak"} if session != "" && strings.Contains(string(sessions), session) { kakExecArgs = append(kakExecArgs, "-c", session) } else if session != "" { @@ -36,12 +33,15 @@ func Edit(filename, session, client string) { kakExecArgs = append(kakExecArgs, "-s", session) } - kakExecArgs = append(kakExecArgs, filename) + if filename != "" { + kakExecArgs = append(kakExecArgs, filename) + } - fmt.Print(kakExecArgs) + fmt.Println("edit", kakExecArgs) execErr := syscall.Exec(kakBinary, kakExecArgs, os.Environ()) if execErr != nil { - log.Fatal(err) + return err } + return nil } diff --git a/cmd/list.go b/cmd/list.go index 463b031..c6b4afd 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -43,7 +43,11 @@ func List() { for _, session := range sessions { for _, client := range session.clients { - fmt.Printf("%s\t%s\t%s\n", session.name, client, session.dir) + if client != "" { + fmt.Printf("%s\t%s\t%s\n", session.name, client, session.dir) + } else { + fmt.Printf("%s\t%s\t%s\n", session.name, "-", session.dir) + } } } diff --git a/main.go b/main.go index ead5344..6ea3b99 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,16 @@ func main() { cmd.Edit(filename, context.session, context.client) } + if attachCmd.Parsed() { + context, err := getContext() + if err != nil { + log.Fatal(err) + } + if err := cmd.Edit("", context.session, context.client); err != nil { + log.Fatal(err) + } + } + if sendCmd.Parsed() { args := sendCmd.Args() kakCommand := strings.Join(args, " ")