-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
125 lines (98 loc) · 2.26 KB
/
data.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
package main
import (
"os"
"strings"
"gopkg.in/yaml.v3"
)
var (
// configDirPath = "tmpdata/"
configDirPath = os.Getenv("HOME") + "/.config/falsename/"
DataDirPath = configDirPath
)
func init() {
if _, err := os.Stat(configDirPath); os.IsNotExist(err) {
os.MkdirAll(configDirPath, 0755)
}
dataDirPathFile := configDirPath + "data_dir_path.txt"
if _, err := os.Stat(dataDirPathFile); err == nil {
data, err := os.ReadFile(dataDirPathFile)
if err == nil {
DataDirPath = strings.TrimSpace(string(data))
if !strings.HasSuffix(DataDirPath, "/") {
DataDirPath += "/"
}
}
}
}
func SetDataDirPath(path string) {
DataDirPath = path
os.WriteFile(configDirPath+"data_dir_path.txt", []byte(DataDirPath), 0644)
}
func SaveCommand(name, command string) {
commands := GetAllCommands()
var foundExisting bool
for i, cmd := range commands {
if cmd.Name == name {
commands[i].Command = command
foundExisting = true
break
}
}
if !foundExisting {
commands = append(commands, Command{Name: name, Command: command})
}
if _, err := os.Stat(DataDirPath); os.IsNotExist(err) {
os.MkdirAll(DataDirPath, 0755)
}
f, _ := os.Create(DataDirPath + "aliases.yml")
defer f.Close()
enc := yaml.NewEncoder(f)
enc.Encode(commands)
}
func GetCommand(name string) string {
commands := GetAllCommands()
for _, cmd := range commands {
if cmd.Name == name {
return cmd.Command
}
}
return "--not found--"
}
func GetAllCommands() []Command {
f, _ := os.Open(DataDirPath + "aliases.yml")
defer f.Close()
var commands []Command
dec := yaml.NewDecoder(f)
dec.Decode(&commands)
return commands
}
func GetShell() string {
f, err := os.Open(DataDirPath + "shell.yml")
if err != nil {
return "/bin/sh"
}
defer f.Close()
var shell string
dec := yaml.NewDecoder(f)
dec.Decode(&shell)
return shell
}
func SetShell(shell string) {
f, _ := os.Create(DataDirPath + "shell.yml")
defer f.Close()
enc := yaml.NewEncoder(f)
enc.Encode(shell)
}
func DeleteCommand(name string) {
commands := GetAllCommands()
for i, cmd := range commands {
if cmd.Name == name {
commands = append(commands[:i], commands[i+1:]...)
break
}
}
f, _ := os.Create(DataDirPath + "aliases.yml")
defer f.Close()
enc := yaml.NewEncoder(f)
enc.Encode(commands)
}