-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_state_handler.go
158 lines (140 loc) · 3.91 KB
/
save_state_handler.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
package main
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
)
type CurrentState struct {
Method httpMethod `json:"method"`
Url string `json:"url"`
RequestBody string `json:"request_body"`
RequestHeaders string `json:"request_headers"`
ResponseBody string `json:"response_body"`
ResponseHeaders string `json:"response_headers"`
}
func (m mainModel) returnCurrentValues() *CurrentState {
return &CurrentState{
Method: m.url.chosenMethod,
Url: m.url.textInput.Value(),
RequestBody: m.request.body.Value(),
RequestHeaders: m.request.headers.Value(),
ResponseBody: m.rawResponse.body,
ResponseHeaders: m.rawResponse.headers,
}
}
func writeCurrentStateToFile(f *os.File, content *CurrentState) {
bytes, err := json.MarshalIndent(content, "", " ")
if err != nil {
log.Println("Error marshilling content to JSON: ", err)
return
}
_, err = f.Write(bytes)
if err != nil {
log.Println("Error writing content to JSON file: ", err)
return
}
if err := f.Sync(); err != nil {
log.Println("Error syncing the file: ", err)
return
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func (m mainModel) storeCurrentState() {
userConfigDir, err := os.UserConfigDir()
if err != nil {
log.Println("Error finding the config dir: ", err)
return
}
filePath := filepath.Join(userConfigDir, "tgorq")
savedStateFile := filepath.Join(userConfigDir, "tgorq", "state.json")
// Check if the path exists. If not, create it.
if !fileExists(filePath) {
err := os.Mkdir(filePath, 0755)
if err != nil {
log.Println("Error making directory: ", err)
return
}
}
// Checking if the file exists and create it if not
if !fileExists(savedStateFile) {
f, err := os.Create(savedStateFile)
if err != nil {
log.Println("Error creating the state file: ", err)
return
}
defer f.Close()
}
f, err := os.OpenFile(savedStateFile, os.O_SYNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
fstat, _ := f.Stat()
if fstat.Size() != 0 {
err = f.Truncate(0)
if err != nil {
log.Println("Error truncating the file: ", err)
return
}
_, err = f.Seek(0, 0)
if err != nil {
log.Println("Error seeking the file: ", err)
return
}
}
writeCurrentStateToFile(f, m.returnCurrentValues())
}
func (m mainModel) stateFileExists() bool {
userConfigDir, err := os.UserConfigDir()
if err != nil {
log.Println("Error finding the config dir: ", err)
return false
}
savedStateFile := filepath.Join(userConfigDir, "tgorq", "state.json")
return fileExists(savedStateFile)
}
func (m mainModel) restorePreviousState() {
userConfigDir, err := os.UserConfigDir()
if err != nil {
log.Println("Error finding the config dir: ", err)
return
}
filePath := filepath.Join(userConfigDir, "tgorq")
savedStateFile := filepath.Join(userConfigDir, "tgorq", "state.json")
// Check if the path exists. If not, create it.
if !fileExists(filePath) {
return
}
// Checking if the file exists and create it if not
if !fileExists(savedStateFile) {
return
}
jsonFile, err := os.Open(savedStateFile)
if err != nil {
log.Println("Error open the JSON file: ", err)
return
}
defer jsonFile.Close()
byteValue, _ := io.ReadAll(jsonFile)
var currentState CurrentState
if err := json.Unmarshal(byteValue, ¤tState); err != nil {
log.Println("Error unmarshilling the JSON file: ", err)
return
}
log.Println("This is the current State variable: ", currentState)
m.url.httpMethodPaginator.Page = int(currentState.Method)
m.url.chosenMethod = currentState.Method
m.url.textInput.SetValue(currentState.Url)
m.request.body.SetValue(currentState.RequestBody)
m.request.headers.SetValue(currentState.RequestHeaders)
m.response.body.SetContent(currentState.ResponseBody)
m.response.headers.SetContent(currentState.ResponseHeaders)
}