-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (79 loc) · 1.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strings"
)
var (
config string
session string
defaultSession string
dryrun bool
tmux string
)
func main() {
tmux = "/usr/local/bin/tmux"
defaultSession = path.Base(os.Getenv("PWD"))
flag.StringVar(&config, "f", ".ta", "the ta config file")
flag.StringVar(&session, "s", defaultSession, "session name")
flag.BoolVar(&dryrun, "d", false, "dry run mode")
flag.Parse()
file, err := os.Open(config)
if err != nil {
log.Fatal(err)
}
defer file.Close()
if existingSession(session) {
attachToSession(session)
return
}
for _, arguments := range Parse(session, file) {
if dryrun {
fmt.Printf("%s %s\n", tmux, strings.Join(arguments, " "))
} else {
err := exec.Command(tmux, arguments...).Run()
if err != nil {
log.Printf("args: %+v, err: %v\n", arguments, err)
}
}
}
if !dryrun {
cleanup(session)
attachToSession(session)
}
}
func existingSession(session string) bool {
cmd := exec.Command(tmux, Args{"ls", "-F", "\"#{session_name}\""}...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("OO: %v", err)
}
cmd.Start()
bytes, _ := ioutil.ReadAll(stdout)
if err := cmd.Wait(); err != nil {
return false
}
sessionName := fmt.Sprintf("\"%s\"", session)
for _, line := range strings.Split(string(bytes), "\n") {
if sessionName == line {
return true
}
}
return false
}
func attachToSession(session string) {
cmd := exec.Command(tmux, Args{"attach-session", "-t", session}...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
}
func cleanup(session string) {
action := "tmux kill-window -t 1"
exec.Command(tmux, sendKeys(session, "1", action)...).Run()
}