-
Notifications
You must be signed in to change notification settings - Fork 0
/
exelist.go
151 lines (123 loc) · 3.16 KB
/
exelist.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
type Exe struct {
Name string
Path string
}
// read a file with a specific (exelist) format and get the list in it
func importFromFile(fileName string) (retList []Exe, retErr error) {
// read the file
data, retErr := ioutil.ReadFile(fileName)
if retErr != nil {
return
}
// split file into string slice, each line a string
var entriesInFile = strings.Split(string(data), "\n")
// loop through the strings
for _, entryFull := range entriesInFile {
// split each line into two parts
var entryInTwo = strings.Split(entryFull, "=>")
// check if split was done right (has two parts)
// and only proceed then
if len(entryInTwo) != 2 {
continue
}
// append the two parts each to a field in an exe struct
var tempExe = Exe{
Name: strings.TrimSpace(entryInTwo[0]),
Path: strings.TrimSpace(entryInTwo[1]),
}
// append the exe struct to the returned list
retList = append(
retList,
tempExe,
)
}
return
}
// scan a directory and get a list of exe files in it (recursively)
func importFromScan(dirName string) (retList []Exe, retErr []error) {
// read the dir
var dirEntryList, readErr = ioutil.ReadDir(dirName)
// if had any errors
if readErr != nil {
// add error to list
retErr = append(retErr, readErr)
// exit
return
}
// go through dir entries
for _, dirEntry := range dirEntryList {
// create vars for the entry
var (
dirEntryName = dirEntry.Name()
dirEntryPath = path.Join(dirName, dirEntry.Name())
dirEntryNameNoSuffix = strings.TrimSuffix(dirEntryName, ".exe")
)
// act depending on it being a dir or file
if dirEntry.IsDir() {
// directories to skip
switch dirEntry.Name() {
case "Windows":
fallthrough
case "windows":
fallthrough
case ".cache":
fallthrough
case ".config":
continue
}
// recursive call to read dirs
var recurList, recurErr = importFromScan(dirEntryPath)
// assign the recursive err to return one
retErr = recurErr
// add result to caller
retList = append(retList, recurList...)
} else {
// if file has wrong extension just skip
if filepath.Ext(dirEntry.Name()) != ".exe" {
continue
}
// try opening the file
var readFile, readErr = os.Open(dirEntryPath)
defer readFile.Close()
// don't add files that can't be read
if readErr != nil {
// add caught error to list and go to next file
retErr = append(retErr, readErr)
continue
}
// then add to list
retList = append(retList,
Exe{
Name: dirEntryNameNoSuffix,
Path: dirEntryPath,
},
)
}
}
return
}
func exportToFile(fileName string, listToWrite []Exe) (retErr error) {
// read out the whole struct into a string
// with proper formatting
var dataAsString string
for _, element := range listToWrite {
dataAsString += fmt.Sprintf("%s => %s\n", element.Name, element.Path)
}
// write the acquired string to the specidied file
var writeErr = ioutil.WriteFile(fileName, []byte(dataAsString), os.FileMode(0755))
// if there was an error in writing return it
if writeErr != nil {
retErr = writeErr
return
}
return
}