-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
570 lines (443 loc) · 12.5 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/exp/slices"
"github.com/urfave/cli/v2"
xdeb "github.com/xdeb-org/xdeb-install/v2/pkg"
)
func readPath(subdir string) ([]string, error) {
path, err := xdeb.RepositoryPath()
if err != nil {
return nil, err
}
entriesPath := filepath.Join(path, subdir)
entries, err := os.ReadDir(entriesPath)
if err != nil {
return nil, fmt.Errorf("no entries found, please sync the repositories first")
}
subdirs := []string{}
for _, entry := range entries {
if entry.IsDir() {
subdirs = append(subdirs, entry.Name())
}
}
return subdirs, nil
}
func findDistribution(provider string, distribution string) (string, error) {
if len(distribution) > 0 && distribution != "*" {
distributions, err := readPath(provider)
if err != nil {
return "", err
}
if !slices.Contains(distributions, distribution) {
return "", fmt.Errorf(
"provider %s does not support distribution '%s', - omit or use any of %v",
provider, distribution, distributions,
)
}
return distribution, nil
}
return "*", nil
}
func findProvider(provider string) (string, error) {
if len(provider) > 0 {
providers, err := readPath("")
if err != nil {
return "", err
}
if !slices.Contains(providers, provider) {
return "", fmt.Errorf("provider '%s' not supported, omit or use any of %v", provider, providers)
}
return provider, nil
}
return "*", nil
}
func deb(context *cli.Context) error {
filePath := context.String("file")
if len(filePath) > 0 {
return file(context, filePath)
}
_, err := xdeb.FindXdeb()
if err != nil {
return err
}
path, err := xdeb.RepositoryPath()
if err != nil {
return nil
}
provider, err := findProvider(context.String("provider"))
if err != nil {
return err
}
distribution, err := findDistribution(provider, context.String("distribution"))
if err != nil {
return err
}
packageName := strings.Trim(context.Args().First(), " ")
if len(packageName) == 0 {
return fmt.Errorf("no package provided to install")
}
packageDefinitions, err := xdeb.FindPackage(packageName, path, provider, distribution, true)
if err != nil {
return err
}
return xdeb.InstallPackage(packageDefinitions[0], context)
}
func file(context *cli.Context, filePath string) error {
_, err := xdeb.FindXdeb()
if err != nil {
return err
}
var packageDefinition xdeb.XdebPackageDefinition
fileUrl, err := url.Parse(filePath)
isUrl := err == nil && fileUrl.Scheme != "" && fileUrl.Host != ""
if !isUrl {
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file '%s' does not exist", filePath)
}
return err
}
if !strings.HasSuffix(filePath, ".deb") {
return fmt.Errorf("file '%s' is not a valid DEB package", filePath)
}
packageDefinition = xdeb.XdebPackageDefinition{
Name: xdeb.TrimPathExtension(filepath.Base(filePath), 1),
}
packageDefinition.Configure(context.String("temp"))
if filePath != packageDefinition.FilePath {
// copy file to temporary xdeb context path
if err := os.MkdirAll(packageDefinition.Path, os.ModePerm); err != nil {
return err
}
data, err := os.ReadFile(filePath)
if err != nil {
return err
}
if err = os.WriteFile(packageDefinition.FilePath, data, os.ModePerm); err != nil {
return err
}
}
} else {
packageDefinition = xdeb.XdebPackageDefinition{
Name: xdeb.TrimPathExtension(filepath.Base(filePath), 1),
Url: filePath,
}
}
return xdeb.InstallPackage(&packageDefinition, context)
}
func search(context *cli.Context) error {
packageName := context.Args().First()
if len(packageName) == 0 {
return fmt.Errorf("no package provided to search for")
}
path, err := xdeb.RepositoryPath()
if err != nil {
return err
}
provider, err := findProvider(context.String("provider"))
if err != nil {
return err
}
distribution, err := findDistribution(provider, context.String("distribution"))
if err != nil {
return err
}
packageDefinitions, err := xdeb.FindPackage(packageName, path, provider, distribution, context.Bool("exact"))
if err != nil {
return err
}
for _, packageDefinition := range packageDefinitions {
fmt.Printf("%s/%s\n", packageDefinition.Provider, packageDefinition.Component)
fmt.Printf(" package: %s\n", packageDefinition.Name)
fmt.Printf(" distribution: %s\n", packageDefinition.Distribution)
if len(packageDefinition.Version) > 0 {
fmt.Printf(" version: %s\n", packageDefinition.Version)
}
fmt.Printf(" url: %s\n", packageDefinition.Url)
if len(packageDefinition.Sha256) > 0 {
fmt.Printf(" sha256: %s\n", packageDefinition.Sha256)
}
fmt.Println()
}
return nil
}
func sync(context *cli.Context) error {
lists, err := xdeb.ParsePackageLists()
if err != nil {
return err
}
args := context.Args()
providerNames := []string{}
for i := 0; i < args.Len(); i++ {
providerName := args.Get(i)
providerNames = append(providerNames, providerName)
}
err = xdeb.SyncRepositories(lists, providerNames...)
if err != nil {
return err
}
xdeb.LogMessage("Finished syncing: %s", strings.ReplaceAll(lists.Path, os.Getenv("HOME"), "~"))
return nil
}
func providers(context *cli.Context) error {
lists, err := xdeb.ParsePackageLists()
if err != nil {
return err
}
showDetails := context.Bool("details")
for _, provider := range lists.Providers {
fmt.Println(provider.Name)
fmt.Printf(" architecture: %s\n", provider.Architecture)
if provider.Custom {
fmt.Printf(" url: %s/%s/%s\n", xdeb.XDEB_INSTALL_REPOSITORIES_URL, xdeb.XDEB_INSTALL_REPOSITORIES_TAG, provider.Url)
} else {
fmt.Printf(" url: %s\n", provider.Url)
}
if showDetails {
for _, distribution := range provider.Distributions {
fmt.Printf(" distribution: %s\n", distribution)
for _, component := range provider.Components {
fmt.Printf(" component: %s\n", component)
}
}
}
fmt.Println()
}
return nil
}
func prepare(context *cli.Context) error {
version := context.Args().First()
var urlPrefix string
if len(version) == 0 {
// install master
urlPrefix = xdeb.XDEB_MASTER_URL
version = "master"
} else {
if version == "latest" {
// find latest release tag
urlPrefix = fmt.Sprintf("%s/latest", xdeb.XDEB_URL)
client := xdeb.NewHttpClient()
resp, err := client.Get(urlPrefix)
if err != nil {
return fmt.Errorf("could not follow URL '%s'", urlPrefix)
}
// get latest release version
releaseUrl := resp.Request.URL.String()
version = releaseUrl[strings.LastIndex(releaseUrl, "/")+1:]
}
urlPrefix = fmt.Sprintf("%s/download/%s", xdeb.XDEB_URL, version)
}
requestUrl := fmt.Sprintf("%s/xdeb", urlPrefix)
path := filepath.Join(os.TempDir(), "xdeb-download", "xdeb")
xdeb.LogMessage("Downloading xdeb [%s] from %s to %s ...", version, requestUrl, path)
xdebFile, err := xdeb.DownloadFile(path, requestUrl, false, false)
if err != nil {
return err
}
// move to /usr/local/bin
args := []string{}
if os.Getuid() > 0 {
xdeb.LogMessage("Detected non-root user, appending 'sudo' to command")
args = append(args, "sudo")
}
targetFile := "/usr/local/bin/xdeb"
args = append(args, "mv", xdebFile, targetFile)
err = xdeb.ExecuteCommand("", args...)
if err != nil {
return err
}
args = make([]string, 0)
if os.Getuid() > 0 {
xdeb.LogMessage("Detected non-root user, appending 'sudo' to command")
args = append(args, "sudo")
}
args = append(args, "chmod", "u+x", targetFile)
err = xdeb.ExecuteCommand("", args...)
if err != nil {
return err
}
path = filepath.Dir(xdebFile)
xdeb.LogMessage("Removing temporary download location %s ...", path)
err = os.RemoveAll(path)
if err != nil {
return err
}
args = make([]string, 0)
if os.Getuid() > 0 {
args = append(args, "sudo")
}
args = append(args, "xbps-install", "-S", "binutils", "tar", "curl", "xz")
return xdeb.ExecuteCommand("", args...)
}
func clean(context *cli.Context) error {
tempPath := context.String("temp")
if len(tempPath) == 0 {
return fmt.Errorf("please provide a temporary xdeb context root path")
}
xdeb.LogMessage("Cleaning up temporary xdeb context root path: %s", tempPath)
err := os.RemoveAll(tempPath)
if err != nil {
return err
}
if context.Bool("lists") {
path, err := xdeb.RepositoryPath()
if err != nil {
return err
}
xdeb.LogMessage("Cleaning up repository path: %s", path)
return os.RemoveAll(path)
}
return nil
}
func unixTime(epochString string) (*time.Time, error) {
if epochString == "now" {
now := time.Now().UTC()
return &now, nil
}
parsed, err := strconv.ParseInt(epochString, 10, 64)
if err != nil {
return nil, err
}
epoch := time.Unix(parsed, 0)
return &epoch, nil
}
var (
VersionString string = "dev"
VersionCompiled string = "now"
VersionAuthors string = "me <me@localhost>"
)
func main() {
authors := []*cli.Author{}
for _, authorString := range strings.Split(VersionAuthors, ",") {
authorItems := strings.Split(authorString, " <")
authorEmail := ""
if len(authorItems) > 1 {
authorEmail = authorItems[1]
}
authorEmail = strings.TrimSuffix(authorEmail, ">")
authors = append(authors, &cli.Author{
Name: authorItems[0],
Email: authorEmail,
})
}
compiled, err := unixTime(VersionCompiled)
if err != nil {
log.Fatalf("cannot parse compiled time from unix epoch: %s", err.Error())
}
app := &cli.App{
Name: xdeb.APPLICATION_NAME,
Usage: "Automation wrapper for the xdeb utility",
UsageText: fmt.Sprintf("%s [global options (except --file)] <package>\n%s [global options] command [command options] [arguments...]", xdeb.APPLICATION_NAME, xdeb.APPLICATION_NAME),
Description: "Simple tool to automatically download, convert, and install DEB packages via the awesome xdeb utility.\nBasically just a wrapper to automate the process.",
Version: VersionString,
Compiled: *compiled,
Authors: authors,
Suggest: true,
Action: deb,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "install a package from a local DEB file or remote URL",
Aliases: []string{"f"},
},
&cli.StringFlag{
Name: "provider",
Usage: "limit search results to a specific provider when --file is not passed",
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "distribution",
Usage: "limit search results to a specific distribution (requires --provider)",
Aliases: []string{"dist", "d"},
},
&cli.StringFlag{
Name: "options",
Aliases: []string{"o"},
Usage: "override XDEB_OPTS, '-i' will be removed if provided",
Value: "-Sde",
},
&cli.StringFlag{
Name: "temp",
Aliases: []string{"t"},
Usage: "set the temporary xdeb context root path",
Value: filepath.Join(os.TempDir(), "xdeb"),
},
},
Commands: []*cli.Command{
{
Name: "xdeb",
HelpName: "xdeb [release version]",
Usage: "install the xdeb utility to the system along with its dependencies",
Action: prepare,
},
{
Name: "providers",
Usage: "list available providers",
Aliases: []string{"p"},
Action: providers,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "details",
Usage: "display provider details (distributions and components)",
},
},
},
{
Name: "sync",
HelpName: "sync [provider list]",
Usage: "synchronize remote repositories",
Aliases: []string{"S"},
Action: sync,
},
{
Name: "search",
Usage: "search remote repositories for a package",
Aliases: []string{"s"},
Action: search,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "exact",
Aliases: []string{"e"},
Usage: "perform an exact match of the package name provided",
},
&cli.StringFlag{
Name: "provider",
Usage: "limit search results to a specific provider",
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "distribution",
Usage: "limit search results to a specific distribution (requires --provider)",
Aliases: []string{"dist", "d"},
},
},
},
{
Name: "clean",
Usage: "cleanup temporary xdeb context root path, optionally the repository lists as well",
Aliases: []string{"c"},
Action: clean,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "lists",
Aliases: []string{"l"},
Usage: "cleanup repository lists as well",
Value: false,
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
xdeb.LogMessage(err.Error())
os.Exit(1)
}
}