Skip to content

Commit a66b54d

Browse files
committed
Create and save tmuxp config
1 parent 33eb99c commit a66b54d

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

pkg/kubetmuxp/kubetmuxp.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/arunvelsriram/kube-tmuxp/pkg/filesystem"
99
"github.com/arunvelsriram/kube-tmuxp/pkg/kubeconfig"
10+
"github.com/arunvelsriram/kube-tmuxp/pkg/tmuxp"
1011
yaml "gopkg.in/yaml.v2"
1112
)
1213

@@ -113,6 +114,23 @@ func (c *Config) Process() error {
113114
}
114115
c.kubeCfg.RenameContext(defaultCtxName, cluster.Context, kubeCfgFile)
115116

117+
fmt.Println("Creating tmuxp config...")
118+
windows := tmuxp.Windows{{Name: "default"}}
119+
env := tmuxp.Environment{"KUBECONFIG": kubeCfgFile}
120+
for k, v := range cluster.Envs {
121+
env[k] = v
122+
}
123+
124+
tmuxpCfg, err := tmuxp.NewConfig(cluster.Context, windows, env, c.filesystem)
125+
if err != nil {
126+
return err
127+
}
128+
129+
tmuxpCfgFile := path.Join(tmuxpCfg.TmuxpConfigsDir(), fmt.Sprintf("%s.yaml", cluster.Context))
130+
if err := tmuxpCfg.Save(tmuxpCfgFile); err != nil {
131+
return err
132+
}
133+
116134
fmt.Println("")
117135
}
118136
}

pkg/tmuxp/tmuxp.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"path"
55

66
"github.com/arunvelsriram/kube-tmuxp/pkg/filesystem"
7+
yaml "gopkg.in/yaml.v2"
78
)
89

910
// Window represents a window in a tmux session
1011
type Window struct {
11-
Name string
12-
Panes []struct{}
12+
Name string `yaml:"window_name"`
13+
Panes []struct{} `yaml:"panes"`
1314
}
1415

1516
// Windows represents a list of windows in a tmux session
@@ -20,9 +21,9 @@ type Environment map[string]string
2021

2122
// Config represents tmuxp config
2223
type Config struct {
23-
SessionName string
24-
Windows
25-
Environment
24+
SessionName string `yaml:"session_name"`
25+
Windows `yaml:"windows"`
26+
Environment `yaml:"environment"`
2627
filesystem filesystem.FileSystem
2728
tmuxpCfgsDir string
2829
}
@@ -33,6 +34,24 @@ func (c *Config) TmuxpConfigsDir() string {
3334
return c.tmuxpCfgsDir
3435
}
3536

37+
// Save saves the tmuxp config as file
38+
func (c *Config) Save(file string) error {
39+
writer, err := c.filesystem.Create(file)
40+
if err != nil {
41+
return err
42+
}
43+
44+
data, err := yaml.Marshal(c)
45+
if err != nil {
46+
return err
47+
}
48+
if _, err := writer.Write(data); err != nil {
49+
return err
50+
}
51+
52+
return nil
53+
}
54+
3655
// NewConfig returns a new tmuxp config
3756
func NewConfig(sessionName string, windows Windows, environment Environment, fs filesystem.FileSystem) (*Config, error) {
3857
home, err := fs.HomeDir()

pkg/tmuxp/tmuxp_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tmuxp_test
22

33
import (
4+
"bytes"
45
"fmt"
56
"testing"
67

@@ -45,3 +46,44 @@ func TestTmuxpConfigsDir(t *testing.T) {
4546

4647
assert.Equal(t, "/Users/test/.tmuxp", tmuxpCfg.TmuxpConfigsDir())
4748
}
49+
50+
func TestSave(t *testing.T) {
51+
t.Run("should save the tmuxp config as file", func(t *testing.T) {
52+
ctrl := gomock.NewController(t)
53+
defer ctrl.Finish()
54+
55+
mockFS := mock.NewFileSystem(ctrl)
56+
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
57+
var writer bytes.Buffer
58+
mockFS.EXPECT().Create("tmuxp-config.yaml").Return(&writer, nil)
59+
tmuxpCfg, _ := tmuxp.NewConfig("session", tmuxp.Windows{{Name: "window"}}, tmuxp.Environment{"TEST_ENV": "value", "ANOTHER_TEST_ENV": "another-value"}, mockFS)
60+
61+
err := tmuxpCfg.Save("tmuxp-config.yaml")
62+
63+
actualContent := writer.String()
64+
expectedContent := `session_name: session
65+
windows:
66+
- window_name: window
67+
panes: []
68+
environment:
69+
ANOTHER_TEST_ENV: another-value
70+
TEST_ENV: value
71+
`
72+
assert.Nil(t, err)
73+
assert.Equal(t, expectedContent, actualContent)
74+
})
75+
76+
t.Run("should return error if tmuxp config cannot be saved", func(t *testing.T) {
77+
ctrl := gomock.NewController(t)
78+
defer ctrl.Finish()
79+
80+
mockFS := mock.NewFileSystem(ctrl)
81+
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
82+
mockFS.EXPECT().Create("tmuxp-config.yaml").Return(nil, fmt.Errorf("some error"))
83+
tmuxpCfg, _ := tmuxp.NewConfig("session", tmuxp.Windows{{Name: "window"}}, tmuxp.Environment{"TEST_ENV": "value", "ANOTHER_TEST_ENV": "another-value"}, mockFS)
84+
85+
err := tmuxpCfg.Save("tmuxp-config.yaml")
86+
87+
assert.EqualError(t, err, "some error")
88+
})
89+
}

0 commit comments

Comments
 (0)