-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_connection.go
194 lines (156 loc) · 4.21 KB
/
save_connection.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
package main
import (
"bytes"
"encoding/gob"
"errors"
"github.com/1egoman/slick/gateway"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
const CONFIG_FILE_NAME = ".slickrc"
func GetConfigFileContents() map[string]string {
configFiles := make(map[string]string)
homeFilename := path.Join(os.Getenv("HOME"), CONFIG_FILE_NAME)
crawledHome := false
pathElements := strings.Split(os.Getenv("PWD"), "/")
for index := 0; index <= len(pathElements); index++ {
filename := "/" + path.Join(path.Join(pathElements[:index]...), CONFIG_FILE_NAME)
log.Println("Searching config path", filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
continue
}
log.Println("Config exists!", filename)
// When traversing through the tree, did we come across the `~/.slickrc`?
if homeFilename == filename {
crawledHome = true
}
configFiles[filename] = string(data)
}
// Finally, look for ~/.slickrc last, if applicable.
if !crawledHome {
data, err := ioutil.ReadFile(homeFilename)
if err == nil {
configFiles[homeFilename] = string(data)
}
}
return configFiles
}
//
// STORAGE OF SAVED CONNECTIONS
//
type SerializedConnection struct {
MessageHistory []gateway.Message
Channels []gateway.Channel
SelectedChannel gateway.Channel
Self gateway.User
Team gateway.Team
}
type SerializedGlobalState struct {
ActiveConnectionIndex int
SelectedMessageIndex int
BottomDisplayedItem int
}
func PathToSavedConnections() string {
return PathToCache() + "connections/"
}
func PathToCache() string {
return os.Getenv("HOME") + "/.slickcache/"
}
func SaveConnection(conn gateway.Connection) error {
if conn.SelectedChannel() == nil {
return errors.New("Can't save connection with no selected channel!")
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(SerializedConnection{
MessageHistory: conn.MessageHistory(),
Channels: conn.Channels(),
SelectedChannel: *conn.SelectedChannel(),
Self: *conn.Self(),
Team: *conn.Team(),
})
if err != nil {
return err
}
err = ioutil.WriteFile(PathToSavedConnections()+conn.Name(), buf.Bytes(), 0777)
if err != nil {
return err
}
return nil
}
func ApplySaveToConnection(name string, conn *gateway.Connection) error {
byt, err := ioutil.ReadFile(PathToSavedConnections() + name)
if err != nil {
return err
}
var serialized SerializedConnection
buf := bytes.NewBuffer(byt)
dec := gob.NewDecoder(buf)
err = dec.Decode(&serialized)
if err != nil {
return err
}
if conn == nil {
return errors.New("Passed connection was nil!")
}
(*conn).SetSelectedChannel(&serialized.SelectedChannel)
(*conn).SetChannels(serialized.Channels)
(*conn).SetMessageHistory(serialized.MessageHistory)
(*conn).SetSelf(serialized.Self)
(*conn).SetTeam(serialized.Team)
return nil
}
func SaveGlobalState(state *State) error {
// Get the index of the active connection
var activeConnectionIndex int = 0
activeConnection := state.ActiveConnection()
for ct, i := range state.Connections {
if i == activeConnection {
activeConnectionIndex = ct
break
}
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(SerializedGlobalState{
ActiveConnectionIndex: activeConnectionIndex,
SelectedMessageIndex: state.SelectedMessageIndex,
BottomDisplayedItem: state.BottomDisplayedItem,
})
if err != nil {
return err
}
err = ioutil.WriteFile(PathToCache()+"globalstate", buf.Bytes(), 0777)
if err != nil {
return err
}
return nil
}
func ApplyGlobalStateToState(state *State) error {
byt, err := ioutil.ReadFile(PathToCache()+"globalstate")
if err != nil {
return err
}
var serialized SerializedGlobalState
buf := bytes.NewBuffer(byt)
dec := gob.NewDecoder(buf)
err = dec.Decode(&serialized)
if err != nil {
return err
}
if state == nil {
return errors.New("Passed state object was nil!")
}
// If the user added or removed connections and this connection index wouldn't work, then don't
// use it.
if serialized.ActiveConnectionIndex < len(state.Connections) {
state.SetActiveConnection(serialized.ActiveConnectionIndex)
}
state.SelectedMessageIndex = serialized.SelectedMessageIndex
state.BottomDisplayedItem = serialized.BottomDisplayedItem
return nil
}