forked from jubnzv/go-tmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
145 lines (121 loc) · 3.53 KB
/
server.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// The MIT License (MIT)
// Copyright (C) 2019 Georgy Komarov <[email protected]>
//
// Represents a tmux server -- object that holds sessions and the windows and
// panes within them.
package tmux
import (
"errors"
"regexp"
"strconv"
"strings"
)
type Server struct {
SocketPath string // Path to tmux server socket
SocketName string // Name of created tmux socket
Sessions []Session // List of sessions used on server initialization
}
// List all sessions managed by this server.
func (s *Server) ListSessions() ([]Session, error) {
args := []string{
"list-sessions",
"-F", "#{session_id}:#{session_name}"}
if s.SocketPath != "" {
args = append(args, "-S", s.SocketPath)
}
if s.SocketName != "" {
args = append(args, "-L", s.SocketName)
}
out, _, err := RunCmd(args)
if err != nil {
return nil, err
}
outLines := strings.Split(out, "\n")
sessions := []Session{}
re := regexp.MustCompile(`\$([0-9]+):(.+)`)
for _, line := range outLines {
result := re.FindStringSubmatch(line)
if len(result) < 3 {
continue
}
id, err_atoi := strconv.Atoi(result[1])
if err_atoi != nil {
return nil, err_atoi
}
sessions = append(sessions, Session{Name: result[2], Id: id})
}
return sessions, nil
}
// Add session to server configuration. This will change only in-library
// server representation. Used for initial configuration before creating new
// server.
func (s *Server) AddSession(session Session) {
s.Sessions = append(s.Sessions, session)
}
// Create new session with given name on this server.
//
// Session always will be detached after creation. Call AttachSession to attach
// it. If session already exists, this function return an error. Check session
// with HaveSession before running it if you need it.
func (s *Server) NewSession(name string) (session Session, err error) {
if checkSessionName(name) == false {
return session, errors.New("Bad session name")
}
args := []string{
"new-session",
"-d",
"-D",
"-s", name,
"-P", "-F", "#{session_id}:#{session_name}"}
out, err_out, err_exec := RunCmd(args)
if err_exec != nil {
// It's okay, if session already exists.
if !strings.Contains(err_out, "exit status 1") {
return session, err_exec
}
}
re := regexp.MustCompile(`\$([0-9]+):(.+)`)
result := re.FindStringSubmatch(out)
if len(result) < 3 {
return session, errors.New("Error creating session")
}
id, err_atoi := strconv.Atoi(result[1])
if err_atoi != nil {
return session, err_atoi
}
session = Session{Name: result[2], Id: id}
return session, nil
}
// Kills session with given name. If killed session not found, KillSession will
// not raise error and just do nothing.
func (s *Server) KillSession(name string) error {
// Running "kill-session" without name causes killing attached session.
if checkSessionName(name) == false {
return errors.New("KillSession: Bad session name")
}
args := []string{"kill-session", "-t", name}
if _, _, err := RunCmd(args); err != nil {
return err
}
return nil
}
// Return true that session with given name is exsits on this server, false
// otherwise.
func (s *Server) HasSession(name string) (bool, error) {
if checkSessionName(name) == false {
return false, errors.New("Bad session name")
}
args := []string{"has-session", "-t", name}
_, err_out, err := RunCmd(args)
if strings.Contains(err_out, "can't find session") {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// Return list with all panes managed by this server.
func (s *Server) ListPanes() ([]Pane, error) {
return ListPanes([]string{"-a"})
}