-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
90 lines (63 loc) · 1.53 KB
/
core.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
package main
import (
"bytes"
"fmt"
"os"
"strings"
)
type SyncEnvFile struct {
Entries []*SyncEnvEntry
}
type SyncEnvEntry struct {
Key string
Value string
}
// Append the new entry to the SyncEnv file and write it
func _add_env(floc string, file *SyncEnvFile) {
for _, item := range addFlag {
_entry := strings.SplitN(item, "=", 2)
if len(_entry) != 2 {
fmt.Println("Improper key-value Pair...")
continue
}
// create a entry
entry := SyncEnvEntry{
Key: _entry[0],
Value: _entry[1],
}
file.Entries = append(file.Entries, &entry)
}
_write_to_sy(floc, file)
}
// Update the varibles with new value to the SyncEnv file and write it
func _update_env(floc string, file *SyncEnvFile) {
for _, item := range updateFlag {
// Split with first "=" character
_entry := strings.SplitN(item, "=", 2)
if len(_entry) != 2 {
fmt.Println("Improper key-value Pair...")
continue
}
key := _entry[0]
new_value := _entry[1]
_look_up_and_set(file, key, new_value)
}
_write_to_sy(floc, file)
}
func _look_up_and_set(file *SyncEnvFile, key string, new_value string) {
for _, entry := range file.Entries {
if entry.Key == key {
entry.Value = new_value
return
}
}
fmt.Printf("Specified variable '%s' was not found\n", key)
}
func _write_to_sy(floc string, sycnfile *SyncEnvFile) {
buffer := &bytes.Buffer{}
for _, entry := range sycnfile.Entries {
_entry := fmt.Sprintf("export %s=%s\n", entry.Key, entry.Value)
buffer.WriteString(_entry)
}
os.WriteFile(floc, buffer.Bytes(), 0702)
}