-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
94 lines (85 loc) · 2.83 KB
/
utils.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/o-richard/DSA/bellmanford"
"github.com/o-richard/DSA/bfs"
"github.com/o-richard/DSA/dfs"
"github.com/o-richard/DSA/dijkstra"
"github.com/o-richard/DSA/floydwarshall"
"github.com/o-richard/DSA/fordfulkerson"
"github.com/o-richard/DSA/heap"
"github.com/o-richard/DSA/huffman"
"github.com/o-richard/DSA/kosaraju"
"github.com/o-richard/DSA/kruskal"
"github.com/o-richard/DSA/linkedlist"
"github.com/o-richard/DSA/longestcommonsubsequence"
"github.com/o-richard/DSA/prim"
"github.com/o-richard/DSA/queue"
"github.com/o-richard/DSA/robinkarp"
"github.com/o-richard/DSA/search"
"github.com/o-richard/DSA/sort"
"github.com/o-richard/DSA/stack"
"github.com/o-richard/DSA/tree"
)
var packageTemplates = map[string]map[string]string{
"bellmanford": bellmanford.Templates,
"bfs": bfs.Templates,
"dfs": dfs.Templates,
"dijkstra": dijkstra.Templates,
"floydwarshall": floydwarshall.Templates,
"fordfulkerson": fordfulkerson.Templates,
"heap": heap.Templates,
"huffman": huffman.Templates,
"kosaraju": kosaraju.Templates,
"kruskal": kruskal.Templates,
"linkedlist": linkedlist.Templates,
"longestcommonsubsequence": longestcommonsubsequence.Templates,
"prim": prim.Templates,
"queue": queue.Templates,
"robinkarp": robinkarp.Templates,
"search": search.Templates,
"sort": sort.Templates,
"stack": stack.Templates,
"tree": tree.Templates,
}
func writeToFile(dir, fileName, content string) {
filePath := fmt.Sprintf("%v/%v.go", dir, fileName)
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
fmt.Println("unable to write to the file path (", filePath, "), ", err)
}
}
func generateTemplates(dirs []string) {
for _, dir := range dirs {
templates := packageTemplates[dir]
for fileName, content := range templates {
writeToFile(dir, fileName, content)
}
}
}
func fetchFileContent(dir, fileName string) {
url := fmt.Sprintf("https://raw.githubusercontent.com/o-richard/DSA/main/%v/%v.go", dir, fileName)
response, err := http.Get(url)
if err != nil {
fmt.Println("unable not fetch the solution of directory ", dir, " and file ", fileName, " from Github, ", err)
return
}
defer response.Body.Close()
content, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("unable to decode response, ", err)
return
}
writeToFile(dir, fileName, string(content))
}
func restoreSolutions(dirs []string) {
for _, dir := range dirs {
templates := packageTemplates[dir]
for fileName := range templates {
fetchFileContent(dir, fileName)
}
}
}