From c18e04b8c045dd89669516a6dd88ffd0d51b1fd9 Mon Sep 17 00:00:00 2001 From: James Walker Date: Sat, 4 Dec 2021 21:35:41 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20stop=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #7 --- cmd/stop.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ tmux/project.go | 51 ++++++++++++++++++++++++++++++--------------- tmux/tmux.go | 5 +++++ 3 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 cmd/stop.go diff --git a/cmd/stop.go b/cmd/stop.go new file mode 100644 index 0000000..0a398e4 --- /dev/null +++ b/cmd/stop.go @@ -0,0 +1,55 @@ +/* +Copyright © 2021 James Walker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/walkah/workon/tmux" +) + +// stopCmd represents the stop command +var stopCmd = &cobra.Command{ + Use: "stop", + Short: "Stop a running project", + Args: cobra.RangeArgs(0, 1), + Run: func(cmd *cobra.Command, args []string) { + var projects []string + if len(args) == 0 { + t := tmux.Tmux{} + projects = t.ListSessions() + } else { + projects = args + } + + for _, project := range projects { + fmt.Println("stopping", project) + tmux.StopProject(project) + } + }, + ValidArgsFunction: completeProjects, +} + +func init() { + rootCmd.AddCommand(stopCmd) +} diff --git a/tmux/project.go b/tmux/project.go index a1c75f5..a7827f7 100644 --- a/tmux/project.go +++ b/tmux/project.go @@ -19,6 +19,7 @@ type Project struct { Name string `yaml:"name"` Root string `yaml:"root"` OnProjectStart []string `yaml:"on_project_start,omitempty"` + OnProjectStop []string `yaml:"on_project_stop,omitempty"` Windows []Window `yaml:"windows"` } @@ -33,37 +34,38 @@ func StartProject(name string) { if !sessionExists(name) { // Run startup commands - if len(p.OnProjectStart) > 0 { - for _, command := range p.OnProjectStart { - args := strings.Fields(command) - cmd := exec.Command(args[0], args[1:]...) - cmd.Dir = p.GetRoot() - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err != nil { - fmt.Println("Unable to run start command:", err) - os.Exit(1) - } - } - } + p.RunCommands(p.OnProjectStart) tmux.Run("new-session", "-d", "-s", name, "-n", p.Windows[0].Name, "-c", p.Windows[0].Root) - for index, window := range p.Windows { if index > 0 { window.Create(tmux) } - window.SendCommands(tmux) } - p.Windows[0].Focus(tmux) } tmux.Attach(name) } +func StopProject(name string) { + if !sessionExists(name) { + return + } + + t := Tmux{} + t.KillSession(name) + + p, err := LoadProject(name) + if err != nil { + fmt.Println("Unable to load project:", err) + os.Exit(1) + } + + p.RunCommands(p.OnProjectStop) +} + func ListProjects() error { projects, err := ProjectList() if err != nil { @@ -194,6 +196,21 @@ func (p *Project) GetRoot() string { return rootPath } +func (p *Project) RunCommands(commands []string) { + for _, command := range commands { + args := strings.Fields(command) + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = p.GetRoot() + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + fmt.Println("Unable to run command:", err) + os.Exit(1) + } + } +} + func getConfigDir() string { home, _ := homedir.Dir() return path.Join(home, ".workon") diff --git a/tmux/tmux.go b/tmux/tmux.go index 8963253..9e0eda0 100644 --- a/tmux/tmux.go +++ b/tmux/tmux.go @@ -54,6 +54,11 @@ func (t *Tmux) ListSessions() []string { return strings.Split(lines, "\n") } +func (t *Tmux) KillSession(name string) error { + _, err := t.Exec("kill-session", "-t", name) + return err +} + func (t *Tmux) getBinary() string { if t.BinPath != "" { return t.BinPath