forked from jubnzv/go-tmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
214 lines (182 loc) · 4.58 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// The MIT License (MIT)
// Copyright (C) 2019-2023 Georgiy Komarov <jubnzv@gmail.com>
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
tmux "github.com/jubnzv/go-tmux"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)
const (
version = "1.0.0"
)
var (
app = kingpin.New("tmux-session", "A utility to manage tmux sessions.")
verbose = app.Flag("verbose", "Enable additional output").Bool()
save = app.Command("save", "Write current tmux session in yaml")
savePath = save.Arg("path", "Path to saving yaml configuration").String()
fileName = save.Arg("name", "yaml file name").String()
load = app.Command("load", "Load tmux configuration from a yaml file")
loadPath = load.Arg("path", "Path to yaml configuration").String()
)
// Represents a tmux window configuration
type WindowConf struct {
WindowName string `yaml:"window_name"`
StartDirectory string `yaml:"start_directory"`
}
// Represents a tmux session configuration
type SessionConf struct {
SessionName string `yaml:"session_name"`
Windows []WindowConf `yaml:"windows"`
}
// Expand ~ in path.
func expandTilde(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
usr, err := user.Current()
if err != nil {
return "", err
}
path = filepath.Join(usr.HomeDir, path[2:])
}
return path, nil
}
// Generate tmux session name from yaml configuration filepath
func getSessionNameFromFilepath(fpath string) string {
session_name := filepath.Base(fpath)
session_name = session_name[0 : len(fpath)-len(filepath.Ext(session_name))]
return session_name
}
// Saves current tmux session in yaml file
func doSave(savePath string, fileName string, verbose bool) error {
// if !tmux.IsInsideTmux() {
// return errors.New("Not inside tmux.")
// }
// Get current session configuration
session_name, err := tmux.GetAttachedSessionName()
if err != nil {
return err
}
session := tmux.Session{Name: session_name}
windows, err := session.ListWindows()
if err != nil {
return err
}
// Generate filename if not specified
if len(fileName) == 0 {
fileName = session_name
}
// Collect windows configurations
windows_conf := []WindowConf{}
for _, w := range windows {
windows_conf = append(windows_conf, WindowConf{
WindowName: w.Name,
StartDirectory: w.StartDirectory})
fmt.Println(w.StartDirectory)
}
// Prepare session configuration
session_conf := SessionConf{
SessionName: fileName,
Windows: windows_conf}
conf_out, err := yaml.Marshal(&session_conf)
if err != nil {
return err
}
savePath, err = expandTilde(savePath)
if err != nil {
return err
}
err = ioutil.WriteFile(savePath, conf_out, 0644)
if err != nil {
return err
}
if verbose {
fmt.Printf("Saved in %s", savePath)
}
return nil
}
// Loads tmux session from json configuration file and attach to it.
func doLoad(loadPath string) error {
// Parse configuration
file_content, err := ioutil.ReadFile(loadPath)
if err != nil {
return err
}
conf_in := SessionConf{}
err = yaml.Unmarshal(file_content, &conf_in)
if err != nil {
return err
}
// Prepare go-tmux session
session := tmux.Session{Name: conf_in.SessionName}
for idx, w := range conf_in.Windows {
session.AddWindow(tmux.Window{
Name: w.WindowName,
// TODO: Read base-index from .tmux.conf
Id: idx + 1,
SessionId: session.Id,
SessionName: session.Name,
StartDirectory: w.StartDirectory,
})
}
// Create new session
server := new(tmux.Server)
server.AddSession(session)
sessions := []*tmux.Session{}
sessions = append(sessions, &session)
conf := tmux.Configuration{
Server: server,
Sessions: sessions,
ActiveSession: nil}
// Apply created session configuration
err = conf.Apply()
if err != nil {
return err
}
// Attach to created session
err = session.AttachSession()
if err != nil {
return err
}
return nil
}
func main() {
rc := 0
var err error
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case save.FullCommand():
// Prepare filepath
var path_to_save string
if *savePath == "" {
session_name, err := tmux.GetAttachedSessionName()
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
path_to_save = fmt.Sprintf("~/.tmux/sessions/%s.yaml", session_name)
} else {
path_to_save = *savePath
}
// Perform saving
err = doSave(path_to_save, *fileName, *verbose)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
case load.FullCommand():
// Perform loading
err = doLoad(*loadPath)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
}
if err != nil {
rc = -1
}
os.Exit(rc)
}