forked from thevan4/GoDownloadPackages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlder.go
104 lines (90 loc) · 2.21 KB
/
dlder.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
type MainMap struct {
sync.Mutex
M map[string]uint
}
func main() {
var i uint
args := os.Args[:]
if len(args) > 2 {
log.Fatal("You must write package name, not just run programm!")
}
pack := args[1]
packagesMap := &MainMap{M: make(map[string]uint)}
Put(pack, i, packagesMap) // put "main" package
i++
RecurseDependens(packagesMap, i) // Get dependens for all packages (exclude some os pack)
folderName := "packages_for_" + pack //make new folder end move to it
os.Mkdir(folderName, 0700)
ex, _ := os.Executable()
exPath := filepath.Dir(ex)
packagesFullPath := exPath + "/" + folderName
err := os.Chdir(packagesFullPath)
if err != nil {
log.Fatal("Cant enter directory: ", packagesFullPath)
}
for keyPack := range packagesMap.M { // download all packages
andso, _ := exec.Command("apt", "download", keyPack).Output()
fmt.Println(string(andso))
}
}
func RecurseDependens(ma *MainMap, i uint) {
for key := range ma.M {
depends, err := ListPackageDepends(key)
if err != nil {
log.Fatal("Cant check dependens: ", err)
}
if depends != nil {
for _, dep := range depends {
ma.Lock()
keyExist := IsKeyExist(dep, ma)
if !keyExist {
Put(dep, i, ma)
i++
}
ma.Unlock()
}
}
}
fmt.Println("What we got:", ma, ". Total packeges: ", i)
}
func Put(key string, val uint, ma *MainMap) {
ma.M[key] = val
}
func IsKeyExist(key string, ma *MainMap) bool { //Check key exist
_, ok := ma.M[key]
if !ok {
return false
}
return true
}
func ListPackageDepends(pak string) ([]string, error) { //Get list of depends
outAptCache, err := exec.Command("apt-cache", "depends", pak).Output()
if err != nil {
return nil, err
}
stdoutresult := string(outAptCache)
arstr := strings.Split(stdoutresult, "\n")
var dependsArrStr []string
for _, da := range arstr {
if !(strings.Contains(da, "<") || strings.Contains(da, ">")) {
if strings.Contains(da, "PreDepends") {
da = string(da[13:])
dependsArrStr = append(dependsArrStr, da)
} else if strings.Contains(da, "Depends") {
da = string(da[11:])
dependsArrStr = append(dependsArrStr, da)
}
}
}
return dependsArrStr, nil
}