forked from aptly-dev/aptly
/
cmd_mirror.go
217 lines (177 loc) · 5.16 KB
/
cmd_mirror.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
"log"
"strings"
)
func aptlyMirrorList(cmd *commander.Command, args []string) {
if len(args) != 0 {
cmd.Usage()
return
}
fmt.Printf("List of mirrors:\n")
repoCollection := debian.NewRemoteRepoCollection(context.database)
repoCollection.ForEach(func(repo *debian.RemoteRepo) {
fmt.Printf(" * %s\n", repo)
})
fmt.Printf("\nTo get more information about repository, run `aptly mirror show <name>`.\n")
}
func aptlyMirrorCreate(cmd *commander.Command, args []string) {
if len(args) < 3 {
cmd.Usage()
return
}
var architectures []string
archs := cmd.Flag.Lookup("architecture").Value.String()
if len(archs) > 0 {
architectures = strings.Split(archs, ",")
}
repo, err := debian.NewRemoteRepo(args[0], args[1], args[2], args[3:], architectures)
if err != nil {
log.Fatalf("Unable to create mirror: %s", err)
}
err = repo.Fetch(context.downloader)
if err != nil {
log.Fatalf("Unable to fetch mirror: %s", err)
}
repoCollection := debian.NewRemoteRepoCollection(context.database)
err = repoCollection.Add(repo)
if err != nil {
log.Fatalf("Unable to add mirror: %s", err)
}
fmt.Printf("\nMirror %s successfully added.\nYou can run 'aptly mirror update %s' to download repository contents.\n", repo, repo.Name)
}
func aptlyMirrorShow(cmd *commander.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
return
}
name := args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err := repoCollection.ByName(name)
if err != nil {
log.Fatalf("Unable to show: %s", err)
}
err = repoCollection.LoadComplete(repo)
if err != nil {
log.Fatalf("Unable to show: %s", err)
}
fmt.Printf("Name: %s\n", repo.Name)
fmt.Printf("Archive Root URL: %s\n", repo.ArchiveRoot)
fmt.Printf("Distribution: %s\n", repo.Distribution)
fmt.Printf("Components: %s\n", strings.Join(repo.Components, ", "))
fmt.Printf("Architectures: %s\n", strings.Join(repo.Architectures, ", "))
if repo.LastDownloadDate.IsZero() {
fmt.Printf("Last update: never\n")
} else {
fmt.Printf("Last update: %s\n", repo.LastDownloadDate.Format("2006-01-02 15:04:05 MST"))
fmt.Printf("Number of packages: %d\n", repo.NumPackages())
}
fmt.Printf("\nInformation from release file:\n")
for name, value := range repo.Meta {
fmt.Printf("%s: %s\n", name, value)
}
}
func aptlyMirrorUpdate(cmd *commander.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
return
}
name := args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err := repoCollection.ByName(name)
if err != nil {
log.Fatalf("Unable to update: %s", err)
}
err = repoCollection.LoadComplete(repo)
if err != nil {
log.Fatalf("Unable to update: %s", err)
}
err = repo.Fetch(context.downloader)
if err != nil {
log.Fatalf("Unable to update: %s", err)
}
err = repo.Download(context.downloader, context.database, context.packageRepository)
if err != nil {
log.Fatalf("Unable to update: %s", err)
}
err = repoCollection.Update(repo)
if err != nil {
log.Fatalf("Unable to update: %s", err)
}
}
func makeCmdMirrorCreate() *commander.Command {
cmd := &commander.Command{
Run: aptlyMirrorCreate,
UsageLine: "create",
Short: "create new mirror of Debian repository",
Long: `
create only stores metadata about new mirror, and fetches Release files (it doesn't download packages)
ex:
$ aptly mirror create <name> <archive url> <distribution> [<component1> ...]
`,
Flag: *flag.NewFlagSet("aptly-mirror-create", flag.ExitOnError),
}
cmd.Flag.String("architecture", "", "limit architectures to specified in the list, comma-delimited list")
return cmd
}
func makeCmdMirrorList() *commander.Command {
cmd := &commander.Command{
Run: aptlyMirrorList,
UsageLine: "list",
Short: "list mirrors of remote repositories",
Long: `
list shows full list of remote repositories.
ex:
$ aptly mirror list
`,
Flag: *flag.NewFlagSet("aptly-mirror-list", flag.ExitOnError),
}
cmd.Flag.Bool("v", false, "enable verbose output")
return cmd
}
func makeCmdMirrorShow() *commander.Command {
cmd := &commander.Command{
Run: aptlyMirrorShow,
UsageLine: "show",
Short: "show details about remote repository mirror",
Long: `
show shows full information about mirror.
ex:
$ aptly mirror show <name>
`,
Flag: *flag.NewFlagSet("aptly-mirror-show", flag.ExitOnError),
}
return cmd
}
func makeCmdMirrorUpdate() *commander.Command {
cmd := &commander.Command{
Run: aptlyMirrorUpdate,
UsageLine: "update",
Short: "update packages from remote mirror",
Long: `
Update downloads list of packages and packages themselves.
ex:
$ aptly mirror update <name>
`,
Flag: *flag.NewFlagSet("aptly-mirror-update", flag.ExitOnError),
}
return cmd
}
func makeCmdMirror() *commander.Commander {
return &commander.Commander{
Name: "mirror",
Short: "manage mirrors of remote repositories",
Commands: []*commander.Command{
makeCmdMirrorCreate(),
makeCmdMirrorList(),
makeCmdMirrorShow(),
//makeCmdMirrorDeestroy(),
makeCmdMirrorUpdate(),
},
Flag: flag.NewFlagSet("aptly-mirror", flag.ExitOnError),
}
}