-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.go
209 lines (160 loc) · 3.87 KB
/
actions.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strings"
)
func hash(text string) string {
h := sha256.New()
h.Write([]byte(text))
return hex.EncodeToString(h.Sum(nil))
}
// This Action adds the Current directory to SyncEnv
func InitAction(file string) {
cdir, _ := os.Getwd()
// Create a SyncEnv file with hash of current directory
chash := hash(cdir)
floc := fmt.Sprintf("%s/%s.sy", SYNCENV_DIR, chash)
_, err := os.Stat(floc)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Adding Current Directory to SyncEnv...\n")
if file != "" {
data, err := os.ReadFile(file)
if err != nil {
fmt.Println("Error in Migrating:", err)
return
}
os.WriteFile(floc, data, 0702)
} else {
// create an empty file
err = os.WriteFile(floc, []byte{}, 0702)
if err != nil {
fmt.Println("Error in Creating the File:", err)
return
}
}
fmt.Printf("Current Directory Added Successfully\n")
return
} else {
fmt.Println("UnKnown Error in Init Action:", err)
return
}
} else {
fmt.Printf("Current Directory already added to SyncEnv!\nUse 'SyncEnv load' to load the variables\n")
return
}
}
// This actions loads the variables and should be eval'ed from shell
func loadAction(by_hook bool, file_path string) {
var msg string
var output string
// If local file is passed
if file_path != "" {
data, err := _load_from_file(file_path)
if err != nil {
if os.IsNotExist(err) {
msg = "echo File not found."
} else {
msg = "echo Some Unknown Error happened"
}
}
output = data
} else {
cdir, _ := os.Getwd()
chash := hash(cdir)
floc := fmt.Sprintf("%s/%s.sy", SYNCENV_DIR, chash)
data, err := os.ReadFile(floc)
if err != nil {
if os.IsNotExist(err) {
msg = "echo No file found to load. use 'SyncEnv --init' first."
} else {
msg = "echo Some Unknown Error happened."
}
}
output = string(data)
}
// Pop out the messages
if !by_hook && msg != "" {
fmt.Println(msg)
return
}
fmt.Print(output)
}
// Action to add new variables
func addAction() {
var syncfile SyncEnvFile
floc, _, err := loadSyncEnvFile(&syncfile)
if err != nil {
return
}
fmt.Printf("Adding the new variables...\n")
_add_env(floc, &syncfile)
fmt.Printf("Addition Action Completed\n")
}
// Function to update the existing variables
func updateAction() {
var syncfile SyncEnvFile
floc, _, err := loadSyncEnvFile(&syncfile)
if err != nil {
return
}
fmt.Printf("Updating the requested variables...\n")
_update_env(floc, &syncfile)
fmt.Printf("Updation Action Completed\n")
}
// Funciton to look at the variables stored
func peekAction() {
var syncfile SyncEnvFile
_, _, err := loadSyncEnvFile(&syncfile)
if err != nil {
return
}
fmt.Printf("Peek results!!!\n\n")
for _, entry := range syncfile.Entries {
fmt.Printf("%s=%s\n", entry.Key, entry.Value)
}
}
func _load_from_file(path string) (string, error) {
output := ""
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
output += fmt.Sprintf("export %s\n", line)
}
return output, nil
}
// Action that ports the SycnEnv variables to file
func portAction() {
file, err := os.Create(portFlag)
if err != nil {
fmt.Printf("Error in Creating the file: %s\n", err)
}
var syncfile SyncEnvFile
_, _, err = loadSyncEnvFile(&syncfile)
if err != nil {
return
}
for _, entry := range syncfile.Entries {
file.WriteString(fmt.Sprintf("%s=%s\n", entry.Key, entry.Value))
}
fmt.Println("Ported Successfully")
}
// Function to override hook the SyncEnv to current Session
func hookAction() {
switch shellFlag {
case "bash":
fmt.Print(BASH_HOOK)
case "zsh":
fmt.Print(ZSH_HOOK)
case "":
fmt.Println("echo No shell provided")
default:
fmt.Println("echo Shell not recognised. SyncEnv supports bash and zsh")
}
}