parent
93c49520ed
commit
c18e04b8c0
55
cmd/stop.go
Normal file
55
cmd/stop.go
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright © 2021 James Walker <walkah@walkah.net>
|
||||
|
||||
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)
|
||||
}
|
@ -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")
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user