This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.go
109 lines (92 loc) · 2.25 KB
/
list.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
package main
import (
"fmt"
"os"
"sort"
"github.com/go2c/optparse"
"github.com/onodera-punpun/prt/ports"
)
// listCommand lists ports.
func listCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argi := o.Bool("installed", 'i', false)
argr := o.Bool("repo", 'r', false)
argv := o.Bool("version", 'v', false)
argh := o.Bool("help", 'h', false)
// Parse arguments.
_, err := o.Parse(input)
if err != nil {
return fmt.Errorf("invaild argument, use `-h` for a list of arguments")
}
// Print help.
if *argh {
fmt.Println("Usage: prt list [arguments]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -i, --installed list installed ports only")
fmt.Println(" -r, --repo list with repo info")
fmt.Println(" -v, --version list with version info")
fmt.Println(" -h, --help print help and exit")
return nil
}
// Get all ports.
all, err := ports.All()
if err != nil {
return err
}
var db ports.Database
if *argi {
// Get installed ports.
if err := db.Parse(); err != nil {
return err
}
// Get port locations.
var pl []ports.Port
for _, n := range db.Packages {
p, err := ports.Locate(all, n.Name)
if err != nil {
// In case `err` is not `nil` we didn't manage to locate this
// port, this is most likely because the port is not in the
// ports-tree. We append an empty `Port` so that the variable
// `i` in the next for loop increments correctly.
pl = append(pl, ports.Port{})
} else {
pl = append(pl, p[0])
}
}
// I'm using all in the the following for loop, so copy `db` to `all`.
all = pl
}
var pl []string
for i, p := range all {
// If the Location is empty it means we didn't manage to locate a ports,
if p.Location.Port == "" {
continue
}
var s string
if !*argr {
s = p.Location.Port
} else {
s = p.Location.Base()
}
if *argv {
if *argi {
s += " " + db.Packages[i].Version
} else {
if err := p.Pkgfile.Parse(); err != nil {
fmt.Fprintf(os.Stderr, "%s%s\n", warning(config.
WarningChar), err)
continue
}
s += " " + p.Pkgfile.Version
}
}
pl = append(pl, s)
}
sort.Strings(pl)
for _, p := range pl {
fmt.Println(p)
}
return nil
}