parse project files. start command

This commit is contained in:
2020-04-13 22:06:09 -04:00
parent eb403c7115
commit 84f1f289ca
7 changed files with 285 additions and 2 deletions

91
tmux/project.go Normal file
View File

@ -0,0 +1,91 @@
package tmux
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)
type Project struct {
Name string `yaml:"name"`
Root string `yaml:"root"`
Windows []Window `yaml:"windows"`
}
func StartProject(name string) {
p, err := LoadProject(name)
if err != nil {
fmt.Println("Unable to load project:", err)
os.Exit(1)
}
tmux := CreateTmux(false)
if !sessionExists(name) {
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)
}
// LoadProject loads and parses the config for the given project.
func LoadProject(name string) (*Project, error) {
project := &Project{}
home, _ := homedir.Dir()
fileName := path.Join(home, ".workon", name+".yml")
data, err := ioutil.ReadFile(fileName)
if err != nil {
return project, err
}
err = yaml.Unmarshal(data, project)
if len(project.Windows) < 1 {
return project, errors.New("No windows defined")
}
rootPath := project.GetRoot()
for index, window := range project.Windows {
project.Windows[index].ID = fmt.Sprintf("%s:%d", project.Name, index)
project.Windows[index].Root = filepath.Join(rootPath, window.Root)
}
return project, err
}
func (p *Project) GetRoot() string {
rootPath, err := homedir.Expand(p.Root)
if err != nil {
fmt.Println("Unable to find root path")
}
return rootPath
}
func sessionExists(name string) bool {
t := Tmux{}
result, err := t.Exec("ls")
if err != nil {
return false
}
re := regexp.MustCompile(fmt.Sprintf("^%s:", name))
return re.MatchString(string(result))
}

56
tmux/tmux.go Normal file
View File

@ -0,0 +1,56 @@
package tmux
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
)
type Tmux struct {
BinPath string
Debug bool
}
func CreateTmux(debug bool) *Tmux {
return &Tmux{Debug: debug}
}
func (t *Tmux) Exec(args ...string) ([]byte, error) {
bin := t.getBinary()
if t.Debug {
fmt.Println(bin, strings.Join(args, " "))
}
return exec.Command(bin, args...).CombinedOutput()
}
func (t *Tmux) Run(args ...string) {
output, err := t.Exec(args...)
if err != nil {
fmt.Println(err, string(output))
}
}
func (t *Tmux) Attach(name string) {
args := []string{}
args = append(args, "-u", "attach-session", "-t", name)
err := syscall.Exec(t.getBinary(), args, os.Environ())
if err != nil {
fmt.Println("Error:", err)
}
}
func (t *Tmux) getBinary() string {
if t.BinPath != "" {
return t.BinPath
}
tmux, err := exec.LookPath("tmux")
if err != nil {
fmt.Println("Error:", err)
}
return tmux
}

27
tmux/window.go Normal file
View File

@ -0,0 +1,27 @@
package tmux
import (
"strings"
)
type Window struct {
Name string `yaml:"name"`
Root string `yaml:"root"`
Commands []string `yaml:"commands"`
ID string `yaml:"-"`
}
func (w *Window) Create(tmux *Tmux) {
tmux.Run("new-window", "-t", w.ID, "-n", w.Name, "-c", w.Root)
}
func (w *Window) SendCommands(tmux *Tmux) {
if len(w.Commands) > 0 {
tmux.Run("send-keys", "-t", w.ID, strings.Join(w.Commands, ";"))
tmux.Run("send-keys", "-t", w.ID, "Enter")
}
}
func (w *Window) Focus(tmux *Tmux) {
tmux.Run("select-window", "-t", w.ID)
}